Moving around a file efficiently can improve your productivity, but it takes a while to get the hang of Vim's jumps. Last week I wrote some tips for practicing marks -- ideas for working marks into your daily workflow. Marks and jumps are closely related, mainly because of the jump list.
Certain commands are considered jumps: you probably already use the Normal mode command
G which means 'go to line'. Searching (
?) also causes jump list entries to be added, and the same is true for repeating searches (
N). Also, motions that move the cursor based on the window's size (
H) will add entries into the jump list.
These commands are motions, because they move the cursor, and the Vim manual calls them 'jump motions' because they add jump list entries.
There are three broad classes of jump motions:
- Freely jumping around a file. Example:
- Jumping based on the window size. Example:
- Text block jumps. Examples:
Other motions, like
j for moving down a line, won't add entries to the jump list. This means you can use jump commands strategically -- if you find yourself switching between two lines that are far apart, jumps are useful, but if you want to deviate slightly from these positions then other motions can be useful to avoid adding more entries to the jump list.
CTRL-O allows the jump list to be navigated, which is displayed in full by typing
:ju). Moving up the list with
CTRL-O will move the cursor to the previous position in the jump list, but it will also add an entry for the position that was jumped from.
Change List Jumps
Whenever a change is made that could be undone, Vim stores the cursor position in the change list. This is separate to the jump list, and can be viewed by typing
g; will go to the previous position in the change list. Like other motions this takes a count argument, so typing
3g; will go to the third older position. The opposite is
g, which moves forward through the list.
The jump list and change list add a strategic element to file navigation. With experience navigating using these features gets easier, but once you're aware of them it feels like having a navigation safety harness -- particularly when working with large and unfamiliar files.