binarydreams.xyz

How to learn Vim fast!

Foreword

I have been an enthusiastic Vim user for a little while now, I started to really use it as my main editor in the second half of 2020. When I first started learning it, I took a lot of quick notes about new things I had learned in a document on my desktop. This tutorial is my attempt at turning these notes and the order in which I learned Vim into a simple and fast to get through introduction to this wonderful editor. I can’t promise that it will be particularly useful to you though…

Preparation:

Open a new terminal window and tile your screen in such a way, that this page takes up the left side, and your terminal the right side of the screen. Then use the terminal and navigate to a folder of your choice and execute the following command:

printf "Hello wrld!\nMy name is " > vimfast.txt

(If this doesn’t work, simply use nano or a similar editor to make a new file with those lines.)

Now we can start editing by typing vim vimfast.txt.

The basics of navigation

Keys in this section: HJKL, WBE

The simplest form of navigation in Vim are the HJKL keys. They work like arrow keys, but have the advantage of being in the middle of they keyboard, easily accessible. H and L move your cursor left and right, respectively. K will bring your cursor up one line, while J will move it down. Try it out now on the two lines in your vimfast.txt.

Now simply moving the cursor one character at a time is a bit slow, but Vim has a very convenient way to move entire words at a time too. Pressing w will jump one word forwards, and b brings you a word back. Try it! It even works across line breaks. Notice how w and b always bring you to the start of a word though? If you want to jump to the end of the word you are currently in, press e. With these three keys alone, you can move way more efficiently than you with just the arrow keys, but also more precise than with the mouse.

Vim has a special understanding of words. When pressing lowercase w and b you will notice that it considers the exclamation point in the first line as a word. But by pressing uppercase W and B you can tell Vim to always jump to the next word after a whitespace, and consider the exclamation point part of the word.

One more trick before we move on: Vim lets you combine lots of actions with numbers, to execute them a fixed number of times. Navigate to the start of the second line and press 3w to jump three words ahead. Cool, right? This works with any number of course, provided there are enough words in the file.

Let’s write some text

Keys in this section: IAO

You might’ve noticed by now, that there’s a typo in the first line. “wrld” is missing an “o”. Let’s fix that. Up until now we have used the keyboard to navigate in our file, but not type text. This is because we were in Vims “Normal Mode”. To write actual text, we need to switch to “Insert Mode”. There are multiple ways to accomplish this:

Navigate to the “r” in the first line and press i. You will see “Insert” in the lower left corner of your editor window and when you press letters on your keyboard, you start writing text. You can now put an “o” into “world” to correct the mistake. If you are ready to exit Insert Mode, just press ESCAPE. It will place you back in Normal Mode and you can navigate around as usual. Insert Mode will always start putting text before your current cursor position, when you press i. That’s why I told you to move to “r” and not “w”. If you want to start writing text after your cursor position, press a. It stands for “append”, and does exactly what it sounds like. Once in Insert Mode, it doesn’t matter if you used i or a to get there, the editing works just the same. Tip: To move the cursor without leaving Insert Mode, use the standard arrow keys!

Of course simply inserting some letters into a line isn’t enough. What if you already wrote some text and now you want to add more in the same line? This is where we can use uppercase letters again. Navigate to the middle of the second line in our text file. Press A and you will start appending text to the end of the line! You can put your name there if you want. A similar action can be executed with I too. Leave Insert Mode with ESCAPE and then press I. Your cursor will be transported to the first word of the line and you will be placed in Insert Mode once more. Very useful when writing code and you need to put a semicolon at the end of a line, or comment out some code by placing two slashes at the start.

If you want to add an entirely new line to your file, you could simply append to the end of the current line and hit ENTER. This is a bit clunky though. Instead we use the o key. Press o and a new line will be created below the one your cursor was in, placing you into Insert Mode immediately. Just like with our other keys earlier, we can modify the behavior of o by capitalizing it. Press O to insert a line above the one you’re cursor is currently in.

Now that you know the basics of adding text, take a moment to add a couple lines to your file with explanations of the commands you just learned. That way you will not only remember them better, but you will also have some more text to play around with for the next sections.

Interlude: Save and quit

Keys in this section: colon

We’ve made some basic changes to our file now, so how do we actually save them? Simple. Make sure you’re in Normal Mode, then press : on your keyboard. This will show the colon you typed in the lower left corner of your screen and you can start typing Vim commands. The two most important commands are “w” and “q”. Typing a “w” after the colon and hitting ENTER will write all the changes we made to our file to the disk. Then we can safely quit the editor with “:q”. We can also combine both commands into “:wq”. To quit without saving changes, use “:q!”. To follow the rest of this guide, open the file again.

