The Power of Recursive Macros in Vim

If for some crazy reason you’re not already a user of Vim, shutdown your computer and go think about your life choices.

Joking aside, Vim is really a great editor. And if you didn’t know – Vim supports macros.

Basics of macros in Vim

Macros represent a simple concept which can be described as “record the sequence of my actions, save them, and anytime I need them again, execute them.”

This is probably the most underused Vim feature which can improve your productivity dramatically. You can do all sorts of amazing stuff with your code using macros.

Incredibly powerful. Here’s a simple example:

What’s happening here:

  • qa – start recording macro in register a
  • i<Tab>' – enter Insert mode, insert Tab and 
  • Esc – get back to Normal mode (so I can run next command)
  • AAppend command, which places cursor at the end of the current line, in Insert mode
  • ', – Insert ',
  • Esc – get back to Normal mode again
  • j – Go down one line
  • ^ – Go to the start of the current line
  • q – Stop recording the macro

Then, using command @a I run the macro on the current line. After that, I can just hit @@ to run the macro I previously run.

Recursive macros

Using macros can be even more effective with recursion. Recursive macros are especially useful when you need to act on many lines in a file.

In order to record a recursive macro, you need to start with an empty register. You can make the register a empty by hitting qaq.

Now, let’s see recursive macro in action, with a slightly different example:

 

Let’s see what’s going on here:

  • qa followed by Tab and ' followed by Esc – same as in first example
  • f: – find the first occurrence of : and place cursor on it
  • C – command to delete everything from cursor to the end of the line, and enter Insert mode
  • ', followed by Escj and ^ – same as in the first example: insert ', get to Normal mode, move one line below and jump to beginning
  • @a – this is the key step: while recording a macro in register a, we call it inside itself!
  • q – stop recording the macro
  • @a – now we run the recursive macro – and there you go – magic! :)

 

There’s so much more you can do with macros in Vim!

My colleagues sometimes stare at my screen and wonder wtf is going on, when my hands are even not on the keyboard – and my code is being edited by a macro :D

You can master this magic too!

Get Macros chapter (plus three other!) from my book Mastering Vim Quickly for free.

Get 4 chapters for Free!


If you liked the post, we should get connected - follow me on Twitter

The “dot” command in Vim

This post is a part of a chapter from my book Mastering Vim Quickly: From WTF to OMG in no time

I believe you have already heard of the principle Don’t Repeat Yourself.

In software engineering, this is a principle of software development where your focus is on reducing repetition of all kinds. As you’ll see throughout the book, Vim has many ways and commands to automate different kinds of tasks, so you don’t have to repeat your actions.

One of the most powerful Vim command when it comes to avoiding repetition is the . (“the dot”) command.

Hitting . in Normal mode will repeat the last native Vim command you’ve executed.

Let’s say you want to delete 5 words from the cursor forward. As you already know, you could press 5dw and it’s done. However, sometimes it’s not convenient to mentally count the number of words.

An alternative would be to use dw to delete one word. And then press .... to call the dot command four times. In this case, you would repeat the latest, dw command, four more times, and in this way achieve the same effect without counting the words.

If you used dd to delete a line, and you want to delete 4 more lines, you could also execute 4. instead of pressing .... . That also works.

It’s very important to understand what is actually repeatable by the dot command. For example, if you have a sample code like this:

my $i
my $learn
my $quickly

and your cursor is positioned on the first line. You want to append ; to all three lines.
You could run a command like: A;<Esc>j

• A – would place your cursor at the end of the first line in Insert mode.
• ; – you press to actually insert it, and then you press Esc to get back to Normal mode.
• j – to move one line down

Now, your cursor is at the second line. If you then press . to repeat the change in next (second) line, this won’t work. Here’s what you’d get:

my $i;
my $learn;
my $quickly

Your cursor will still be at the second line rather than on the third line, but ; will be appended. This brings us to conclusion that only this part of our original command was repeated: A;<Esc>.

Now, why is this the case? It’s important to remember that with the dot command, you can repeat the commands which change the contents of the buffer.

A change is any command which you can use to modify your text. In our example, we had the case that command j wasn’t repeated, and our cursor wasn’t moved to the third line.

