Need help with vimpeccable?
Click the “chat” button below for chat support from the developer who created it, or find similar developers for support.

About the developer

svermeulen
291 Stars 4 Forks MIT License 71 Commits 6 Opened issues

Description

Neovim plugin that allows you to easily map keys directly to lua code inside your init.lua

Services available

!
?

Need anything else?

Contributors list

# 61,085
Vim
viml
c-sharp
linq
64 commits
# 669,582
Lua
Shell
Python
1 commit
# 269,755
Svelte
sapper
CSS
HTML
1 commit

Vimpeccable

Write your .vimrc in Lua!

Vimpeccable is a plugin for Neovim that adds a simple lua api to map keys directly to lua code. This can be used to easily replace your vimscript-based

.vimrc
/
init.vim
with
init.lua
instead.

NOTE: Requires Neovim 0.5+

Table of Contents

Quick Start Example

Given the following example .vimrc:

call plug#begin(stdpath('data') . '/plugged')
Plug 'morhetz/gruvbox'
call plug#end()

set ignorecase set smartcase set incsearch

set history=5000

set tabstop=4 set shiftwidth=4

let mapleader = "<space>"

nnoremap hw :echo 'hello world'

" Toggle line numbers nnoremap n :setlocal number!

" Keep the cursor in place while joining lines nnoremap J mzJ`z

nnoremap ev :vsplit ~/.config/nvim/init.vim

colorscheme gruvbox

When using Neovim 0.5 and Vimpeccable, you could instead write it in lua or any lua-based language as well. You can do this by creating an

init.lua
file instead of
init.vim
with contents:
vim.cmd 'packadd paq-nvim'

require 'paq' { {'savq/paq-nvim', opt = true}; 'morhetz/gruvbox'; 'svermeulen/vimpeccable' }

vim.o.ignorecase = true vim.o.smartcase = true vim.o.incsearch = true

vim.o.hidden = true

vim.o.history = 5000

vim.o.tabstop = 4 vim.o.shiftwidth = vim.o.tabstop vim.g.mapleader = " "

vim.cmd('colorscheme gruvbox')

-- Note that we are using 'vimp' (not 'vim') below to add the maps -- vimp is shorthand for vimpeccable local vimp = require('vimp')

vimp.nnoremap('hw', function() print('hello') print('world') end)

-- Toggle line numbers vimp.nnoremap('n', function() vim.wo.number = not vim.wo.number end)

-- Keep the cursor in place while joining lines vimp.nnoremap('J', 'mzJ`z')

vimp.nnoremap('ev', ':vsplit ~/.config/nvim/init.lua') -- Or: -- vimp.nnoremap('ev', [[:vsplit ~/.config/nvim/init.lua]]) -- Or: -- vimp.nnoremap('ev', function() -- vim.cmd('vsplit ~/.config/nvim/init.lua') -- end)

vim.cmd('colorscheme gruvbox')

For the purposes of this example we use paq-nvim but you are of course free to use whichever plugin manager you prefer.

Then you can open Neovim and execute

:PaqInstall
, and then you should be able to execute all the maps from the example (eg.
hw
to print 'hello world',
ev
to open
init.lua
, etc.)

Vimpeccable Command Syntax

Vimpeccable mirrors the standard vim API and so has all the variations of

nnoremap
,
nmap
,
xnnoremap
, etc. that you probably are already familiar with.

The standard format to add a mapping in vimscript is:

[MODE](nore?)map [OPTIONS] [LHS] [RHS]

Where: -

MODE
can be one of
x
,
v
,
s
,
o
,
i
,
c
,
t
-
nore
is optional and determines whether the command is 'recursive' or not. Recursive here would, for example, allow executing other user-defined maps triggered from a user defined map. -
OPTIONS
can be one or more options such as
, 
, 
 etc.

Examples:

nnoremap hw :echo 'hello world'

" Note that we need to use recursive here we are mapping to a non-default RHS nmap c Commentary xmap c Commentary

nnoremap t :call g:DoCustomThing()

Vimpeccable mirrors the above except that it is a lua method call and therefore requires that each parameter is seperated by commas:

vimp.[MODE](nore?)map [OPTIONS?], [LHS], [RHS]

Examples:

-- Note that in lua we can represent strings either with quotes or with double square brackets
vimp.nnoremap('hw', [[:echo 'hello world']])

vimp.nmap('c', 'Commentary') vimp.xmap('c', 'Commentary')

