Leader not used in mapping

Hello,

For some reason mapleader is set but when I include it in a mapping the mapping ignores it.
I set mapleader in init.lua at first but later tried setting it in the same file as the mapping just before I set the mapping.

echo mapleader
-----
,

Using the default mapping from the telescope docs.

vim.g.mapleader = ","

local builtin = require('telescope.builtin')
vim.keymap.set('n', '<leader>ff', builtin.find_files, {})
vim.keymap.set('n', '<leader>fg', builtin.live_grep, {})
vim.keymap.set('n', '<leader>fb', builtin.buffers, {})
vim.keymap.set('n', '<leader>fh', builtin.help_tags, {})

However whenever I just press ff without leader the mapping works.

I have tried setting the leader just above these mappings as well.

What is going on here?

How are you setting mapleader?

Hi mroavi,

I should have included this for completenes yeah. (added/edited it)

vim.g.mapleader = ","

This is set at the top level init.lua but for testing I moved this to the line above the mapping from my original post.

|- init.lua
|_lua
|  |- init.lua
|  |- mappings.lua

This is the dir structure, so it is set in mapping together with the mapping. Then fro mthe root init file I require/import the folder.

Hmm … your config looks exactly like mine. The differences are (1) I’m using space instead of comma as mapleader:

vim.g.mapleader = ' '

and (2) I’m not passing the empty table to vim.keymap.set:

vim.keymap.set("n", "<Leader>hi", builtin.highlights)

Could you try these changes and see if you get different behavior?

If this doesn’t help, then the proper way to debug this is to start with a clean config that only sets mapleader (like above) and creates a dummy keymap that for example prints hello world:

vim.keymap.set("n", "<Leader><Leader>", function() print("Hello World!") end)

This should work. Then, start adding the rest of your configuration to detect what makes it break.

You can use nvim -u my_config_file.lua, where my_config_file.lua contains the config of mapleader and the definition of the dummy keymap.

Thanks for the pointer on loading a specific config.

Turns out I made a stupid mistake. I wanted to separate mappings from the plugin configs, so I would be able to map and view it in one place.

Turns out I copied the default telescope mappings from the conffile without removing those. The original ones did not have the leader key set.

β”œβ”€β”€ autoload
β”‚   └── plug.vim
β”œβ”€β”€ colors
β”œβ”€β”€ ftplugin
β”œβ”€β”€ init.lua
β”œβ”€β”€ lua
β”‚   β”œβ”€β”€ mappings.lua
β”‚   β”œβ”€β”€ options.lua
β”‚   β”œβ”€β”€ plugsconf
β”‚   β”‚   β”œβ”€β”€ cmpconf.lua
β”‚   β”‚   β”œβ”€β”€ init.lua
β”‚   β”‚   β”œβ”€β”€ ionideconf.lua
β”‚   β”‚   β”œβ”€β”€ lspconf.lua
β”‚   β”‚   β”œβ”€β”€ lualine
β”‚   β”‚   β”‚   β”œβ”€β”€ lualineconf_bubbles.lua
β”‚   β”‚   β”‚   β”œβ”€β”€ lualineconf_evil.lua
β”‚   β”‚   β”‚   β”œβ”€β”€ lualineconf.lua
β”‚   β”‚   β”‚   └── lualineconf_slanted.lua
β”‚   β”‚   └── telescopeconf.lua
β”‚   β”œβ”€β”€ vars.lua
β”‚   └── vimplugs.lua
└── plugin
    └── packer_compiled.lua

My idea is to require/source plugin specific settings from the vimplugs.lua, this file installs using packer. Then afterwards at the bottom I require the conf files. The mappings.lua contains a module with methods for settings mappings. The idea is that I require a setup method on it from the main init and any mapping used in a setup block of packer also use a name specific method from mappings module. Still need to implement plug specific methods in mappings.

This is the best I came up with until now, just got sloppy when transitioning.

1 Like