Keybindings

General Bindings

There are three style options for settings keybindings.

Vim style

Set bindings with vim.cmd. For more details read :help vim.cmd

# Just take your vim keybindings and wrap them in vim.cmd
vim.cmd("nnoremap W :w<CR>")

# Multiline Statements
vim.cmd([[
    map <Leader>bb :!bundle install<cr>
    map <Leader>gdm :Git diff master<cr>
    imap jj <esc>
]])

# Calling lua functions
vim.cmd("vim.cmd("nnoremap <silent> gi <cmd>lua vim.lsp.buf.implementation()<CR>")")
1
2
3
4
5
6
7
8
9
10
11
12

Neovim style

Use the vim.api.nvim_set_keymap function. Arguments for the function are: (mode, keybind, command, options). For more details read :help map-arguments

vim.api.nvim_set_keymap('n', '<Leader><Space>', ':set hlsearch!<CR>', { noremap = true, silent = true })
-- Vim equivalent
-- :nnoremap <silent> <Leader><Space> :set hlsearch<CR>

vim.api.nvim_set_keymap('n', '<Leader>tegf',  [[<Cmd>lua require('telescope.builtin').git_files()<CR>]], { noremap = true, silent = true })
-- Vim equivalent
-- :nnoremap <silent> <Leader>tegf <Cmd>lua require('telescope.builtin').git_files()<CR>

vim.api.nvim_buf_set_keymap(0, '', 'cc', 'line(".") == 1 ? "cc" : "ggcc"', { noremap = true, expr = true })
-- Vim equivalent
-- :noremap <buffer> <expr> cc line('.') == 1 ? 'cc' : 'ggcc'
1
2
3
4
5
6
7
8
9
10
11

LunarVim keybindings

Use <Leader>Lk to view the keybindings set by Lunarvim.

To modify a single Lunarvim keymapping

  -- X closes a buffer
  lvim.keys.normal_mode["<S-x>"] = ":BufferClose<CR>"
1
2

To remove keymappings set by Lunarvim

  -- use the default vim behavior for H and L
  lvim.keys.normal_mode["<S-l>"] = nil
  lvim.keys.normal_mode["<S-h>"] = nil
  -- vim.opt.scrolloff = 0 -- Required so L moves to the last line
1
2
3
4

Erase Lunarvim bindings and replace them with your own mappings

 lvim.keys.normal_mode = {
   -- Page down/up
   ["[d"] = "<PageUp>",
   ["]d"] = "<PageDown>",

   -- Navigate buffers
   ["<Tab>"] = ":bnext<CR>",
   ["<S-Tab>"] = ":bprevious<CR>",
 }
1
2
3
4
5
6
7
8
9

Listing what is mapped

Use <Leader>Lk to view the keybindings set by Lunarvim.

To see if a particular key has already been bound:

:verbose map <TAB>
1
  • :nmap for normal mode mappings
  • :vmap for visual mode mappings
  • :imap for insert mode mappings

Or just list every mapping

:map
1

To output this to a searchable buffer

:enew|pu=execute('map')
1

To view keybindings for the nvimtree plugin. Make sure you're in an nvimtree buffer and type g? to toggle the keybindings help

Whichkey Bindings

To add or remap keybindings for whichkey use lvim.builtin.which_key.mappings

Single mapping

Map a single key.

lvim.builtin.which_key.mappings["P"] = {
  "<cmd>lua require'telescope'.extensions.project.project{}<CR>", "Projects"
}
1
2
3

Adding a key to a existing submenu.

lvim.builtin.which_key.mappings["tP"] = {
  "<cmd>lua require'telescope'.extensions.project.project{}<CR>", "Projects"
}
1
2
3

Map a group of keys. Definitions would be triggered by pressing <Leader>td. The name for this menu would appear as Trouble.

lvim.builtin.which_key.mappings["t"] = {
  name = "Trouble",
  r = { "<cmd>Trouble lsp_references<cr>", "References" },
  f = { "<cmd>Trouble lsp_definitions<cr>", "Definitions" },
  d = { "<cmd>Trouble lsp_document_diagnostics<cr>", "Diagnosticss" },
  q = { "<cmd>Trouble quickfix<cr>", "QuickFix" },
  l = { "<cmd>Trouble loclist<cr>", "LocationList" },
  w = { "<cmd>Trouble lsp_workspace_diagnostics<cr>", "Diagnosticss" },
}
1
2
3
4
5
6
7
8
9

Replace all whichkey mappings

To clear all whichkey bindings and replace all mappings with your own, use this form.

lvim.builtin.which_key.mappings = {
  ["c"] = { "<cmd>BufferClose!<CR>", "Close Buffer" },
  ["e"] = { "<cmd>lua require'core.nvimtree'.toggle_tree()<CR>", "Explorer" },
  ["h"] = { '<cmd>let @/=""<CR>', "No Highlight" },

  p = {
    name = "Packer",
    c = { "<cmd>PackerCompile<cr>", "Compile" },
    i = { "<cmd>PackerInstall<cr>", "Install" },
    r = { "<cmd>lua require('lv-utils').reload_lv_config()<cr>", "Reload" },
    s = { "<cmd>PackerSync<cr>", "Sync" },
    u = { "<cmd>PackerUpdate<cr>", "Update" },
  },
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14

Example mappings

Consult the LunarVim configuration for whichkeyopen in new window to see more examples of how to map keys.

Leader Key

The default leader key is Space. This can be changed with the following

lvim.leader = "space"
1

Cursor Movement

By default, when pressing left/right cursor keys, Vim will not move to the previous/next line after reaching first/last character in the line. This can be quite annoying for new users. Fortunately this behaviour can be easily changed by putting this in your vimrc file:

To enable:

lvim.line_wrap_cursor_movement = true
1

Enabling maps the following command

set whichwrap+=<,>,h,l,[,]
1