-- Also note that we need to pass the options as a list instead of as seperate parameters -- Also note that unlike vimscript, the options are not surrounded with angle brackets vimp.nnoremap({'expr', 'silent'}, '1', [[:call g:DoCustomThing()]])

-- Or, alternatively, implement DoCustomThing in lua instead: vimp.nnoremap({'expr', 'silent'}, '1', function() -- Add logic here end)

Vimpeccable also comes with extra methods named

bind
and
rbind
which allow passing the mode as a parameter instead of needing to use different methods:
vimp.bind('n', 'hw', [[:echo 'hello world']])

-- plugs need to use rbind vimp.rbind('nx', 'c', 'Commentary')

Note that the only difference here is that

rbind
is 'recursive' so allows the use of custom user maps as part of the RHS value.

These methods can be especially useful to allow binding multiple modes at the same time. Note also that you can pass multiple values for LHS like this as well:

vimp.rbind('nx', {'c', 'gc'}, 'Commentary')

Which in vimscript would require 4 different statements for each variation.

Runtime Reloading of Entire Vimrc Plugin

For many vimmers, It is common to regularly be making tweaks to your vimrc.

In order to make edits at runtime without requiring a full restart of vim, often what people do is open up their vimrc and then simply execute

:so %
to re-source it. The lua equivalent of this would be
:luafile %
, however, if we were to attempt this when using vimpeccable we would get errors complaining about duplicate maps. This is a feature, not a bug, and is helpful to avoid accidentally clobbering existing maps. But how would we reload our vimpeccable config at runtime then?

To show how this is done, let's add the following to our

init.lua
from above:
-- r = reload vimrc
vimp.nnoremap('r', function()
  -- Remove all previously added vimpeccable maps
  vimp.unmap_all()
  -- Unload the lua namespace so that the next time require('config.X') is called
  -- it will reload the file
  require("config.util").unload_lua_namespace('config')
  -- Make sure all open buffers are saved
  vim.cmd('silent wa')
  -- Execute our vimrc lua file again to add back our maps
  dofile(vim.fn.stdpath('config') .. '/init.lua')

print("Reloaded vimrc!") end)

Let's also add a custom lua namespace, where we can place all our custom Lua that we'll want to reload. We can do this by: - Creating a new directory at

lua/config
next to our
init.lua
- Creating a new file in this directory named
util.lua
with contents:
local M = {}

M['unload_lua_namespace'] = function(prefix) local prefix_with_dot = prefix .. '.' for key, value in pairs(package.loaded) do if key == prefix or key:sub(1, #prefix_with_dot) == prefix_with_dot then package.loaded[key] = nil end end end

return M

Now we can open neovim again and execute

r
to fully reload our config. The call to
vimp.unmap_all
will remove all the maps that have been added via vimpeccable, and the call to
unload_lua_namespace
will clear all our custom lua code from the lua cache. If we did not do the
unload_lua_namespace
step, and then changed something inside our
lua/config
directory, then that code would not be reloaded.

To test our new

r
reload mapping, try changing the
hw
mapping to print something different, then press
r
and then
hw
to see the new text. Or, try adding another utility function to
util.lua
and then calling it from a new map, to ensure that code will be updated as well.

As your vimrc grows in complexity, you may want to split it up into multiple files, which we can now do easily in lua by using the

require
method.

Repeatable Maps

Vimpeccable can also optionally make custom maps repeatable with the

.
key. For example, given the following maps:
vimp.bind('[e', ':move--')
vimp.bind(']e', ':move+')

You might want to be able to hit

]e..
to move the current line three lines down. By default this would not work. You can fix this by making it repeatable by just passing in the
repeatable
option like this:
vimp.bind({'repeatable'}, '[e', ':move--')
vimp.bind({'repeatable'}, ']e', ':move+')

Note that this feature requires that vim-repeat is installed.

Duplicate Map Detection

By default, vimpeccable will reject any maps that are already taken. To see what that looks like, try adding the following map to the same

