Showing posts with label repeat. Show all posts
Showing posts with label repeat. Show all posts

Wednesday, August 4, 2010

LINUX: Creating Macros in VIM Editor

A fantastically efficient way to repeat a series of actions is to use a macro. You may be familiar with macros from common products like MS Word. They are quite easy to create and use.

The simple steps we are going to follow are:
1. Assign macro to a key
2. Start recording
3. Do a bunch of stuff
4. Tell VIM we are done recording
5. Use the macro!
To assign the macro to a key and start recording in one shot, what we do is type "q" followed by whatever key we want to set the macro to. Let's say here we will use "s". So typing "qs" will cause a message "recording" to appear in the bottom left, indicating that we have started recording a macro to store in "s". Don't worry, you wont accidentally run the macro, by typing "s" since macros are run by typing the "@" symbol first.

Now that we are recording, it is time to do some stuff. Let us say that we need to add a semicolon to the end of the line, and then move the cursor to the next line to prepare for running the macro again. Here is what you do:
1. Type "i" to go into insert mode
2. Press key to go to end of line
3. Type ";" since we are in the right place
4. Press the down arrow key to move to the next line
5. Press Escape key to tell VIM we are done inserting text
6. Press "q" one more time to stop recording (the "recording" message goes away)
Now the macro is set. Type "@s" to run the macro. Now, let's say we want to run it on 20 lines of code...Use the trick learned in a previous post to repeat the macro 20 times: "20@s". Viola!

Friday, July 30, 2010

LINUX: Repeat Command/Action N Times in VIM Editor

All too often I find myself redoing a command over and over. I've often said, there must be a better way to do this. And there is! While writing some automation scripts in an agile "paired-programming" method yesterday, I learned a few neat tricks for VI from my programming partner. I'll share them in the next few posts.

The first is repeating a command. If, for example, you want to delete 100 lines from your file in VIM. Pressing the "d" key twice deletes a single line. So I used to just keep typing dd 100 times till everything I want was deleted. If you know how many times you want to run the command, you just type the number first, then the command.
100dd
Typing the above in VIM will repeat the single line delete (dd) command 100 times. How else can it be use? It can even insert text repeatedly! Type the following
50iTHE END IS NEAR<escape><escape>
The 50i tells VIM that you are going to insert some text that you want repeated 50 times. The text "THE END IS NEAR" is what will be repeated. And pressing the escape key twice implements it. You would get:
THE END IS NEARTHE END IS NEARTHE END IS NEARTHE END IS NEARTHE END IS NEARTHE END IS NEARTHE END IS NEARTHE END IS NEARTHE END IS NEARTHE END IS NEARTHE END IS NEARTHE END IS NEARTHE END IS NEARTHE END IS NEARTHE END IS NEARTHE END IS NEARTHE END IS NEARTHE END IS NEARTHE END IS NEARTHE END IS NEARTHE END IS NEARTHE END IS NEARTHE END IS NEARTHE END IS NEARTHE END IS NEARTHE END IS NEARTHE END IS NEARTHE END IS NEARTHE END IS NEARTHE END IS NEARTHE END IS NEARTHE END IS NEARTHE END IS NEARTHE END IS NEARTHE END IS NEARTHE END IS NEARTHE END IS NEARTHE END IS NEARTHE END IS NEARTHE END IS NEARTHE END IS NEARTHE END IS NEARTHE END IS NEARTHE END IS NEARTHE END IS NEARTHE END IS NEARTHE END IS NEARTHE END IS NEARTHE END IS NEARTHE END IS NEAR
Notice it is all one line. In order to make it appear on multiple lines, just press the enter key and then end of the text, before the escape. Very simple.