How to set Leader key in Lua?

I am in the process of converting my init.vim over to lua files instead. One thing I have been getting hung up on is that I can’t seem to set the leader key. In my init.vim I have the leader key set to space using let mapleader = ' '. I have tried using the idiomatic vim.g.maplader = ' ' in my lua setup but it is not working.

I also tried setting the leader key by using my version from the init.vim wrapped in a Neovim cmd vim.cmd 'let mapleader = " "' and that has not worked either. I am sure I am probably just missing something here. Any ideas?

In my config I have vim.g.mapleader = ‘;’ and vim.g.maplocalleader = ‘;’, but I’m not sure if that’s the best way or if it’ll work for you.

1 Like

I have tried both of those with no luck. I am not really sure why I am unable to set the leader key :confused: this totally breaks my entire workflow haha.

So I think I figured it out. A syntax error in another file seemed to be causing an issue? I won’t claim that I fully understand what happened here, cause I don’t , but my mapping of the leader key appears to be working as intended now with a simple vim.g.mapleader = ' '

3 Likes

Strange; glad you got it figured out though!

In case someone finds this thread:
I made the mistake to set mapleader after setting the keymaps with vim.keymap.set. vim.g.mapleader = ' ' works for me if I put it before the calls to vim.keymap.set. If you configure vim with both lua and vimsript, I suppose you have to consider the order in which the files are loaded.