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

How do I run a Vim command without adding entries to the jump list?

Answer

:keepjumps {cmd}

Explanation

:keepjumps is a command modifier that suppresses any jump list updates caused by the command that follows it. This is invaluable when writing macros, mappings, or scripts that move the cursor for internal reasons — you don't want those intermediate positions cluttering <C-o> / <C-i> navigation for the user.

How it works

Normally, motions like G, /pattern, and :call cursor() add the current position to the jump list before moving. Prefixing with :keepjumps tells Vim to skip that recording:

:keepjumps normal! G

Without :keepjumps, pressing <C-o> after this would jump back to wherever G was called from. With it, the jump list is unchanged.

Example

A mapping that formats a buffer without polluting the jump list:

nnoremap <leader>= :keepjumps normal! gg=G``<CR>
  • gg=G — go to top and auto-indent the whole file
  • `` — jump back to the position before gg
  • Without :keepjumps, the jump list would now contain both the top-of-file position and the pre-format position, making <C-o> unpredictable

Tips

  • Pair with :keeppatterns to also avoid polluting the search history: :keepjumps keeppatterns g/pat/cmd
  • :keepjumps works with any Ex command that causes cursor movement: :keepjumps %s/old/new/g won't record the positions of each substitution
  • Marks set with m{a-z} during a :keepjumps block are still recorded — only the automatic jump list entries are suppressed

Next

How do I rename a variable across all its case variants (camelCase, snake_case, SCREAMING_CASE) in one command?