Commands like j are called motions (or nouns)—and they don’t affect the text itself. Command j just moves the cursor, but doesn’t change text anyhow, so it can’t be repeated.

Think in terms of the grammar of your native language: Not nouns, but verbs are used to express some sort of action. The same is true in Vim: nouns (or motions) can’t affect the text, so they can’t be repeated with the dot command.

Of course, if you’d like to repeat multiple changes, or a combination of movements and changes, you can easily record those into a macro. You can learn all you need on macros from Macros chapter of my book Mastering Vim Quickly-download it here. To see all the commands which can affect the text in a buffer, take a look at :help change.txt

Still reading? Every week I publish awesome tips on Vim in my Mastering Vim Quickly Newsletter. Join thousands of other Vim fans here: masteringvim.com


If you liked the post, we should get connected - follow me on Twitter

Persistent Undo in Vim

This post is a part of a chapter from my book Mastering Vim Quickly: From WTF to OMG in no time

As you already saw in the chapter on Undo/redo, Vim is pretty powerful when it comes to these features. However, there’s one more feature which I didn’t mention, as it takes a bit of configuration.

In Vim, like in every other text editor, you can perform undo/redo in your current session. Once the session is closed, and you reopen the same file, running undo will do nothing – as you will be already at the oldest change.

Vim supports persistent undo, which means that you can run undo/redo even from your previous sessions.

 

Persistent Unto in Vim

Persistent Undo in Vim

 

This is great feature indeed. This way you can go back historically through changes of any of your files.

How this works? Vim creates hidden file where it stores the undo history, for every file you edit. Now, configuration is very simple. You could add only this line to your .vimrc:

set undofile " Maintain undo history between sessions

and it would work.

However, Vim will write undo history files in the same directory as the file you edit. Overtime, this will become messy. You don’t want that. That’s why, I recommend you complete the next two steps as well.

First, create a dedicated directory for these undo history files, by running a command like:

$ mkdir ~/.vim/undodir

My assumption is that ~/.vim is your Vim directory, where your .vimrc, among others, is located.

Then, once you have created the directory, you need to add only one more line to your .vimrc file:

set undodir=~/.vim/undodir

That’s all. Vim will store all the undo history files in that directory, and you’ll have persistent undo working flawlessly.

 

 


Would you like to get 4 chapters from Mastering Vim Quickly for Free? Sign up below:


If you liked the post, we should get connected - follow me on Twitter

5 Phases of Vim Use

 

1. WTF is going on here?

 

2. Hmm… this is cool. I want to see other interesting features this thing has.

 

3. OMG. <mind blown>

 

4. The worst thing about Vim…

worst_thing_vim

 

5. I’m actually a wizard… and this is how I feel when someone sees me using Vim

 

vim_wizard

 


Over the years, Vim got a reputation that it’s really difficult to learn it. I’ve heard many times from guys who are convinced it will take them months to reach proficiency. That’s simply wrong.

That’s why I wrote a book: Mastering Vim Quickly (from WTF to OMG in no time) which will teach you Vim the way I learned it – easily and quickly.

 


If you liked the post, we should get connected - follow me on Twitter

Saving Read-only Files in Vim – The sudo tee trick

How many times you had the following situation: You open a file with Vim and make some changes. When you try to save the file, you realize that you didn’t run Vim with sudo?!

There is a solution for this. The next time when you get into this situation, you can use the following command in order to save the changes you made:

:w !sudo tee % >/dev/null

There’s just one problem – it’s a bit hard to remember it. That’s why, if you’re going to need it often, add the following line to your .vimrc:

cmap w!! w !sudo tee % >/dev/null

This way, when you get to the same situation again, you can just type :w!! to save the file, even if you did not run Vim with sudo.


Over the years, Vim got a reputation that it’s really difficult to learn it. I’ve heard many times from guys who are convinced it will take them months to reach proficiency. That’s simply wrong.

That’s why I’ve started to write a book: Mastering Vim Quickly (from WTF to OMG in no time) which will teach you Vim the way I learned it – easily and quickly.

If you want to get updates and sample content from the book, leave me your email below, and I’ll make sure to keep you updated. You could also go to Mastering Vim Quickly page and check it out.


If you liked the post, we should get connected - follow me on Twitter

Why Vim – 21 Reasons to Learn Vim

 

