Little Known Vim Command – Join

As the author of Mastering Vim Quickly I sometimes get various Vim related questions from my readers.

Much more often I get emails from my subscribers at where I share free Vim tips.

Although this command is very simple, my experience showed that a lot of people are not aware of it. So it’s time to try to change that :)

Yesterday I was contacted by one of my readers – and as always, I try to answer my readers as soon as possible.

mastering vim quickly


As you can see from the screenshot, he needed to get from this

postscore(font, number, color,

to this:

postscore(font, number, color,
lang, size)

He knew that there must be a better way, than going to Insert mode, and using arrow keys and Backspace. And there is.

The solution to this is simple – put your cursor anywhere on the line where lang, is, and hit J (Shift+j). That’s a join command. For more info see :help J

So, whenever you want to merge two lines into one, position the cursor anywhere on the first line, and press J to join the two lines. Simple as that :)

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

My Story: How to Accidentally Self-Publish a Book


This is the first part of a post which will guide you through the entire process of writing and self publishing a book for the first time.

It’s about my book called Mastering Vim Quickly: From WTF to OMG in no time.

The idea

Vim is a very powerful text editor, used mainly by sysadmins, software developers and similar IT people. It’s known by its steep learning curve, because it’s different than majority of all editors.

This post shows that more than a million people looked for the answer on how to exit Vim. If you need to Google or go to StackOverflow in order to figure out how to exit a text editor – there must be something specific about it, right? :)

So sometimes in 2014, I wanted to improve my Vim skills. I was already a Vim user for more than a couple of years, but I knew there’s much more I can learn.

I did a quick research on the best books on Vim, and I’ve bought a few. Soon enough, I was pretty disappointed. All of these books had too much information which I didn’t really need or want to know.

They were also way too long. I really didn’t have the wish or time to spend reading a 300 page book, just to learn some new Vim features and concepts.

About that time I came up with idea to write my own book – a book from which I would like to learn Vim.

It should be a short read, not more than 150 pages. It should contain the most important Vim concepts, and only the most important features.

If you never used Vim, you should know that this is very important, because Vim has really a lot of features. And you can never stop learning it.

I wrote a few blog posts about the idea for the book. I also left a small sign up form below these posts, for people to sign up for the updates, in case they were interested in the book.

I also wrote a very simple Twitter bot, which would read RSS feeds of some blogs and websites writing often on Vim, and from time to time tweet out those posts with the original links, of course. So all it was really doing was sharing the Vim links consistently.

I did follow a couple of hundred Vim users on Twitter as well, and I let the little script to run on my VPS.

And about that time, I was looking for a new job…

The problem

It was December 2014.
I just moved to Berlin, Germany.

I had a few job offers, and I was about to join one startup.
Those days, I finally decided to realize my idea – write a book on Vim.

However, unexpectedly things went wrong with my work permit.

Long story short: My homeland is not part of the European Union. The only way to get a work permit was – I had to have a university degree recognized in Germany.

Now, I did have the Bachelor university degree. But, it wasn’t recognized in Germany. That’s why, I couldn’t get any job in Germany. I had to come back to Serbia.

Obviously, I had to completely cancel all the work on Mastering Vim Quickly. I closed my freelancing business. I’ve fired all the clients. And I had to go back to university again.

I spent a whole year on completing my Master degree in Computer Science. The one which is recognized in Germany. It took a lot of hard work, but I did it!

I got back to Berlin.

It was a long way. It took a lot of time and effort, but it was worth of it.
I’ve found a dream job in an awesome startup.

Surprise #1

I got back to Berlin, ready to start with new job. The previous year was very though. My whole life, for most of the days, was dedicated to getting that Master degree.

I already kinda gave up from writing a book on Vim. I already had different ideas and priorities, and my motivation wasn’t as high as before.

However, I remembered and decided to check what’s going on with my bot and old Twitter account. I totally forgot about these. I finally had some time to clean up my VPS and remove this bot and some other obsolete stuff.

To my great surprise, I discovered that my Twitter account @MasteringVim had around 4000 followers! How the hell that happened? Turns out, bot was tweeting regularly.

Most of the tweets were Vim articles from other websites. From time to time, it would tweet my few of my articles about the idea for the book “Mastering Vim Quickly”.

Lots of these Vim articles were good. Some of them were tweeted more than a few times. Sometimes, articles would be crappy. But, the it seems like the consistency was the magic trick.

A small group of about a hundred followers I had (before I left Berlin) started to retweet those, and bit by bit, my account started to reach bigger audience.

About a year after, 4K followers, Wow!

I quickly checked my free Mailchimp account (after more than a year!) to see if anyone actually signed up for the updates on my book. You can guess – there was much more people that I expected – around 400 of them!

Crap. Now I felt responsible to at least inform the subscribers why they didn’t hear a word from me for more than a year. I went through the list of emails.

There were people I knew, and people I “knew” from some online communities. Still, the majority were total strangers.

So after a bit of thinking, I’ve decided – I’ll write them an email. I’ll tell them what happened in my private life. I’ll inform them that I didn’t even start writing Mastering Vim Quickly.

And I thought, for sure most of them won’t be interested in the book after a year. Some marketing gooroos told me that email list which you don’t contact often is “cold” and pretty much non-responsive.

Here’s what I had in mind: I’ll write them the truth, there’s no book yet. They’ll get disappointed, or they’ll unsubscribe and/or send hate emails – because they won’t even remember me and my idea.

