So let's review some movement commands in VIM.
-
hjkl
for left up down right -
wbe
for jumping between words in a sentence.
Now let's say you have a huge paragraph you're jumping through. The whole thing is on one line, but it's like 200 words long. You want to edit word number 155, but you don't necessarily know it's word #155, you just know it's pretty far along in the paragraph.
How do you get there?
Well, if you're like me, you'll probably start hammering on w
, jumping word by word. But then you'll realize "damn, this is going to take a long time" (155 presses, but you're not aware of that).
What's next? Well, you can jump by 10 words, by typing 10w
. And you can repeat that by typing 10w10w10w10w10w10w
but that also takes a really long time.
So what's the solution?
Macros!!
Macros can be named with any single letter. So first, let's pick a macro name. We'll choose a
.
- Start recording the macro by typing
q
, then the macro namea
- Type your macro
- End the recording by typing
q
For the case we just had, where we want to repeat 10w
, we would type qa10wq
.
Then, to use the macro, we type @a
.
And finally, the best part, we can type @@
to repeat the previous macro.
Of course, this can be used for any action you want to repeat.
Hopefully this tip was helpful. Follow me on here or on twitter for more quick tips as they come up.
Top comments (2)
Macros definitely rock. I only tend to use them when operating on similar patterns of text multiple times in a row.
My preference for movement is to use one of these:
I
- insert before first character in current lineA
- insert after last character in current line0
- go to first character in current line$
- go to last character in current line[
- move a paragraph up]
- move a paragraph downI also really enjoy doing
f.
and then;
afterwards, to move from period to period in a block of (non code) text. The rarely used,
is nice to move to the previous result too. Love it!Vim is a beautiful "language"! Always cool to see more posts on the topic.
Thanks for sharing!
I'm not aware of
;
or,
commands, I dont think (sometimes when I get to a keyboard I'll realize I actually do use the shortcut without knowing it.I'll have to check them out!
Another problem I have to solve for switching to VIM full time is dropping multiple cursors. For example, a thing I commonly do in other editors is to drop multiple cursors where there is a similar pattern, like
',
after each array or list element in many languages.I suspect macros may be the answer to that problem as well.