You're close, but vim.cmd("normal! <C-o>") doesn't work directly in a Lua function mapped to multiple modes because:
normal! only works in Normal mode. When you're in Insert or Visual mode, it doesn't make sense to execute Normal mode commands directly.
- Even if you were only in Normal mode,
vim.cmd("normal! <C-o>") wouldn't work as expected because <C-o> in a string isn't interpreted as the actual Ctrl+o keypress; it's just a literal string.
To achieve what you want—executing <C-o> and <C-i> from a Lua function across multiple modes—you need to:
- Check the current mode (
vim.api.nvim_get_mode().mode) and decide how to handle the command.
- Feed the correct key to Neovim as if it were pressed by the user.
Here's how you can do it:
local function jump_back()
local mode = vim.api.nvim_get_mode().mode
if mode == "i" then
-- In Insert mode, <C-o> works natively, so we feed it directly
vim.api.nvim_feedkeys(vim.api.nvim_replace_termcodes("<C-o>", true, false, true), "n", false)
elseif mode == "v" or mode == "V" or mode == "\22" then
-- In Visual mode, we need to exit Visual mode first with <Esc>, then <C-o>
vim.api.nvim_feedkeys(vim.api.nvim_replace_termcodes("<Esc><C-o>", true, false, true), "n", false)
else
-- In Normal mode, just <C-o> is fine
vim.api.nvim_feedkeys(vim.api.nvim_replace_termcodes("<C-o>", true, false, true), "n", false)
end
end
local function jump_forward()
local mode = vim.api.nvim_get_mode().mode
if mode == "i" then
-- In Insert mode, <C-i> is the same as <Tab>, so we feed <C-i> directly
vim.api.nvim_feedkeys(vim.api.nvim_replace_termcodes("<C-i>", true, false, true), "n", false)
elseif mode == "v" or mode == "V" or mode == "\22" then
-- In Visual mode, exit Visual mode first with <Esc>, then <C-i>
vim.api.nvim_feedkeys(vim.api.nvim_replace_termcodes("<Esc><C-i>", true, false, true), "n", false)
else
-- In Normal mode, just <C-i> is fine
vim.api.nvim_feedkeys(vim.api.nvim_replace_termcodes("<C-i>", true, false, true), "n", false)
end
end
vim.api.nvim_set_keymap('n', '<leader>o', '', { callback = jump_back })
vim.api.nvim_set_keymap('v', '<leader>o', '', { callback = jump_back })
vim.api.nvim_set_keymap('i', '<leader>o', '', { callback = jump_back })
vim.api.nvim_set_keymap('n', '<leader>i', '', { callback = jump_forward })
vim.api.nvim_set_keymap('v', '<leader>i', '', { callback = jump_forward })
vim.api.nvim_set_keymap('i', '<leader>i', '', { callback = jump_forward })
Explanation:
vim.api.nvim_get_mode().mode gets the current mode (i for Insert, n for Normal, v for Visual, V for Visual Line, \22 for Visual Block).
vim.api.nvim_feedkeys() simulates feeding keys to Neovim. The first argument is the keys to feed, the second ("n") means "do not remap these keys" (similar to noremap), and the third (false) means "do not trigger an event" (you usually want false here unless you know what you're doing).
vim.api.nvim_replace_termcodes() translates the string "<C-o>" or "<C-i>" into the actual key codes Neovim understands. This is crucial because just writing "<C-o>" in a string doesn't magically become the Ctrl+o keypress; you need this function to convert it.
Note: The callback option in nvim_set_keymap requires Neovim 0.7 or later. If you're using an older version, you'll need to upgrade or find another workaround (like using an autocommand to set mode-specific mappings).
Now, pressing <leader>o in Normal, Insert, or Visual mode should jump you back in the jumplist, and <leader>i should jump you forward!
Also, as a side note, if you really want to map exactly <C-o> and <C-i> (instead of using <leader>o and <leader>i), be aware that:
<C-o> in Insert mode already works natively (to execute one Normal mode command and return to Insert).
<C-i> is equivalent to <Tab> in most terminals and modes, so remapping it globally might have unintended consequences.
So, use these mappings wisely!