That will let me give up of writing the book without guilty conscience that I let anyone down.

Surprise #2

Something totally unexpected happened. I’ve sent that email, as planned. What happened is that I got a bunch of email replies, congratulating me on my persistence to get the CS degree and get back to Germany.

Many of these subscribers were still interested in the book, and more than a few already wanted to pre-order it, and asked me how to pay for it! WOW!

I had no choice really. Giving up now would be pretty bad. I knew many of these people, and they were expecting on me to deliver what I promised – a great book on Vim.

So that’s how I decided again, I’m going to write Mastering Vim Quickly!

The first thing I did was to stop the Twitter bot. That time I started tweeting personally, and I still do today. Via @MasteringVim I share the best tips on Vim. It currently has around 20K followers.

Surprise #3

While I was writing the book, few things happened, which I would never expect.

First, I’ve got contacted to cooperate with one big publishing house. We tried talking, but we had different visions, so no cooperation happened.

Then, I got invited to a meetup to talk about a book. A book, which still doesn’t exist. I actually did it. :)

Lastly, I got invited to a conference to talk about Vim – and my book wasn’t even written yet. I had to say no, and focus on the actual writing.

What’s next

In the next post (part 2 of this story) I’ll share my experience on the writing process itself, the book “launch” and the process of publishing a paperback on Amazon.


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

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:

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

Your First Vim Session

The major barrier to any skill acquisition isn’t intellectual, but emotional. The same goes for learning Vim. That’s why, as one of the first chapters in my book Mastering Vim Quickly, I teach how to do something very cool.

It’s very motivational, especially for someone who is new to the Vim world. You see you can use some advanced concepts, even if you don’t really understand them yet. Right from the start. Many of my readers who were beginners told me this chapter gave them a huge motivation boost.

Here’s a quote from one of them: “Your tag line to the title of your work MVQ, “From WTF to OMG in no time” is well earned, because it did exactly that to me.  The example that comes to mind is quite simple but it was the first time I remember saying to myself “How marvellous”,  Page 19 Task: Change work pick from line 2 to last line (line 6) to s. The fact that moving down a line in visual mode did not select the rest of the line above was a revelation!  This simple action and explanation made me look forward to more secrets explained further in the book.  Thank you.”

This post will present you the chapter I’m talking about – Your First Vim Session. At the end of it, there’s also a video which goes along with it.

To run Vim, open your terminal, and type: vim

This will start Vim, as always, in Normal mode. To start with inserting some text, you need to switch to Insert mode. You do this by simply pressing i. Feel free to type some text, and then try moving your cursor around with arrow keys, so you can gain familiarity.

Now, when you’re done with typing, let’s save this text in a file. To do so, you need to get back to Normal mode. You do this always by pressing the Esc key.

If you have never used Vim before, you’re probably a bit confused. What in the world is a mode? I’ll explain it to you very soon. For now, just try to understand that Vim is different than any other text editor you’ve used. The concept of modes is very simple: you want to insert text—then enter the Insert mode first. You’re done with typing? Exit Insert mode.

Now, whenever you want to type a command, you need to type :. This is how every command starts. The command to save the file and exit Vim is :wq. Letter w comes from write, letter q comes from quit. So, type :wq test.txt and press Enter. Voilà, you’ve just created a file called test.txt.

If you want to open a file you’ve just created, just type in your terminal: vim test.txt. Press key i and add some more text to your file. But, you don’t want to save it this time.
In order to close the file without saving it, and exit Vim, you’ll need to get back to Normal mode (press Esc) and type :q!.

Simple as that. Of course, there are several more ways to open and close files in Vim, but this is enough for your fist Vim session.

Now, let’s do something cool. You won’t understand everything now, but you’ll have a complete understanding of this example when you read the entire book.

If you use and git in the command line, you’ve probably had to squash some of your commits at times. This example will show you how quickly you can do it with Vim, even though you’re just at the very start.

In case you don’t know what I’m talking about, never mind—just copy the snippet below to Vim and try out the example.

Info: You’re squashing your commits, here’s the list of them:

pick e08be68 Add initial Rexfile
pick 5b4143f Fixes
pick 855be75 fix
pick d7a5285 Initial work
pick 59e82a2 add more stuff
pick 34cfc9c Fixes

Task: Change word pick from line 2 to last line (line 6) to s.


1. Open Vim. Press i to enter Insert mode. Then copy the text from above. Paste the text in Vim, using the shortcut for pasting in your terminal.

2. Now press Esc to get back to Normal mode. Use arrows to place your cursor on the beginning of the second line.

3. Press Ctrl-v. Then press right-arrow three times. You’ll notice that you just selected word pick in second line.

4. Now press down-arrow four times. You’ll notice that you’ve selected all words pick starting from second to last line.

5. While this block of words is selected, press c. That’s a command for changing text.

6. All words in your selection will be deleted, and cursor placed on second line. Mode will change to Insert. Now just type s. That’s what we want to replace word pick with.

7. Last final step – press Esc. And voila! You did it!

Here’s how the result should look like:

pick e08be68 Add initial Rexfile
s 5b4143f Fixes
s 855be75 fix
s d7a5285 Initial work
s 59e82a2 add more stuff
s 34cfc9c Fixes

This might look complicated, but trust me, it’s very easy. Keep reading, and soon you’ll understand what actually happened in this example.


If you’re interested in Vim, definitely take a look at my book Mastering Vim Quickly: From WTF to OMG in no time.

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