Deleting or changing letters, words and lines

Keys in this section: XRDC, 0$

Vim allows us to delete text as we would in other editors while writing text in Insert Mode. When we just want to delete a letter without replacing it or delete multiple words at once, we quickly realize that Insert Mode isn’t ideal for that. In other text editors we might use the mouse to mark the text we want to delete, but Vim is a keyboard-only editor and has some much faster alternatives for us.

Let’s start with individual characters. To delete a single character, press x. That’s it. You will stay in Normal Mode, but the character under/after your cursor is gone. Just like earlier, we can modify this behavior by using X instead, which will delete the character before our cursor. We can replace a single character by pressing r. No need for Insert Mode. Both x and r can of course be combined with numbers as well, to delete or replace multiple characters at once. Just type 5x to delete the next 5 letters for example. Uppercase R will put us in Replace Mode. Here we can keep replacing letters as long as we want, even using the arrow keys for navigation, and exit anytime with ESCAPE, just like in Insert Mode. I personally never use R, because of another command I’ll explain in the near future, but it’s nice to know nonetheless.

Spamming X a bunch of times isn’t much better than using Insert Mode though. What about deleting entire words? This is where d comes in. Pressing d on it’s own won’t do anything at first, but we can combine it with some other key to delete what we want. Here are the common ones:

  • dd deletes the current line
  • dw deletes the next word and db the previous one
  • d3w deletes the next 3 words, works with any number > 0
  • D deletes everything from your cursor to the end of the line
  • d0 does the opposite of D, try it!

So how does d0 work? As we can see from dw and db, combining d with some movement key will delete accordingly. The same goes for 0. Pressing it will move your cursor to the beginning of the current line, and therefore d deletes until the beginning of the line. Jumping to the end of the line is done with $, making these two keys similar to I and A but without placing us into Insert Mode. Here is where we can see a nice example of Vim consistency by the way. D will delete until the end of the line, but d$ will actually do the same thing. I use D more often, since it’s easier to type, but this kind of logical behavior is what makes Vim really satisfying to use, once you get to know it well. We can also accomplish the same thing as d3w with 3dw. The former deletes 3 words, and the latter deletes the next word 3 times. Consistency!

