vimtricks.wiki Concise Vim tricks, one at a time.

How do I execute a motion or command without adding an entry to the jump list?

Answer

:keepjumps {command}

Explanation

The :keepjumps modifier lets you run any movement or command without recording a new entry in the jump list. This is invaluable in autocommands, custom mappings, and Vimscript functions where you need to reposition the cursor programmatically—without polluting the jump list that <C-o> and <C-i> depend on.

How it works

  • Prefix any Ex command, motion, or normal call with :keepjumps
  • Vim executes the command normally but suppresses the jump list entry
  • The <C-o> / <C-i> history is untouched, so users can still navigate their own jumps naturally

Example

Without :keepjumps, this autocommand creates an unwanted jump entry every time a file is opened:

autocmd BufReadPost * normal! gg

With :keepjumps, the cursor moves to the top silently:

autocmd BufReadPost * keepjumps normal! gg

Similarly, in a mapping that jumps to a search match and returns:

nnoremap <leader>f :keepjumps execute 'normal! gg/TODO\n'<CR>

Tips

  • :keepjumps only suppresses jump list entries, not mark updates—'' (last jump mark) is still set
  • Combine with :keeppatterns when you also want to avoid altering the search register
  • Useful alongside :silent in scripts that need zero side effects on interactive state
  • keepjumps normal! {motion} is the most common pattern in plugin and autocommand code

Next

How do I create new files and directories directly inside Vim's built-in file browser without leaving Vim?