Skip to Content

LoadLocal

This one’s in Lua because I use Neovim, but it should be directly translatable to VimScript.

I often need to rerun the same commands in a specific buffer. For example, I might regularly run one command to expand an XML metafile, and then another to run one of the expanded outputs.

function LoadLocal(local_cmd)
  vim.b.local_cmd = local_cmd
end

function RunLocal()
  vim.cmd(vim.b.local_cmd)
end

vim.cmd [[command! -nargs=1 LoadLocal call v:lua.LoadLocal(<f-args>)]]
vim.keymap.set('n', 'gxl', RunLocal, {silent = true})

Use it like this:

:LoadLocal !python %

Then, for that buffer and only that buffer, typing gxl will call python on the file.

Notes

I have a few different map leaders. The spacebar is my default <leader> for most mappings, but I also use \ for some navigational commands, gx for triggering scripts, and ; for insert-mode commands, like

inoremap ;r <c-R>+