dimus
dimus

Reputation: 9372

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

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?

Upvotes: 4

Views: 5702

Answers (1)

severin
severin

Reputation: 2126

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.

Upvotes: 6

Related Questions