How to more quickly jump around in Vim using marks
There is endless ways of both jumping and navigating in Vim. One of those ways is using something called marks. You can think of it like precise bookmarks that you can quickly set and then jump between.
Setting a mark
You set a mark (at the location of the cursor) with the key m
followed by a letter (a-z
or A-Z
). Do note that you get no visual indication that you have set a mark there and that the uppercase marks (A-Z
) works across your buffers.
If you set a mark using a uppercase letter it will open that document in the same buffer that you’re currently in, it will not spawn a new buffer.
Jumping to a mark
You can now jump to that exact position from anywhere in the document with the key `a
. Or if you use 'a
it will then jump to the beginning of the line where the mark a
was set.
Delete, change and yank text using marks
You can use marks to change, delete and yank text as well.
Delete:
d'a |
Delete all the lines from the current line to the line with the mark a . |
d`a |
Delete all the text from the current cursor position to the position of mark a |
Change:
c'a |
Change all the text from the current line to the line of mark a |
c`a |
Change all the text from the current cursor position to the position of mark a |
Yank:
y'a |
Yank all the lines from current line to the line with the mark a |
y`a |
Yank all the text from the current cursor position to the position of mark a |
Listing all the marks
You can list all marks using the command :marks
(or a specific mark with :marks a
or :marks abC
):
mark line col file/text
a 36 45 Yank all the lines from current line to the line with the
Scrolling through all the marks
It’s also possible to scroll/jump between all the marks as well.
]' |
Jump to the next line with a lowercase mark |
[' |
Jump to the previous line with a lowercase mark |
]` |
Jump to the next lowercase mark |
[` |
Jump to the previous lowercase mark |
I switched place with “`” and “’”
On my Swedish keyboard the character '
is a lot easier to access than the character `
. Because of that and the fact that I use the “`” feature a lot more, I deicided to switch place with them by adding this to my configuration file:
nnoremap ' `
nnoremap ` '
Source (and even more information): https://vim.fandom.com/wiki/Using_marks