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

How do I run commands without polluting the jump list?

Answer

:keepjumps

Explanation

When writing scripts or running commands that move the cursor (like :g, :s, or :normal), Vim normally adds each cursor position to the jump list. This clutters your jump list and makes <C-o> / <C-i> navigation less useful. The :keepjumps modifier runs any Ex command without recording cursor movements in the jump list.

How it works

  • :keepjumps {command} — executes {command} normally but suppresses all jump list entries that would be created
  • The cursor still moves as expected; only the jump list recording is suppressed
  • Also suppresses changes to the changelist (g; / g,) for the executed command

Example

Without :keepjumps, a global substitute pollutes your jump list:

" This adds entries for every match to your jump list
:g/TODO/s/TODO/DONE/g

With :keepjumps, the jump list stays clean:

" Same substitution, but jump list is untouched
:keepjumps g/TODO/s/TODO/DONE/g

Another common use — jumping to the top of a file in a script without polluting jumps:

:keepjumps normal! gg

Tips

  • Particularly valuable in autocommands and Vimscript functions where you need to move the cursor but do not want to disrupt the user's navigation flow
  • Combine with keeppatterns to also avoid modifying the search register: :keepjumps keeppatterns g/pattern/d
  • Does not affect marks — only the jump list and changelist are suppressed

Next

How do I return to normal mode from absolutely any mode in Vim?