I always forget or make a mistake on how to make a macro in VI so today I noticed that I kept on messing up too much and this has gone on way to long:

  1. put the cursor where you want to start recording
  2. make sure your in command mode
  3. type the letter “q” then choose a letter between a-z or A-Z to assign the macro
  4. you should notice the “-recording-“ message in the status bar
  5. do your actions that you want in the macro
  6. if you use the insert command make sure to use “esc” to get out of insert mode before ending the macro
  7. end the macro by pressing “q”

to use the macro:

  1. position the cursor where you want to use the macro
  2. type “@” then the letter you choose for your macro

to repeat the macro many times:

  1. position the cursor where you want the macro to repeat
  2. type a number
  3. type “@”
  4. then type the macro letter

here is an example of howto use the # symbol to comment out a line for 300 lines:

  1. type “q”
  2. type “a” (assigns the macro to this letter)
  3. type “i” (go to insert mode)
  4. type “#” (to insert the comment)
  5. type esc to get out of insert mode
  6. type “h” to move one character to the left
  7. type “j” to move one line down
  8. type “q” to end the macro
  9. type “300” then “@” then “a” to repeat the macro 300 times
comments powered by Disqus