vimrc.lua
(assuming you're using the config from above):
vimp.bind('hw', function() print('hi!') end)

If you then execute

r
, you should see the following error or similar:

This is because we have already defined a map for

hw
above this line. Note that this error will not stop the rest of our config from loading. By default, Vimpeccable will simply log the error and continue, to ensure as much as your config can be loaded as possible.

In some cases you might want to override the previous mapping anyway, which you can do by passing in the

override
option like this:
vimp.bind({'override'}, 'hw', function() print('hi!') end)

If you then reload with

r
, and press
hw
, you should now see the new output.

Vimpeccable will also automatically detect maps that 'shadow' each other as well. For example, if we change our map to this instead:

vimp.bind('h', function() print('hi!') end)

And then attempt to reload again with

r
, we will get a similar error:

This is different from vim's default behaviour. If we added these maps using vimscript like this instead:

nnoremap hw :echo 'hello world'
nnoremap h :echo 'hi!'

Then every time we execute

h
, there would be a delay before we see the 'hi!' text printed, because vim needs to wait to see if you're in the process of executing
hw
instead.

Chord Cancellation Maps

If you find yourself using a lot of leader maps, you might notice that it is not possible to cancel a leader operation without sometimes causing unintended side effects. For example, given the following map:

vimp.bind('ddb', function() print("Executed map!") end)

If you then type

dd
and then hit any key other than
b
, you will find that the current line is deleted. This is because vim will do its best to try and match what you've already typed to another existing map, and in this case it chooses
dd
to delete the current line. A similar problem occurs if we type
d
and then hit any other key other than d, except in this case vim decides to just move the cursor one character to the right.

You can avoid these problems by adding the following to the bottom of your

vimrc.lua
:
vimp.add_chord_cancellations('n', '')

Now if we reload with

r
, then hit
dd
, then the line will no longer be deleted. And similarly, if we hit
d
, nothing will happen anymore.

Under the hood, what vimpeccable is actually doing here is adding maps for

dd
and
d
and explicitly mapping them to do nothing.

Buffer Local Maps

Vimpeccable also supports buffer local maps. Given this vimscript map:

nnoremap  t1 :echo 'buffer local map!'

As you might expect, the equivalent in lua would be:

vimp.nnoremap({'buffer'}, 't1', [[:echo 'buffer local map!']])
vimp.nnoremap({'buffer'}, 't2', [[:echo 'another buffer local map!']])

Or alternatively:

vimp.add_buffer_maps(function()
  vimp.nnoremap('t1', function() print('lua map!') end)
  vimp.nnoremap('t2', function() print('lua map two!') end)
end)

You can also specify the exact buffer if you know the buffer id like this:

vimp.add_buffer_maps(bufferId, function()
  vimp.nnoremap('t1', function() print('lua map!') end)
  vimp.nnoremap('t2', function() print('lua map two!') end)
end)

Advanced Options

  • vimp.always_override
    (default:
    false
    )
    • When true, all maps will be added as if they always have the
      override
      option set
  • vimp.all_maps
    • A dictionary containing the full list of maps and their associated info
  • vimp.current_map_info
    • A table of info for the currently executing map
  • vimp.maps_in_progress
    • A collection of all maps that are currently executing

User Command Maps

In some cases it might be better to define a custom action as a vim command rather than mapping it to a key. This way we don't use up any open key maps and our custom commands are discoverable on the command line by pressing tab (which can be easier than having to remember whatever leader map we chose). For example, you might want to define the following user commands in vimscript:

function! g:OpenFileOnGithub()
    echom "Open the URL on github for current file on current line"
endfunction

function! g:RenameFile(newName) echom "Todo - rename current file to " . a:newName endfunction

command! -nargs=0 SvOpenFileOnGithub call g:OpenFileOnGithub() command! -nargs=* SvRename call g:RenameFile()

Note here that I'm using

Sv
as a prefix on my commands so that I can just type
Sv
on the command line to see the full list.

To do this in lua with vimpeccable instead, you could do this:

vimp.map_command('SvOpenFileOnGithub', function()
  print("Todo - Open the URL on github for current file on current line")
end)

vimp.map_command('SvRename', function(newName) print("Todo - rename current file to " .. newName) end)

Note that vimpeccable will automatically fill in the

nargs
value for the command based on the given function signature.

Viewing Maps

If you want to view all the maps that are managed by vimpeccable, you can call

vimp.show_maps
. For example, you might add the following commands to your config:
-- Arguments: mode (optional)
vimp.map_command("ShowAllMaps", function(...) 
  -- Use empty string as prefix to select all
  vimp.show_maps('', ...)
end)

-- Arguments: prefix (required), mode (optional) vimp.map_command("ShowMaps", function(...) vimp.show_maps(...) end)

Then, you can execute

:ShowAllMaps
to view the full list of maps,
:ShowAllMaps i
for all insert-mode maps,
:ShowMaps 
for all leader maps,
:ShowMaps  n
for leader mode normal-mode maps, etc.

We use cookies. If you continue to browse the site, you agree to the use of cookies. For more information on our use of cookies please see our Privacy Policy.