Finally let’s take a look at one of my favorite keys in Vim: c. This key does basically the same thing as d but also places you into Insert Mode after the deletion. This is already a very useful action when you want to change one or two words in a piece of code, but it can be combined with more motions to become better than anything you could do with a mouse. For c the same combinations apply as the ones for d listed above, but you can also do something like:

  • ci” while in “quotes” will allow you to change all the text in the quotation marks
  • ci( or ci) while in (some brackets) will let you change all the text in the brackets
  • and you’ve probably guessed by now that ci[ and ci{ works too

The “change in” combination is great for editing code and I use it very frequently. Just remember that it will really delete all the text within the quotes/brackets, so don’t use it if you only wanna change 3 out of 10 words or so.

Non-essential advanced navigation

Keys in this section: (){}, G, HLM, Z, FT, /N

In this section I just want to mention a bunch of movemement commands that I use from time to time, but that aren’t important enough for their own section. Don’t worry too much if you can’t keep all of them in your memory immediately, maybe you will forget about some of them and rediscover their use later, or reread this guide to remember.

  • ( and ) will move you one sentence backwards or forwards, not super useful for editing code, but sometimes nice when writing a longer piece of normal text like this
  • { and } will move you one paragraph backwards or forwards, very similar to the normal brackets
  • gg moves you to the start of the current file
  • G moves you to the end of the current file
  • H puts your cursor at the top of the screen, L on the bottom and M in the middle, not super useful for me personally, but worth mentioning
  • z works similarly, but moves your view instead of the cursor. zt moves the line with your cursors to the top of your view, zb to the bottom and zz to the center. Try it out on a large file to understand it

Finally there are f and t. These two allow you to search for a letter in the current line, starting from your cursor position. f and a character will move your cursor to the next instance of that character in the current line. t does almost the same, but positions your cursor in front of the specified character, not on it directly. This is less useful for general navigation in my opinion, but of course these commands can also be used in combination with d or c for example. dt( will delete everything between the cursor and the next opening bracket. cfw will delete from your cursor up until and including the next “w” and then place you in Insert Mode. Additionally, F and T will search for a character in the line, but backwards from your cursor.

If you want to search for a character or combination of characters in the whole file, press / and start typing your search term. Then press ENTER and your cursor will jump to the next instance of that phrase, if it exists. To jump to the next instance, press n and to go to the previous one in the file, press N. Again, Vim logic and consistency.

Copying and pasting - Vim style

Keys in this section: YP

Copying and pasting is very important for writing code, because nobody wants to type the same lines multiple times and waste time. Luckily copying and pasting in Vim is extremely intuitive. To copy text we use the “yank” command y and to paste we use “put” p. Yank works basically like delete and change do too. yy yanks a line, yw yanks a word, yi( yanks everything inside the brackets, super simple. Paste is similarly simple. Press p to put what you yanked. If you yanked part of a line, put will just append it after your cursor. If you yanked a whole line or multiple ones (with 3yy for example) then put will insert them below the line with your cursor. To insert them above instead just press P.

What about cutting though? Well, you basically already know how to cut and paste. Everytime you delete something with d, x or even c the content you deleted will be put in the register, Vims clipboard. This is great because it means that you don’t need to learn another command for cutting, but it also means you should be careful about your order of operations. Yanking a line and then deleting a different one will overwrite the yanking you did. Instead put immediately after yanking, so you don’t “lose” the contents you yankend and have to get them again.

Also keep in mind that Vims clipboard and your system clipboard are not the same thing usually. The way to copy and paste with your system clipboard can differs depending on where/how you use Vim, so just search for the right way online.

Replacing the mouse with Visual Mode

Keys in this section: V

Visual Mode allows us to highlight text similar to how we would highlight text with a mouse in a different editor. There are multiple kinds of Visual Mode in Vim, but I will only cover the standard Visual Mode and Visual Line here. Let’s start with the default: v will put you in Visual Mode, and moving your cursor will highlight text. This works basically like clicking and holding left click on a mouse, however you can use all your usual ways of movement, like W or f et cetera.

Visual Line is called with V and works on a line by line basis, as the name suggest. You aren’t highlighting individual characters anymore, but entire lines. Moving left and right inside a line doesn’t have an effect on the selection, only up and down does.

So what’s the point of Visual Mode? While in this mode you can execute many commands that you know from normal mode, but applied to the whole highlighted selection. d will delete everything you highlighted, y will yank all of it and c changes it all. You can also highlight text and then put with p to replace the highlighted text.

Undo, redo, repeat

Keys in this section: UR.

So, what if you made a mistake and put 5 lines in the wrong place? Or worse yet, you deleted 5 lines and want to get them back? You just undo your last action(s) of course. Undoing is simple in Vim. Press u to undo the last action, and CONTROL + r to redo it. That’s basically it. There is also another cool feature that fits in this category and that is repeat. Pressing . will perform whatever you did last again. So if you pressed I// to insert two slashes at the beginning of the current line, you could exit Insert Mode, move to the line below and press . to apply the same action again. I often do this exact combo to comment out multiple lines.

One thing to note here: Deleting a single character with x and rewriting multiple lines with 3cc are both considered a single action in Vim. Don’t be confused when undoing or repeating.

Short intro to macro magic

Keys in this section: Q

Macros are really cool and not that difficult to use, but I want to keep this section short and only cover the basics. To record a macro in Vim you press q followed by another key. The second key will be the one that the macro gets assigned to, but don’t worry, it won’t trigger everytime you press that key. Once you’ve selected the key to assign the macro to, you can start actually recording it. During the recording process, all your actions will still affect the file you’re editing, so you can make sure your macro will really work. Once you’re done recording, simply press q again and the macro will be saved.

To play the macro you have to press @ and the key you assigned the macro to. It will now execute the keys you pressed earlier again, once. But since this is Vim, you can of course specify a number before the @ to execute the macro more than once.

As an example we can think about commenting out lines in Java again. First we press qw to start recording a macro and then I//ESCAPEj. This sequence will move the cursor in front of the first word in the line, insert two forward slashes to mark it as a comment, then exit the Insert Mode and move down one line. If we execute this command 4 times now with 4@w it will comment out 4 more lines. Pretty neat!

What’s next?

This is the end of this guide for now, but it doesn’t have to be the end of your learning curve with Vim. I only scratched the surface of what is possible in Vim and there are many more ways to optimize and speed up your editing. The commands included in this guide are only here to serve as a good base, so that you can start actually editing with Vim quickly and learn more features as you go along.

If you are interested in more customization, you should start editing your own Vim configuration file, if you want more features like autocompletion for code or different colorschemes, then take a look at Vim pluginmanagers. And if editing in the terminal isn’t for you, then you can either install a GUI for Vim or add a Vim keybinds extension to your preferred text editor or IDE. I also recommend checking out Neovim, because it provides better support for plugins among many other things.