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

How do I make Ctrl-O and Ctrl-I behave like browser back and forward in Vim?

Answer

:set jumpoptions+=stack

Explanation

By default, Vim's jumplist can feel surprising: if you jump backward and then make a new jump, the old forward path is not always discarded like a browser history stack. Setting jumpoptions+=stack changes that behavior so jump navigation is more predictable during deep code exploration. This is especially useful when you chain motions such as gd, /search, marks, and quickfix jumps.

How it works

  • :set jumpoptions+=stack enables the stack flag in the 'jumpoptions' setting
  • With stack, when you go back with <C-o> and then perform a new jump, Vim discards the old forward branch
  • This gives you a true stack-like history instead of mixed forward states from earlier navigation branches

The result is cleaner mental context when traversing unfamiliar code.

Example

Suppose you jump through locations in this order:

A -> B -> C

You press <C-o> to return to B, then jump to D via a new search. With stack enabled, history becomes:

A -> B -> D

Now <C-i> takes you to D-related forward history, not stale entries from C.

Tips

  • Add it to your vimrc/init.vim so behavior is consistent across sessions
  • Pair with :jumps when debugging confusing navigation history
  • If you rely on legacy jumplist behavior, test this in one project before adopting globally

Next

How do I continuously record and restore sessions with vim-obsession?