Learn Practice Daily golf Reference

How to jump back to where you were in Vim

In Normal mode, press Ctrl-o to go back to where the cursor was before the last jump, and again to go further back. Ctrl-i or Tab goes forward. Jumps are the big moves, such as G, gg, a search or %.

The ninja turning round on a winding path of code-line stepping stones to follow a trail of glowing footprints back to a little flag where it started, a Ctrl-O keycap by the flag.

You're finishing total() in the burrito shop's order code, and the bill needs tax. The tax rate lives in another file, so you jump to the top to import it. With the import in, you want to be back on the line you were writing, without scrolling down and looking for it.

5import { PRICES } from './menu.js';
4
3export function total(items) {
2 let sum = 0;
1 for (const item of items) sum += PRICES[item];
6 return sum
1}
NORMALLoading the editor…Ln 6, Col :12
Try it here

Import the tax, then finish the line

The cursor is on the return you were writing.

Go to the top with gg, open a line above with O, type import { TAX } from './tax.js'; and press Escorder.js
5import { PRICES } from './menu.js';
4
3export function total(items) {
2 let sum = 0;
1 for (const item of items) sum += PRICES[item];
6 return sum
1}
NORMALLoading the editor…0.0sLn 6, Col :12
Open this text in the playground

This editor needs a keyboard. Read on, and play it at your computer.

Which moves count as jumps

Vim remembers where you were each time you make a big move, and Ctrl-o walks back through those places. These are the big moves:

  • G and gg, with or without a line number
  • a search with / or ?, and n, N, * and #
  • % to a matching bracket
  • { and } between paragraphs, ( and ) between sentences
  • H, M and L to the top, middle and bottom of the screen
  • going to a mark, and opening another file, so Ctrl-o can take you back to the file you came from

Small moves don't count. j, k, w, b, f and $ leave nothing behind, even with a count like 10j, and neither does scrolling with Ctrl-d. If you got somewhere with 10j, Ctrl-o skips straight past it to wherever the last big move started.

Check yourself

A search, then a little walk

The cursor is at the start of the first line. In Normal mode you search with /return and ↵, go up two lines with k k, then press Ctrl-o. Which line is the cursor on?

BeforeJust a picture: pick an answer below (1, 2, 3, 4)
1import { TAX } from './tax.js';
1import { PRICES } from './menu.js';
2
3export function total(items) {
4 let sum = 0;
5 for (const item of items) sum += PRICES[item];
6 return sum * (1 + TAX);
7}
NORMAL
Pick an answer (click it, or press its number), then watch the engine play the keys to check it.

Go back more than once, and forward again

Each Ctrl-o goes one jump further back, so after a few jumps you can retrace them in order. Ctrl-i goes forward again, for when you went one too far, and Tab does the same. A count works too: 3Ctrl-o goes back three jumps at once.

To see the trail, type :jumps. Each line is a place you jumped from, oldest at the top, and a > marks where Ctrl-o and Ctrl-i are in it. Vim keeps the last hundred jumps for each window.

Try it here

Two jumps back

You're adding a new burrito under veggie, and two fixes at either end of the file come first.

The last line has no semicolon. Go there with G, then A, ; and Escmenu.js
4// Byte Burrito prices
3export const PRICES = {
2 carnitas: 9,
1 pastor: 10,
5 veggie: 8,
1};
2export const DEFAULT = 'carnitas'
NORMALLoading the editor…0.0sLn 5, Col :3
Open this text in the playground

This editor needs a keyboard. Read on, and play it at your computer.

Flip to the last jump, or go to your last edit

Two backticks, ``, take you to where you were before the latest jump, at the exact column. Press them again and you're back where you started, so `` flips between two places, which is handy when you're comparing a function with the place that calls it. Two apostrophes, '', do the same but land at the start of the line's text.

To get back to your last edit, `. goes to the spot you last changed and '. to its line. They work however you moved away from it, even with nothing but j and k. g; steps back through your changes one at a time, newest first, and g, comes forward again. :changes lists them.

Check yourself

Back to the jump or the edit

The cursor is on the pastor line, in Normal mode. You add // spicy at the end with A and press Esc. Then you look at the top with gg and at the bottom with G. Now you press ``. Which line is the cursor on?

BeforeJust a picture: pick an answer below (1, 2, 3, 4)
3// Byte Burrito prices, in dollars
2export const PRICES = {
1 carnitas: 9,
4 pastor: 10,
1 veggie: 8,
2 chorizo: 11,
3};
4export const DEFAULT = 'carnitas';
NORMAL
Pick an answer (click it, or press its number), then watch the engine play the keys to check it.
KeysGoes to
Ctrl-owhere you were before the last jump, then further back each time
Ctrl-i or Tabforward again, after Ctrl-o
3Ctrl-o / 3Ctrl-ithree jumps back / forward at once
``the exact spot before the latest jump (press again to flip back)
''the line you were on before the latest jump
`. / '.the spot / the line of your last change
g; / g,back / forward through your changes
:jumpsthe list of jumps, with > at where you are
:changesthe list of changes