Skip to main content

An introduction to Vim macros

Vim is my favourite text editor, because it is minimalist while also being insanely configurable. While I may describe my ideal Vim setup sometime in the future, I do want to share some tips and tricks that make Vim so damn efficient for certain tasks. Today, I shall focus on macros.

Here’s the key idea to keep in mind when discussing about Vim. Vim is not just an editor, it is a text manipulation program. Inserting text is just one of the many tasks that Vim can accomplish. A macro is a small Vim program that the user can record in order to make text editing more efficient.

Consider a simple use case. Let’s say that I have a CSV file that looks like this

New York,NY,USA
Seattle,WA,USA
Mumbai,MH,India

Let’s say that I want to add another column at the beginning of each line that is sequentially numbered, like so

1,New York,NY,USA
2,Seattle,WA,USA
3,Mumbai,MH,India

I could go down each line and manually add each column, but there’s a better way. The solution is to program a Vim macro that does the following tasks in order.

  1. Assuming that the first column of a certain line is the required number for this line (row), we copy the first column to the next line.

  2. Increment the line number by one so that it is one more than the previous line (row).

So, I edit the first line, in order to manually add the new column to the first line.

1,New York,NY,USA
Seattle,WA,USA
Mumbai,MH,India

Next, I ensure that I’m not executing any Vim command. I can verify this by pressing ESC a couple of times. Then I press q followed by a letter to identify this macro, e.g.: a. The macro then consists of the following keystrokes

  1. 0: Move to the beginning of the line.
  2. v: Visual mode.
  3. /,: Find the first , to match.
  4. y: Yank (copy) the highlighted text.
  5. j0: Go to the beginning of the next line.
  6. P: Paste the copied text. At this point, the cursor should be on the ,.
  7. h: Move the cursor one position to the left so that it is on the last digit of the number.
  8. Ctrla: Increment the number.
  9. q: Stop recording the macro.

Now, pressing @a would perform the nine steps outlined with just two key-presses. Let’s say the file had 50 more lines to be edited. In this case, I could use another powerful trick that Vim offers – the ability to repeat a command multiple times. So, ensure that I’m out of executing any command, I could type in a number followed by a command that would execute the specified command a certain number of times. So, pressing 50@a would execute the macro a fifty times, which is an insanely powerful technique for modifying multiple lines with a single command.

That’s all for Vim macros for today. I hope you found this post useful. Macros were one of the things I learnt relatively late as a Vim user, but now that I do know about this trick, I use it all the time, and it has saved me countless hours and a lot of headache when modifying code or text. Until the next time, when I take some time to write some more about cool Vim tricks and tips.

Popular posts from this blog

Progressive Snapshot: Is it worth it?

I turned 25 last year, which in the highly mathematical and calculating eyes of the US insurance industry meant that I had suddenly matured into a much more responsible driver than I was at 24 years and 364 days of age. As a result, I expected my insurance rates to go down. Imagine my surprise when my insurance renewal notice from GEICO actually quoted a $50 increase in my insurance rates. To me, this was a clear signal that it was time to switch companies.Typically, I score really high on brand loyalty. I tend to stick with a brand for as long as possible, unless they really mess up. This qualified as a major mess up. As a result, I started shopping for insurance quotes.Two companies that quoted me significantly lower rates (30%–40% lower) were Progressive and Allstate. Both had an optional programme that could give me further discounts based on my consenting to the companies tracking my driving habits. Now, I am a careful driver – I hardly ever accelerate hard. I hate using the brak…

Cornell Graduate Students United: At What Cost?

On Monday and Tuesday, we graduate students at Cornell will be voting on whether or not we want to unionise. Actually, scratch that, only graduate students who hold a TA, RA, or GRA appointment can unionise.This is a shitty arrangement, and I will be voting against it.For those of you who are not aware of how graduate school works at Cornell, you could be on one of many appointments.FellowshipA graduate student on a fellowship gets a stipend and tuition paid without associated teaching or research opportunities. Graduate students on a fellowship typically work towards their own theses, but will be excluded from the unionGraduate research assistantshipsA GRA gives a graduate student stipend and tuition without teaching responsibilities. However, this money comes out of a specific project grant, and the students typically work on their own theses. Students on GRAs magically qualify to join the union, whereas there is virtually no difference between a GRA and a fellowship for the most pa…

Build those noise cancelling headphones

So, here's another DIYLet me start by putting the cart before the horse. I shall start with the credits. This project was done while I was working on my Electronics Design Lab, along with my friends, Srujan M and Indrasen Bhattacharya. The work would not have been possible without the generous help received from the staff at Wadhwani Electronics Laboratory, who ensured that the only thing we did right was to leave the lab on time. This project would also not have been possible without the guidance of our dear and learned professors. It would probably have just about become additional dead weight on the head.Enough with the credits, now, I need to dive right into noise cancellation and how it works.The essence of sound is a pressure wave. The pressure wave, when incident on the eardrum sets into motion the complex mechanisms inside the ear, and after a long path, rather like the Cog advertisement, ends up making some nerves vibrate. The nerves send electrical signals to the brain, …