DaleSchool

Marks and Jumps

Beginner15min

Learning Objectives

  • Mark a position with m{a-z}
  • Jump to a mark with '{mark}
  • Navigate the jump list with Ctrl+o and Ctrl+i
  • Inspect your marks with :marks

Working Code

Imagine editing a 500-line file. You define a function on line 50 and call it on line 300. Jumping between the two by memorizing line numbers gets tiring fast.

Marks let you save positions like bookmarks.

50G             ← jump to line 50
ma              ← mark this position as 'a'

300G            ← jump to line 300
mb              ← mark this position as 'b'

'a              ← jump to mark a (line 50)!
'b              ← jump to mark b (line 300)!

One character takes you exactly where you want.

Try It Yourself

Imagine the file below. Open it in Vim and practice:

// line 1: configuration
const API_URL = "https://api.example.com";
const TIMEOUT = 5000;

// ... code omitted ...

// line 50: function definitions
function fetchData(url) {
  return fetch(url);
}

// ... code omitted ...

// line 100: usage
fetchData(API_URL);
  1. In the configuration section, press mc (c for config) to set a mark.
  2. In the function definition section, press mf (f for function).
  3. In the usage section, press mu (u for usage).
  4. Hop between the three with 'c, 'f, 'u.
  5. Inspect your marks with :marks.

Now practice with the jump list:

gg              ← go to top of file
/fetchData      ← jump by search
50G             ← jump to line 50
Ctrl+o          ← go back to previous position (search match)
Ctrl+o          ← go back further (top of file)
Ctrl+i          ← go forward again (search match)

"Why?"

Marks are bookmarks in a file. Just as you bookmark web pages you visit often, you mark code positions you return to.

Lowercase marks (a-z) are valid only within the current file. You get 26 independent ones per file.

Uppercase marks (A-Z) are global. They cross files. If you set mA here and press 'A in another file, Vim jumps back to the original location. Great for pinning a main config file or entry function.

' (apostrophe) vs ` (backtick): 'a moves to the first non-whitespace character on the marked line. `a moves to the exact cursor position (line + column) where you marked.

The jump list is your browser's back/forward buttons. Every "big jump" (search /, line jump G, mark jump, etc.) is logged automatically. Press Ctrl+o (older) to go back, Ctrl+i to go forward.

Deep Dive

Special marks worth knowing

Vim maintains several marks automatically, without you setting them:

MarkMeaning
`.Location of the last edit
`"Cursor position when the file was last closed
''Position before the last jump
`[Start of the last changed/pasted text
`]End of the last changed/pasted text

'' (two single quotes) is especially handy — it takes you back to "where you just were."

Jump list vs. change list

The jump list (:jumps) records "big moves" of the cursor. Navigate it with Ctrl+o / Ctrl+i.

The change list (:changes) records where you edited. Navigate with g; (previous edit) and g, (next edit). Press g; when you think, "wait, where did I just change something?"

Open any real code file you're working on (or any long text file if not):

  1. At the top of the file, set mt (top).
  2. Near the middle, set mm (middle).
  3. At the bottom, set mb (bottom).
  4. Bounce between them with 't, 'm, 'b.
  5. Check your marks with :marks.
  6. Press Ctrl+o several times to walk back through the jump list.

Challenge: Set an uppercase mark with mC, open another file (:e otherfile), and press 'C. Do you return to the original file?

question: How do you save the current cursor position as mark f? answers:

question: Which key jumps to the previous position in the jump list? answers:

question: What's the difference between lowercase (a-z) and uppercase (A-Z) marks? answers: