Click here to Skip to main content
15,868,141 members
Articles / Mobile Apps

LINUX: Creating Macros in VIM Editor

Rate me:
Please Sign up or sign in to vote.
5.00/5 (1 vote)
18 Aug 2010CPOL2 min read 21K   3   1
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 Microsoft 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 won't 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 <end> 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 <esc> 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!

This article was originally posted at http://www.chiefsandendians.com/feeds/posts/default

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Chief Technology Officer Chiefs And Endians
United States United States
Come visit us at http://www.chiefsandendians.com

A compilation of varied technical gems learned over many years of experience in the industry.

Hint for the confused: Endian is a Computer Science term--the title is a play on words, not a misspelling.

Comments and Discussions

 
GeneralMore efficient ways to skin a cat... Pin
Woody146197-Sep-10 13:40
Woody146197-Sep-10 13:40 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.