Vim 101: Marks
Part of the joy of using Vim is the ability to quickly move around a file, and even between files. Marks serve as a kind of bookmarking system, allowing positions within files to be saved and jumped to.
Some marks are persisted between sessions because Vim saves them in your .viminfo
file, so chances are you already have some marks stored. To see a list, open Vim and type :marks
. Notice how the list looks a little bit like registers -- the naming scheme for marks is similar.
Setting Marks and Jumping
Typing '0
will cause Vim to jump to the 0
mark, which is a "special mark" that represents the last file edited when Vim was exited.
To set a mark, type ma
-- this will store the current position in the a
mark. Move somewhere else within the file then type 'a
to go back. These lowercase marks are remembered as long as the file is in the buffer list. Conversely, the uppercase marks 'A
to 'Z
include the file name, so they can be used to efficiently jump between points in different files.
There are a lot more special marks, but one that’s worth knowing about is ''
. Typing this will move to the position before the last jump, or where the last mark as created. I find myself using this when I skip somewhere far away in a long file and have forgot to set a mark from where I left off.
To read more about these motion commands, take a look at :help mark-motions
.
Combined with :cd
Let’s say I’ve come back to my computer after a long weekend. I want to continue working where I left off on Friday. Rather than navigating to my project’s directory and opening Vim, I could simply open Vim, type '0
, and the last file I edited would appear.
That’s great, but later I may need to set the current directory to be my project’s directory. The :cd
command can be used to change directory, or :cd %:p:h
can be used to change directory to the currently open file. Now ctrlp.vim will do what I expect!