Add Insert Mode Mappings for Vimtex Into Neovim
I use vimtex to aid my way of writing LaTeX in Neovim, but vimtex is written in vimscript and my Neovim configuration is in Lua. As a result, when I am not careful I may spend more time than acceptable to configure something.
Here is how to use vim.fn to access functions from plugins written in vimscript if you use lazy.nvim as your plugin manager.
This is the recommended configuration from vimtex’s repo using lazy.
1{
2 "lervag/vimtex",
3 lazy = false, -- we don't want to lazy load VimTeX
4 -- tag = "v2.15", -- uncomment to pin to a specific release
5 init = function()
6 -- VimTeX configuration goes here, e.g.
7 vim.g.vimtex_view_method = "zathura"
8 end
9}
If you want to access the vimtex#imaps#add_map
function to add a new insert mode map you can use several options, in particular vim.cmd and vim.fn
.
I went with with the later because it allows for a more Lua-like syntax by transforming Lua tables into vimscript ones.
However, you may be tempted to add any piece of configuration for vimtex inside of the plugin spec above. I am here to tell you…
DO NOT CALL
vim.fn
INSIDE THE PLUGIN SPEC!
I tried that, which means that I called vim.fn
inside of the function definition provided as value to the init
parameter.
If you do the same, since the plugin will not be loaded before it is loaded—silly, yes, but that’s what is happening—Neovim will scream that your function is unknown, because it is at the moment.
What you want to do, since this is a filetype thing, is to write this function call in the corresponding filetype configuration.
In my case—and most cases if you are configuring with Lua—this will be in the file after/ftplugin/tex.lua
.
In there you add
1--- ~/.config/nvim/after/ftplugin/tex.lua
2...
3vim.fn['vimtex#imaps#add_map']({lhs = '---', rhs = '\\textemdash', wrapper = 'vimtex#imaps#wrap_trivial'})
4...
And it’s done!
This insert mode mapping will be triggered by pressing <localleader>---
and will insert a \textemdash
command.
I know that writing ---
in LaTeX is exactly the same as using the \textemdash
command, but I like to have the command, that’s how I write and that’s why I did it.
To learn why the code above is like that, see :h vimtex-imaps.
The moral of this entry is, as usual: learn your tools.