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.

1 Like

I’d like to create something that is conceptually really simple; I’ve used both stock vi and stock Emacs (Gosling and GNU) since the eighties, but with vi, I never changed more than 1 or 2 “vi” settings and I never ever modified either vim or neovim, so the config language is not yet familiar to me, nor is Lua, though a few tools I’ve used, including neovim, do use Lua.
Here’s what I’d like to add, nothing else:

  1. I’d like to add something like space q to quit; could be bound to :q!
  2. I’d like to add something similar like space w to write out the file/buffer, like :w or :w!

If it’s really easy, perhaps a third space key binding to do a force write-quit, similar to :wqa

Do I have to set up gobs of lines, or is there an easy way to do this without having to become an expert vim, neovim or Lua script writer?

I didn’t end up doing a space key binding; instead I did what I’ve done with Emacs instances many times: I defined a few function keys to perform Ctrl and Esc key sequences; so with vim and nvim, I used F6-F9 to add a few variations for writing, saving, and quitting. Not the space key, but it accomplishes a similar objective using different keys.