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.

Comments

Popular posts from this blog

On Harry Potter and why I dislike the series

There could not be a better time for this post. There could not have been a worse time for this post. Now that the penultimate movie of the series is out, and my facebook wall filled with people who loved the movie. But this is something I really wanted to say, and I shall say it anyway. Harry Potter is pathetic literature. Now, you must be wondering why I say that. There are many reasons. Firstly, the storyline itself is flawed. When a writer sits down to write anything, he/she must set up some essential rules about what is happening. These rules must remain constant irrespective of how many times he/she changes his/her mind. This is so that the readers are allowed to have some sensibility in what they are reading. In the fourth book, Rowling goes ahead and kills Cedric. Then, at the end of the book, the horseless carriages are there again. Nothing special. We all knew that they are horseless. But then comes the fifth book, and BAM, the horses are actually winged beasts that only thos

ERROR_SUCCESS

ERROR_SUCCESS. This macro would be familiar to all those who have done some programming in WIN32. It is the output of the GetLastError() function to check the thread's last error state when no error has occurred. Weird, isn't it? I mean, if it is a success, then why is it marked as an error in the macro? This is one example of a badly made API. APIs are considered bad when programming in them becomes non-intuitive. Software is said to be bad (or said to suck) when it seems counter-intuitive to the user. There is one very simple example of this. Start notepad. Type in any text. Click on close. The message that you see is: This makes no sense to me as a user. Of course, the programmer follows the approach that he creates a temporary file called Untitled , and in that file he allows the user to make all his changes. But how am I, as a user to understand that? A similar disconnect occurs even between two different programmers. That is why it takes a whole lot of effort to make

Elements of a Story: The Whispers

I'm compelled to begin each post with a meta. That way, my blog posts seem less like essays or dissertations, and more like diary entries, or web logs. So here goes... I started this blog a little over a year ago. The main purpose of this blog was to experiment with styles of writing, and find an effective outlet for all the subjects I wish to rant about; saving my classmates the agony of having to listen to them. As I wrote this blog, I've experimented with so many styles, and have received comments claiming that my work is a shameless copy greatly inspired by so-and-so author/work. Fact is that I simply chanced upon that style. I read, so obviously, my work shall reflect the styles of those I admire, but I've worked out so many styles without even knowing that they exist, only to be informed of them later. Recently, I've been struck with the seeming absence of whispers as an element of a story. The more I've thought of the subject, the more I've been convince