After writing about the history of Vim, I wrote a post where I told you what you can expect from my upcoming book Mastering Vim Quickly.

Today, I will try to mention as many as possible reasons to learn Vim. So, if you’re a beginner with Vim and you’re not sure should you keep learning it, or if you only think about learning Vim, this post if for you.

This post is not going to cover awesome Vim features as reasons to learn Vim, so if this is what you expect, don’t lose your time on reading.

So, here is the list of reasons why to learn Vim:

  1. Vi is guaranteed to exist on all Unix systems and exists on most Linux ones as well. That kind of broad coverage makes learning it worth it – because, if you know Vim then you know Vi as well.

  2. Vim is free and has a vibrant community

  3. Vim works over telnet & SSH connections. There’s no better way to edit files remotely via terminal than using Vim. Sysadmins know this.

  4. Vim’s configuration is portable. You just need to copy a few directories and files, and that’s all.

  5. Vim compiles/has precompiled binaries for almost every OS out there. You will never have to post “I need a [some text editor] alternative for [some OS]” anymore. You’ll have Vim instead!

  6. It’s also usually very fast and lightweight even when editing huge files of source code. Seriously huge.

  7. If you’re a programmer who edits a lot of text, then it’s important to learn an serious text editor like Vim. Serious text editors are highly optimized to perform the kinds of tasks that you will be doing a lot. For example, adding the same bit of text to the end of every line is trivial in Vim, but ridiculously tiresome otherwise.

  8. If you plan to configure anything in a Unix based system likelihood is you going to be editing a fair few config files, therefore you’ll want to learn a terminal based editor, of which Vim is one of the best.

  9. Vim allows you to easily code without taking your hands off the keyboard.

  10. Your fingers very rarely have to leave the home row, which means you’ll be able to edit text very, very quickly.

  11. It starts up faster than any IDE or text editor I’ve used, and it has many powerful features.

  12. It helps you focus on the coding process itself, you won’t be using the mouse at all to deal with it, that’ll save you a lot of time when you’re just writing code.

  13. It’s fun! Editing text is like a game to me now. I sincerely enjoy it – which is pretty ridiculous, when you think about it.

  14. If you’re working remotely, there is no choice to use Eclipse or similar GTK based IDE. You can do everything with Vim: source control, SQL, debug, compile, browsing – really fast browsing even over 1 GB source code. Visual Studio or Eclipse couldn’t handle all of this.

  15. There are a lot of good plugins out there which add a lot of functionality.

  16. Vim is a really good tool once you familiarize yourself with it.

  17. It has a reputation for the quality and the completeness of its docs. That’s true. Vim is thoroughly documented. You will find most of the  answers to your questions in its help system as soon as you will manage to stick the proper keywords in your help queries.

  18. You might want to learn Vim because many people think it is cool.

  19. You don’t have to press Ctrl or Alt all the time in order to run commands or shortcuts.

  20. It’s much quicker to use vi for a sudo edit: $ sudo vi. For example, if you run text editor 100 times a day, with typing sudo emacs, you’ll have to make 300 keypresses more! Okay, this is a joke :) chill out emacs guys (and yes, I know that there’s something like alias :) )

  21. Vim is addictive. You will find yourself wishing you could use Vim commands in all your computing, and cursing whenever you can’t. For example, I use Google Chrome with Vimium extension, so I surf the Internet without a mouse or touchpad. How cool is that!

On the end, have have to add that Vim’s usefulness depends on what you’re working with. If you’re an Java/C#/etc developer, you’ll most probably be more comfortable with an IDE.

But if you work with Python, Perl, Ruby, C, web, etc. or you’re a system admin/devops, Vim is the way to go. Vim is fast, powerful and does everything you need.


Over the years, Vim got a reputation that it’s really difficult to learn it. I’ve heard many times from guys who are convinced it will take them months to reach proficiency. That’s simply wrong.

That’s why I’ve started to write a book: Mastering Vim Quickly (from WTF to OMG in no time) which will teach you Vim the way I learned it – easily and quickly.

If you want to get updates and sample content from the book, leave me your email below, and I’ll make sure to keep you updated. You could also go to Mastering Vim Quickly page and check it out.

[mc4wp_form]


If you liked the post, we should get connected - follow me on Twitter