Learn Practice Daily golf Reference

How to set a mark and jump back to it in Vim

In Normal mode, press ma to set mark a at the cursor. 'a jumps back to that line, and `a to the exact spot. Any letter from a to z works. Capital marks, like mA, work across files.

The ninja, further along a row of clay code strips, looks back at a small red flag lettered a planted in one of them, an ma keycap beside the flag.

Chorizo goes on the menu tomorrow at Byte Burrito. In menu.js it needs a price, a blurb, and the special at the top switched to it. You're on the pastor price, right where chorizo's price will go, and you want to do the other two jobs first and then land back here without looking for the line.

3export const SPECIAL = 'carnitas';
2export const PRICES = {
1 carnitas: 9,
4 pastor: 10,
1};
2export const BLURBS = {
3 carnitas: 'Slow-cooked pork',
4 pastor: 'Pork and pineapple',
5};
NORMALLoading the editor…Ln 4, Col :3
Try it here

Chorizo on the menu

The cursor is on the pastor price, where chorizo's price goes last.

Set mark a here with ma. Go to the top with gg and make chorizo the special: ci', chorizo, then Escmenu.js
3export const SPECIAL = 'carnitas';
2export const PRICES = {
1 carnitas: 9,
4 pastor: 10,
1};
2export const BLURBS = {
3 carnitas: 'Slow-cooked pork',
4 pastor: 'Pork and pineapple',
5};
NORMALLoading the editor…0.0sLn 4, Col :3
Open this text in the playground

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

Go back to the exact spot with a backtick

'a lands at the start of the marked line's text. The backtick version, `a, goes back to the line and the column you marked, which is what you want when you left off in the middle of a line.

Each lowercase letter is a separate mark, so mb leaves a where it is. Setting a letter again moves that mark to the cursor.

Try it here

Grilled pineapple

The pastor blurb should say grilled pineapple. The cursor is already on pineapple when you remember that pastor is today's special.

Set mark b on pineapple with mb. Go to the top with gg and change the special to pastor with ci', pastor and Escmenu.js
7export const SPECIAL = 'carnitas';
6export const PRICES = {
5 carnitas: 9,
4 pastor: 10,
3};
2export const BLURBS = {
1 carnitas: 'Slow-cooked pork',
8 pastor: 'Pork and pineapple',
1};
NORMALLoading the editor…0.0sLn 8, Col :21
Open this text in the playground

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

Vim also keeps one mark for you. `` goes back to where you were before your last jump, and '' to that line. How to jump back covers those and Ctrl-o. Your own marks stay where you put them, however many jumps you make after.

Delete or copy everything up to a mark

A mark is also a place to aim an operator at. d'a deletes every line from the cursor's line to the marked one, both included, and y'a copies them. The mark can be above the cursor or below it. This is how you remove a block too long to count: set ma on its first line, scroll or search down to its last, and press d'a.

Before
export const DRINKS = {
horchata: 3,
// summer only
agua_fresca: 3,
limonada: 3,
// all year
jarritos: 4,
};
majjd'a
export const DRINKS = {
horchata: 3,
// all year
jarritos: 4,
};
ma marks the summer comment, j j goes down to the last summer drink, and d'a deletes all three lines

With the backtick, the operator works on characters instead of whole lines. d`a deletes from the mark to the cursor, and the character at whichever of the two comes later stays.

Check yourself

Lines or characters

The cursor is on the p of price, in Normal mode. You set mark a with ma, go to the end of the line with $, then press d`a. What does the line say now?

BeforeJust a picture: pick an answer below (1, 2, 3, 4)
1const bill = price + tip;
NORMAL
Pick an answer (click it, or press its number), then watch the engine play the keys to check it.

Jump to a mark in another file

Lowercase marks belong to their file. Each file has its own a to z, so 'a in another file goes to that file's mark a, or says E20: Mark not set.

Capital marks, mA to mZ, remember the file as well, and there's only one of each. Set mV in your vimrc and 'V opens it again from wherever you are, at the marked line.

Vim and Neovim remember your marks after you quit: every capital mark, and the lowercase marks of the last hundred files you edited.

List and delete your marks

:marks lists every mark with its line, its column and the text on that line, or the file name for a capital mark in another file. The marks Vim sets for you are in the list too. :marks aB lists only those two.

:delmarks a deletes mark a, :delmarks a-d deletes a to d, and :delmarks! deletes every lowercase mark in the current file. Capitals go by name, as in :delmarks A. A deleted mark stays deleted the next time you open the file.

Practise the keys

Which mark keys?

Which keys do it?
You'll want to come back to this line later. Set mark c on it.
scroll here, then press it
1 of 5
You'll want to come back to this line later. Set mark c on it.
KeysDoes
masets mark a at the cursor (any letter from a to z)
'agoes to the line of mark a, at the start of its text
`agoes to the exact line and column of mark a
mA / 'Aa capital mark: it remembers the file, and works from any file
d'a / y'adeletes / copies the lines from the cursor's line to mark a
d`a / y`adeletes / copies the characters from the cursor to mark a
:'a,'bdruns an Ex command (here, delete) on the lines from mark a to mark b
`` / ''goes back to where you were before the last jump
`< / `>goes to the start / end of the last Visual selection
:markslists the marks, with line, column and text
:delmarks a / :delmarks a-d / :delmarks!deletes one mark / a range / every lowercase mark in the file