Vim has a plethora of modes for all sorts of different tasks – 6 basic modes and a few advanced ones only of knowledge to those people. For the most part, what any beginner in Vim would bother about is to type text, save the file, and then exit Vim. However, Vim is not the regular text you might have used before – a simple text modification in Vim without the basic knowledge of it could lead to sky-high levels of frustration.
Now don’t get me wrong here! Vim is not that bad simply because it does things the hard way. Once you know what you gotta do, you’ll be able to manipulate your text like never before. You might as well be able to give your friends an eyegasm of confusion. This tutorial will walk you through the most common ways you can navigate inside of Vim Insert Mode, the place where you insert all the text you wish. Let’s jump right into it, shall we?
Warning: Emacs keybindings won’t work in Vim Insert Mode
For those of you switching from Emacs and wondering whether the navigation keybindings in Emacs will also work in Vim, you’re outta luck. But if you’re just utterly desperate to have those keybindings in Vim, don’t worry. You can assign them using Vim mappings. Why not check out some tutorials available online?
Enter Insert Mode
Okay so you’re fired up Vim for the first time, and are wondering how you’ll insert text. No, wait. You must already be trying to press keys in hope of text to appear on screen. Trust me. You won’t make progress until you press just the keys that are assigned to enter the Vim Insert Mode. The most common way you’d do this is by pressing “i” on your keyboard (i for Insert). If you’ve successfully done that, you should see a message appear on the bottom of the screen that says “– INSERT –“. This is how it should look like:
You will now be able to insert text like you casually would anywhere else. This isn’t the only method of inserting text, though. There are at least more than 10 ways you could. But you don’t need to know it all for now. I’ll tell you a few common ones, just in case you’re curious.
The closest brother to the i to insert text is “a” (a for append). If you notice very, very carefully, you should be able to see their difference. If you press i, your cursor will start inserting text just before the character your cursor is placed on.
In comparison, if you press a, you’re cursor will start inserting text after the character your cursor is placed on, therefore, appending text:
There is one other way I’d like to show for now. It is by pressing “o” on your keyboard. Now what this essentially does is instead of starting to insert text from your current position, it’ll first create a newline after your current line and then enter Insert Mode. Also, note that if there’s already another line after the current line, it’ll be sent down further to and a whole new line will be created for you to insert text. The following example should be able to demonstrate that properly:
Besides pressing o (which is lowercase) you can use an uppercase o as well (by pressing shift + o). This will insert a newline before your current line:
Leave the Insert Mode
This is the simplest thing you could ever do in Vim. If you’re done with inserting the text you want, you can leave the Insert Mode and come back to the Vim Normal Mode (which is where you first land on when you start Vim), by pressing the Esc key on your keyboard. You should notice that the “— INSERT –” message is no longer visible at the bottom, this tells you that you’re no longer in Insert Mode.
Ctrl + O to leave Insert Mode for once
I came to learn about this a lot later myself, but it turns out to be extremely handy, and yet, time saving! That is, if you press Ctrl + o while you’re in Insert Mode, you’ll be sent to the Vim Normal Mode, and you’ll be able to execute a single command from there. Once you enter the command, Vim will automatically take you back to the Insert Mode.
Now how is this beneficial? Let’s say, you want to delete the current line by pressing dd on your keyboard in Normal Mode. To do that, you’ll first have to press Esc to leave Insert Mode, then press dd to delete the current line, and then once again press i to enter Insert Mode. As dd is counted as a single command, you could save yourself a few keystrokes by pressing Ctrl + o in Insert Mode instead.
How cool is that, you think?
Navigating with arrow keys
This is pretty much the same as in elsewhere. While in Insert Mode, or basically in any mode in Vim, you can press the arrow keys on your keyboard to navigate between lines and columns. Pressing the left and right arrow keys will allow you to navigate left and right between letters, while pressing up and down arrow keys will allow you to navigate between the lines. Simple.
Navigating between words
This trick is quite handy, as is also pretty much a universal command now like the previous one. That is, if you press Ctrl + left and right arrow keys, you’ll be able to navigate by words instead of single letters. This could be a whole lot faster when navigating through lots of words at a time:
You must be wondering why the editor interface seems to have changed. That’s because I had to switch to a more recent version of Vim, with my own added colors. Older versions of Vim, that are below Vim 8.0, had several issues using ctrl with other keys. As the terminal protocols were not bypassed entirely in Vim back then, these commands did not use to work before. But starting from Vim 8, a lot has changed and you can now finally use these to navigate in Vim Insert Mode.
Moving to the start and end of lines
Moving to the beginning and the end of the line is the same as you’d do elsewhere, which is by pressing the HOME and END buttons on your keyboard. Unfortunately, some of the keyboards don’t have these dedicated keys. You can use keymappings to assign a different key to do the job for you, if you wish.
Moving to the start and end of file
If you combine ctrl with the above HOME and END keys, you’ll be able to go all the way to the end or the beginning of the entire file. In my testing though, there seemed to be a few mishaps. Just like the previous one where ctrl + arrow keys had problems in Vim versions below 8.0, this one does too. You would probably want to avoid this trick, and rather press capital G to move to the end of the file, or gg to move to the beginning of the file in Vim Normal Mode instead. Might as well do so by using Ctrl + o as mentioned previously for a quicker switch.
This pretty much covers all the basics of navigation in Vim Insert Mode. There’s a lot more to the different modes in Vim, than you may ever think. There are Vim users who have been using it for more than 20 years but still discover something new everyday. This is because Vim does not give you a fixed list of things to do. It gives you the barebones and allows you to create combos that give rise to famous Vim commands everyone else might start using. There have been times when people discovered such combos, posted them on the web, and everyone suddenly started using them. It’s not just the combos, however. There are many prefixed Vim commands that you might have never known existed.
That’s how deep Vim is.