I wrote a little function to highlight a current line when navigating which basically mimics this emacs package. It works fine for navigating between windows.

navigation flash

function Util.flash_cursorline()
  vim.cmd([[hi CursorLine guifg=#000000 guibg=#ffffff]])
  vim.fn.timer_start(200, function()
    vim.cmd([[hi CursorLine guifg=NONE guibg=NONE]])
vim.cmd([[au WinEnter * lua require('util').flash_cursorline()]])

Is there an autocommand, or any other way to run a function when jumping between jumplist entries with CTRL-O, CTRL-I in normal mode?

  • Welcome to Vi&Vim SE. :h 'cursorline' isn't sufficient? (I can't tell what you have now because the gif isn't animated AFAICT.)
    – B Layer
    May 5 at 20:21
  • 1
    In Vim I you can just do nnoremap <C-o> <C-o>:call YourFunction<CR>. This would make the jump, then call the function. Not sure if that would work with Neovim/lua...
    – mattb
    May 5 at 21:51
  • @BLayer I'm using cursorline and it is helpful, but this feature would be nice to have too.
    – vikdotdev
    May 6 at 7:18
  • @mattb thank you, this gives me an idea for the solution
    – vikdotdev
    May 6 at 7:20
  • Cool. Just wanted to make sure you were aware of it (cursorline).
    – B Layer
    May 6 at 8:09

@mattb's reply gave me an idea to map additional function to <C-o> and <C-i> and it seems to be working alright.

vim.api.nvim_set_keymap("n", "<C-o>", "<C-o>:lua require('util').flash_cursorline()<CR>", { noremap = true, silent = true })
vim.api.nvim_set_keymap("n", "<C-i>", "<C-i>:lua require('util').flash_cursorline()<CR>", { noremap = true, silent = true })

  • Nice! But note that the <space><bar><space> you included is happening in Normal mode, so those are being interpreted as Normal-mode commands. You'll probably want to skip those. You should also include { silent = true } in your options, to prevent the :lua call from flashing on the command line. If this was regular Vimscript, I'd write it as :nnoremap <silent> <C-o> <C-o>:Lua require('util').flash_cursorline()<CR>, so you might want to use the equivalent.
    – filbranden
    May 6 at 20:44
  • 1
    @filbranden good points! I'll modify the answer.
    – vikdotdev
    May 7 at 7:18
  • I edited your answer to also drop the <bar>, which in this case was working as vimhelp.org/motion.txt.html#bar, which typically moves to a specific column in a line, given by a count. (Without a count, it goes to the first column, which is probably harmless here... But it's still unnecessary and unrelated to the issue itself, so I thought it was better to drop it.)
    – filbranden
    May 7 at 12:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.