This time I’d like to talk about some some useful tips how to move through a file. Vim motions and marks cannot only be helpful to jump to a specific location, but also to select, copy-paste and delete text passages.
Basic Vim motions
Of course, there are some simply and basic motions, such as:
- 0 jumps to the first character in line
- ^ jumps to the first non-blank character in line (nice for indent text)
- $ jumps to the last character in line
- gg jumps to the first line in the buffer
- G jumps to the last line in the buffer
- :42 or 42gg jumps to line# 42
Of course these are some basic motions and you might already know them very well 😉 But there’s more!
What you see
Something I’ve learned in the past and what I’m using regularly today, is the use of the predefined “marks” of the window. In my opinion, these three normal command keys are really useful if applied correctly:
- H jumps to the top of the window
- M jumps to the middle of the window
- L jumps to the bottom of the window
Now that’s nice, but only used for jumping it’s not very powerful. The nice thing about motions is, you can combine them – and this is where the magic happens.
Let’s say you want to cut some bits from a file and you don’t know if it’s 40, 41 or even 42 lines. If you’re a newbie, you might press dd (or . afterwards) several times until you’ve deleted all the lines in question. That might work for deleting a couple of lines, but isn’t very sexy. What about cutting the whole block so you can paste it later?
All of this works much better if you…
- Align the last line of the block on the end of the window
- Put the cursor on the first line of the block
- Press dL
Our fancy little Vim now cuts the text from your current cursor position until the end of the window.
When I wrote “align the last line of the block on the end of the window“, then you can use your cursor keys to move to the bottom of the window to scroll down. Unfortunately, that takes a lot of time because suddenly you’ve one line too much, so you’ve to scroll a line up again, means moving the cursor all through the window to the top again. To avoid those cursor movements, you can use the following keystrokes:
- Ctrl-d scroll down half a screen (default)
- Ctrl-u scroll up half a screen (default)
- Ctrl-e scroll one line down (extra line)
- Ctrl-y scroll one line up
With those keystrokes in mind, you can quickly align the last line on the end of the window and then go for the L thingy.
Custom marks
If there’s no visual Vim available, then marks become one of my best friends in Vim, especially when I’ve to code a lot in a crappy enterprise environment without proper editor.
As mentioned above, there are some predefined marks such as the start & end of the file or the window positions. But did you know you can define your own marks? Defining own marks and jumping to them is quite simple:
- m{a-zA-Z} Set mark {a-zA-Z} at cursor position
- ‘{a-z} Jump to the mark {a-z} in the buffer
- ‘{A-Z} Jump to the mark {A-Z} in the file where it was set
- :marks List all marks
And again, just defining and jumping to marks isn’t very fancy. However, you can combine them again to do awesome stuff like:
- d’a Delete lines from current cursor position to mark a
- >’a Indent lines from current cursor position to mark a
Did you ever wanted to jump between two positions in a single file, for example to compare something? Easy, just use ” and you’ll jump to your last position in file.
111 Comments