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
normalcall 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
:keepjumpsonly suppresses jump list entries, not mark updates—''(last jump mark) is still set- Combine with
:keeppatternswhen you also want to avoid altering the search register - Useful alongside
:silentin scripts that need zero side effects on interactive state keepjumps normal! {motion}is the most common pattern in plugin and autocommand code