Editors note: This is the third in a series of introductory posts on vim, the almighty text editor. If you’re new to vim then this post won’t make any sense, so definitely start with the first one which you can find here.
So, vim is vim is vim. Vim is awesome. It’s a super fast, lightweight text editor. It has four main modes. The most important of which is normal mode which is normal and is used for:
In the last posts we covered the bare bones basics of navigating your text file in normal mode.
Now it’s time to dive in and start mucking around with our document.
Learning the bare bones basics of editing text in vim is not simply a matter of
- this is editing in normal mode
- this is editing in insert mode
because when it comes to editing, there is a little bit of cross collaboration between these modes that requires a bit of an understanding of both.
So for this post I’m going to introduce insert mode, then get into the wonder of editing in vim in the next post.
By the end of this post you should be able to get into and out of insert mode in the simplest ways… that’s pretty much it actually. But by then you will be at least as good, or better, at editing text in vim as in many other basic text editors.
You should also by the end of the next post be starting to really feel a bit more confident with vim, you will be able to get in, get out, move around and do some basic editing maneuvers. Which is literally what a text editor is supposed to do, after all.
You’re rambling again Geoff… get on with it.
Insert mode is not normal
Insert mode is very self-explanatory, especially once you know what normal mode is.
Insert mode is the mode where you insert text. This is what you’ll do in insert mode 99.99% of the time. There are a few other things you can do in insert mode which to be honest almost feel beyond the scope of a super basic introduction like this one, but I’ll go over a couple of them before we’re done… because I’m a legend.
So we’re going to pull out a text file or some other plain textish type file that you want to make changes to. If you copy pasted the text I gave you from my last post then use that.
Whip open that document in vim in whatever way you know how to so that you’re looking at your document in normal mode.
Obviously the first thing we need to do is get into insert mode. Turns out there are a number of ways to do this, all with their own uses and benefits.
For now I’d just like you to press the key called ‘i’.
If you look at the bottom left of your buffer now you should see this:
— INSERT —
That means you’re no longer in normal mode, you’re in insert mode (yay).
If you’ve wanted to but managed to resist the temptation up to this point, then now would be a perfectly acceptable time for a quick button mash. Go on, do something crazy.
Oh yeah. Let the freedom wash over you.
Congratulations, if you never used vim before reading my tutorials, then you just made your first official change to a text document, using vim. It probably also feels nice to have your keys doing what you think they should. It won’t be long before that feeling goes away, and it becomes much nicer to be in normal mode.
Now, you would think that ‘i’ stands for ‘insert’, which I guess it probably does. However it’s not the only, and maybe not even the most useful way of entering insert mode. If ‘i’ does stand for ‘insert’ then it must be because when you press the key called ‘i’, it enters insert mode with your cursor in the exact current position it’s in. So without any fancy footwork, you’re literally just entering insert mode.
And really the only reason I said any of that is to help cement this command deep into your long-term memory right from the get-go.
To get into insert mode in the simplest and most intuitive way, you press ‘i’.
Now you need to get out of insert mode, because it’s not normal to be there. It’s normal to be in normal mode.
To do that you simply need to press
You’re now back in normal mode.
Remember the picture of the ADM keyboard from my previous post… remember where the <ESC> key was? Yep. Now look where it is on your keyboard.
Don’t worry, it’s not so bad. It’s right up in the top corner, so even though it seems a world away from your fingers, it’s pretty easy to just throw out a ring finger.
It’s not even the only option for getting back to normal mode. Pressing and holding
will both work.
Turns out there are even cleverer ways than this.
But for now that’s it.
Get into insert mode – i
Edit text – Pretty much the way you would expect
Get out of insert mode – <ESC>
If I felt like being lazy, I could just about leave it there and move back on to normal mode, and editing and stuff.
But vim is wonderful, and I wouldn’t be capturing that wonder if I left you there. So we’ll go through the other most ordinary ways of entering insert mode from normal mode.
press the key called ‘i’.
Now press the key called ‘a’.
press <ESC> again.
See the difference?
It’s a subtle, but profound difference.
For example, from normal mode, jump to the end of the current word your cursor is on (you press ‘e’, in case you’d forgotten).
Now, suppose you want to write some text after this word. If you press ‘i’ from here, it’ll enter insert mode with your cursor before the last letter, which is pretty annoying if you ask me.
If you wanted to avoid that you would have to ‘l’ after you ‘e’, and then ‘i’ to get into insert mode. Simple.
So in sequence it would be
That actually doesn’t sound too bad, but ‘a’ cuts out the middle man.
This will get you to the end of the word and then open insert mode after the current word which is super useful. It might only cut a fraction of a second of the timing of the keystrokes but, more importantly, it reduces the cognitive load on your brain.
Having said that, you can also move to the beginning of the next word and press ‘i’ like so
So there’s a balance, and again, more evidence that vim has all the bases covered. But there’s a quirky reason that I personally like ‘a’ just a little better.
Move to somewhere in the middle of a line of text and get into insert mode. Notice what happens to the cursor when you press <ESC> from insert mode. Where does it go?
Repeatedly press ‘a’ then <ESC> a couple of times quickly. Now press ‘i’ then <ESC> a couple times quickly. Does the latter just feel a little more unwieldy to you? It does me.
So this is personal opinion, but to me ‘a’ and <ESC> are just a little more in harmony with each other than ‘i’ and <ESC>.
But there’s more!
Remember how I said there’s a difference between upper and lower cased commands?
Sometimes they’re very different, like ‘j’ and ‘J’ (don’t press ‘J’, don’t do it).
But very often the differences are related. Often the uppercase variant is an extension of the lowercase variant in some way. Uppercase commands are often a ‘stronger’ form of the lowercase command.
So press ‘a’ again.
Then <ESC> back to normal mode again.
Now press ‘A’.
Now <ESC> back to normal mode again.
Now press ‘i’.
Now <ESC> back to normal mode again.
Now press ‘I’.
Now <ESC> back to normal mode again.
Hopefully you see that in a crude way, ‘a’ tends to push your cursor forwards, and ‘i’ tends to go more in the backwards direction. Technically that’s because ‘a’ stands for ‘append’.
All four of these commands are very useful when they’re needed.
You’ve seen me emphasize the <ESC> a lot so far in this post, that’s deliberate. It’s time to become one with your <ESC> key. Do whatever it takes to make pressing <ESC> an automatic reflex. Nothing bad will ever happen in Vim by pressing the <ESC> key, only good things, or nothing. Rumor has it some pro vimmers are so pedantic about this it’s their practise to mash it a bunch of times every single time until they hear it beep, because it’s that much apart of vimming. Pressing at almost anytime in vim, in any mode, will return you to the safety of normal mode.
When in doubt <ESC> out.
These are the four simplest entry points for getting into insert mode, but they are not the only ones. I’ll talk about a couple more soon. For now though enjoy the fact that we’re making some real progress.
You can now get in and out of vim, move around, and get in and out of insert mode, which by extension means you can now edit your document in vim in much the same way that you can in the majority of other text editors.
Now as promised, before we go back to normal mode, here’s a couple of cool commands that can save you some time in insert mode (every time you ever use a command in vim prefixed by <ctrl> you need to press and hold <ctrl> then press the other key):
<ctrl> w – Delete the word behind the cursor
<ctrl> u – Delete to the beginning of the line before the cursor
The above two are about the least complicated ‘insert commands’ that you will come across. They are also quite handy if you’re in the zone and don’t feel like going back to normal mode to do things. As someone who writes prose in vim I very often type a word, then immediately regret it, and almost as often do the same with an entire line, so it helps to just quickly be able to cut them away with no fuss. I shouldn’t have to also point out how much quicker this is in vim than it is in Word.
So there you have it. You now know what you need to know to use vim as efficiently as any basic text editor. From here on, everything we learn is what really puts vim ahead of the pack in terms of speed and functionality.
In the next post we’ll be going back to normal mode to look at some of the innumerable editing options you have at your disposal and we’ll talking about how to understand the command structure in a way that doesn’t require having to ‘memorize’ every individual command.
Thanks so much again for reading, I hope you enjoyed it. Let me know what you thought in the comments and make sure you subscribe/follow to get notified every time a new post comes out (never more than once per week).