Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to write Lua commands in Neovim with mod keys (Ctrl, Alt, Shift etc)

Tags:

lua

neovim

If I want to move cursor down with Lua in Neovim I can use the command

:lua vim.cmd('normal j')

'Ctrl-E' combination in Vim/Neovim scrolls the window one line down. How do I use it with Lua? For example this approach does not work:

:lua vim.cmd('normal <C-e>')

How to provide modifier key sequences (Alt-, Ctrl-, Shift-) for Lua commands in Neovim?

like image 935
dimus Avatar asked Oct 19 '25 19:10

dimus


1 Answers

You have to escape the keycode using vim.api.nvim_replace_termcodes(). See the section on that function in nanotee's Nvim Lua Guide and in the Neovim API docs.

:lua vim.cmd(vim.api.nvim_replace_termcodes('normal <C-e>'))

In my config I go with nanotee's advice and define a helper function to avoid spelling out that insanely long function name.

local function t(str)
    return vim.api.nvim_replace_termcodes(str, true, true, true)
end

That shortens your example to

vim.cmd(t('normal <C-e>'))

if used in a scope where t() is defined.

like image 107
severin Avatar answered Oct 22 '25 14:10

severin