Vim
is one of the popular text editors of Linux. Because of its
capabilities, there is a large fan base of this command line based text
editor. In this article, we will discuss some impressive features of vim
editor that can help you save a lot of time while working on vim.
Introduction to VIM Editor macros
Have you ever come across a situation where you have to type
redundantly into files? For example, type a text repeatedly with changes
related to numbers only (that increase sequentially). Definitely, it
would be a highly mundane and unattractive task if we have to do it
manually. So, there is an easy and interesting way out with the help of vim macros.
Recording
The concept is basically recording our single individual operation
and repeat it as many times as we can, therefore automating the
redundant ones. Here is how we do the recording in vim:
- Start the recording by pressing key ‘q’ followed by a character name register, the recording to be saved to, let us say ‘x’.
- Do the individual operation which has to be done within the loop.
- Stop the recording by again pressing ‘q’
- Run the recording ‘i’ number of times by typing in the format
i@x
where,
i => A number representing how many times to run the recording
x => Run the recording identified by this register
Coming back to our example, let us get our hands on to it.
We have a requirement to list down all addresses in a particular
area, where all addresses are same except the house numbers which are
sequentially increasing.
So, create a new file using Vim editor using following command
$vim addr
Now, follow these steps :
- Enter the insert mode(press i) and type the first address of the area,
house no. 300 santa clara, ca
- Come out of the insert mode (press escape key), and then start the recording. Press q following by a.
- Next, copy the current line and paste it to next line by pressing yy and then p.
- Navigate the cursor to the number on second line and press ctrl + a (ie hold ctrl and press a)to increment the number.
- Stop the recording by pressing q.
- Run the newly created recording 12 times by typing 12@a.
It is amazing to see how we see 12 lines of text addresses with sequential house numbers.
house no. 300 santa clara, ca house no. 301 santa clara, ca house no. 302 santa clara, ca house no. 303 santa clara, ca house no. 304 santa clara, ca house no. 305 santa clara, ca house no. 306 santa clara, ca house no. 307 santa clara, ca house no. 308 santa clara, ca house no. 309 santa clara, ca house no. 310 santa clara, ca house no. 311 santa clara, ca house no. 312 santa clara, ca house no. 313 santa clara, ca
Similarly, we can do various similar tasks like typing redundant lines of text through the same way.
Further moving on to a next level, let’s say we have 1000 files and we want to convert the third line of every file with a text
I love macros
then on line number 5 type
Its awesome
Here are the steps to do this using our smart chap Vim “macro”.
- Open all files at once by vim * if all files are in the same directory OR vim <path of each file>.
- Press q and then x to start recording the macro in letter x.
- Go to line number 3. (ie Type :3 and press enter).
- To replace the text on this line press c + $. This is for changing everything from current cursor position till end of line.
- Now type I love macros and press escape to switch off the insert mode.
- To go to 5th line press the down arrow key twice. Remember you were at line number 3 at that time.
- Now again press c + $, type Its awesome and press escape to switch off insert mode.
- Now save these changes in current file and go to next file by typing :wn and press enter.
- These are the steps you want to perform so now save the macro by pressing “q”.
- If 1000 files were opened initially type 999 @x
This is superb, isn’t it? The usage of macros and creativity to use
them are unlimited and extends with your imagination. You can automate
your mundane tasks, make your life easier and proud to be lazy by
automating operations.
Macros in buffer
We have set a macro, and if we want to see what it contains, type
:enew
This will create a new file.
Now type :
"ap
Here a is the register in which our macro is stored. This will print the macro. Note that here the macro can also be edited.
This is what I get in my case
yyp^A
As expected, we see key combinations
yy – yanked
p – paste
^A – increment the number
The screen looks like :
You can edit it, say delete ^A and save it to the buffer using following key combination
^”ay$
Re-open our ‘addr’ file and now again run the macro in register a using following
3@a
Notice the difference. The house number is no more incrementing.
house no. 300 santa clara, ca house no. 301 santa clara, ca house no. 302 santa clara, ca house no. 303 santa clara, ca house no. 304 santa clara, ca house no. 305 santa clara, ca house no. 306 santa clara, ca house no. 307 santa clara, ca house no. 308 santa clara, ca house no. 309 santa clara, ca house no. 310 santa clara, ca house no. 311 santa clara, ca house no. 312 santa clara, ca house no. 313 santa clara, ca house no. 313 santa clara, ca house no. 313 santa clara, ca house no. 313 santa clara, ca
source: http://mylinuxbook.com
No comments:
Post a Comment