Vim (The editor) is able to record and replay macros; unfortunately, this feature is often unknown. Since I learned about it, I'm not afraid any more to do large and monotonous editing tasks as Vim can do most of the work for me.
OK, but, what is a macro? A macro is a list of prerecorded actions that can be executed (replayed) at any time in an automated fashion. Macros are identified by a single letter and can be recorded while Vim is running — that they are volatile in the sense that they are forgotten when the editor is closed. I'm sure there is a way to store them in the configuration file but I haven't looked for it (yet).
In order to record a macro, press q followed by the name — a single letter — you want to assign to it. The most commonly used name is q (because it's quick to type after the recording command), so simply press this key twice, qq, to start recording a macro named q. You will see that the status line changes to recording so that you know what is going on.
With the editor in recording mode, do changes to the document as you'd usually do. The macro will remember everything; even changing the current document to another one (e.g., :wn) will be stored (useful in multifile edits). When you are done, simply press q again to stop recording.
So, now that you have your macro recorded, how do you replay it? Simply press @q and watch Vim redo your actions! Note that @ stands for the replay command and q indicates the macro's name.
Be careful when recoding macros, though. It is useful to stick to generic movements around the text rather than simple ones; that is, instead of moving letter by letter, move word by word, go to the beginning/end of the line, move X lines forward/backward, etc. This way, the macro will be applicable even if the text you are currently editing does not match the one you used to record the macro.
Monday, December 12, 2005
Subscribe to:
Post Comments (Atom)
9 comments:
I used this to put a tabspace before 32 lines without having to do it all manually:
qq[down][home][tab][escape]q
and then
32@q
Thanks for showing how macros work!
/Liffon
An even better way to put tabbing before a series of lines is to use visual block mode.
Move to the first character of the first line of the block you want to indent.
[control-v][press j/down enough to highlight the first character of each line of the block][shift-i][tab][escape]
A macro is a list of prerecorded actions that can be executed (replayed) at any time in an automated fashion. Macros are identified by a single letter and can be recorded while Vim is running — that they are volatile in the sense that they are forgotten when the editor is closed. I'm sure there is a way to store them in the configuration file but I haven't looked for it (yet).
Here is how you can store your macros in the configuration file:
1. record the macro. Let's say you identify the macro by letter a.
2. type :register a to see what sequence of commands is in the macro.
You might see something like this:
:register a
--- Registers ---
"a 0vf:Uvv
3. Insert a statement like this into your .vimrc
map ,, 0vf:Uvv
4. Restart vim.
- RareCactus
An even even better way to add a tab in front of 32 lines is to press:
>32>
That's 'Greater than' 3 2 'Greater than'
Or you could just use command mode to search and replace
:.,+31s/^/TAB/
*shrug*
32>> is even easier to type :)
I often don't know how many lines I want to tab so I just go to the first line and set a mark:
ma
Then go to the last line of the section I want indented and enter the command:
>'a
Thanks, your post helped me create a vim macro.
--
Regards
Parag Shah
http://www.adaptivelearningonline.net
Thanks for this quick tutorial.
I've been using VIM for years, even have the VIM O'Reilly book. It amazes me after all this time that I still find features that can help me be more productive!
Post a Comment