Vim Editing in Normal Mode: Learning to speak Vim

Editor’s note: This is the fourth in a series of posts on vim (Emacs’ arch nemesis). If you’re new here, or new to vim then you’ll definitely want to start with the first post which you can find here.

 

So here we are, back where we belong. Good old normal mode (press <ESC> one more time, just to be sure). And now we are ready to learn some cool tricks. 

So far my previous posts have given us what we need to be basically as productive as we normally would in any ordinary text editor. From here on in what your learning is the stuff that really separates vim (and vi too really) from other text editors.

Sure, moving around a text file blazingly fast is a fantastic vim trick. But being able to move around blazingly fast and make innumerable small and large edits as you go without having to point and click your mouse cursor where you want it every time, or pressing and holding the arrow keys like a cave man, is a crucial element of the zen of vim.

All the plugins and extensibility and all the bells and whistles are all great. But I think people choose vim because at its heart, it’s fast (I chose it because it’s fun, and because I wanted to feel like a slick programmer when really I was just a noob writing basic R scripts).

Using vim is not like using an ordinary text editor, it’s an experience. You don’t edit text in vim, you communicate with vim and vim does all the work for you. All you have to do is learn the language that vim speaks, and it’s not that hard. This post we’re going to learn a bit about that language.

However this series is still only a bare bones basics series. Enough to just get you started, give you a taste, and leave you wanting more; all without you wanting to throw your monitor out the window.

Now as much as I would love to, there really is just way too many editing commands to go through one by one and explain them the way I have done for most so far. Even the bare bones basics would take forever. So we’re going to start moving a bit quicker from here, and I’ll leave it up to you to take it at your own pace and spend time creating the muscle memory in your fingers to automate each of these commands. Mastering navigation, and editing in normal mode in vim is a personal journey, and can only be done well by dedicating time to do it yourself.

I recommend taking a good chunk of time for the first few days. Resolve to deliberately practise and memorize about 5 – 10 new commands or command combinations per day. Then once you’re feeling pretty confident with the basics, work on learning 1-2 a day. You’ll learn a lot of commands very quickly if you can commit to this.

Editing the vim way

So get back into vim in your usual way and open up whatever text file you’ve been working on.

The following are the crux of vim editing. They are the most basic editing commands that you will use in vim.

y – ‘yank’ which in ordinary computer world is like ‘copy’

d – ‘delete’ which means to delete.

Delete is actually not like delete in ordinary computer world, it’s more like ‘cut’, because almighty vim actually saves the deleted text into a ‘register’. I’ll do a whole post soon on registers, for now just know that vim has registers, which are like clipboards in ordinary computer world, only way more badass. What this means for you is that you can delete text and then…

p – ‘put’ which in ordinary computer world this is like ‘paste’. So ‘put’ will paste things you have ‘yanked’ and things you have ‘deleted’.

Each time you ‘yank’ or ‘delete’ it will overwrite the contents of the register with the new text (just like you would expect in ordinary computer world).

Man I hope that’s not too confusing…

So these are the most ordinary editing commands and they will be quite familiar to you.

However I haven’t yet shown you how to select the text you want to ‘yank’, ‘delete’ or ‘put’. I will very shortly.

Even more inserting

OK, so if the options I gave in the last post for entering into insert mode weren’t enough, here’s a couple more. The reason I didn’t include these in the previous post is because they make a change to your buffer, so I consider them editing commands, whereas the ones in the previous post just get you into insert mode.

c – ‘change’ can also be thought of as ‘cut and replace’. It will delete whatever text you specify and automatically enter into insert mode.

o – ‘open’. This sucker is one of my favourites. What this does is insert a new line under the line the cursor is currently sitting on and will place the cursor on that line and enter insert mode. So much packed into literally one keystroke, I just love it.

O – (Uppercase o, not the number zero) will do the same as ‘o’, except that it puts a new line above the cursor.

The next couple of commands are more special.

x – Pressing the key called ‘x’ simply deletes the character under the cursor, no more, no less. This is a really neat little key which I find very helpful and use it a lot, because I make a lot of typos.

rX – r means ‘replace’. So X can be any letter, number or almost any symbol and ‘r’ will replace the character under the cursor with X. Also very useful for fixing typos. It works by first pressing ‘r’ then pressing the key you want to swap in (it does not work by pressing and holding ‘r’).

. – (your full stop, or period, key) This is one of the most beautiful things that vim has to offer. Pressing the humble period key quite simply repeats the last change you made.

That might not sound so useful at first, but believe me it is very, very useful. For both programming and writing prose, this little sucker is vim heroine. As the saying goes DNRYAAC (Do Not Repeat Yourself At All Costs). I’m not even going to defend it. Just see how long you go before you realise you have to just do the same thing a couple of times in a row…

The above three commands will typically work in isolation, unlike yank, delete and put which require further information.

The zen of vim

If I haven’t said it enough, this is just a drop in the ocean… thousands people, there are thousands upon thousands of 2-3 keystrokes and combinations that will automatically do a ton of menial, ordinary small changes with lighting efficiency. But even that is not really what makes vim so special.

As others before me have stated, vim commands are like their own little language. They have an organic nature to them. This is the zen of vim.

Once you start understanding that language and it’s structure, then the work of having to memorize those commands is dramatically reduced. When you learn to speak vim, you become one with it. And all it really takes is to just get your head around the basic ideas outlined in this post.

Learning to speak vim

I’ve already explained that very often uppercase letters are related to lowercase, very often they are some form of extension of the function of the lower case command. I’ve also briefly touched on the fact that you can prefix commands with a number, so that vim executes that command the number of times you ask.

Now it’s time to get right into the unlimited functionality of key combinations.

Combining commands in vim is, as others before me have stated, like forming a sentence. Stringing commands together becomes a matter of asking vim ‘do a thing, to some stuff, the number of times and in the particular way that I want.’

Vimmers often describe edit commands as ‘verbs’ (technically they’re called ‘operators’), and motion commands as ‘objects’.

We know that vim thinks of ‘w’ as ‘word’ and thinks of ‘l’ as ‘one to the right’ and ‘h’ as ‘one to the left’, these (and all the rest) are the motion commands.

We also now know that vim thinks of ‘y’ as ‘yank’, and ‘d’ as ‘delete’.

All of these commands can be combined in specific ways to achieve a specific outcome, and it would be silly to try to list them all one at a time. It will be much better to understand the structure of the command sequence and then work through and figure out what different combinations do yourself.

(for all combinations discussed below, each command is pressed one-after-another. It’s not a press and hold situation)

So the basic sequence is:

XYZ

where X is ‘number of times’, Y is ‘some action’ and Z is ‘some motion/object’.

Typically ‘motion/object’ commands will work on their own. ‘w’ moves to the next word, ‘e’ moves to the end of a word, ‘$’ moves to the end of the line, ‘^’ moves to the first character on the line etc.

But actions have to be directed towards something. So ‘y’ will only yank once it knows what it’s yanking, same with ‘d’. When you hit ‘y’, vim will patiently for you to tell it what to yank.

Same with numbers. If you just type a number, like ‘5’ and nothing else, vim will be all like “5 what mate?”.

So the motion commands (some of which we’ve already learnt) are the source of all your power. They get you around, and they direct the actions of your editing commands. They are the heart and soul of vim.

So because I don’t want to completely leave you in the lurch…

Here’s a couple of super common examples:

dw – delete from the cursor to the beginning of the next word (another way to think about it is – from the cursor to the end + any whitespace)

dl – delete the character to the right (which is actually the character under the cursor

de – delete from the cursor to the end of the word (no whitespace)

dd – delete the entire line

D – (uppercase ‘d’) delete from the cursor to the end of the line

The above five commands will also work with ‘c’. Using c will be identical to using ‘d’ except that it will automatically put you in insert mode after.

The above five commands will also work with ‘y’. Using y will ‘yank’ (copy) the desired text but will not delete it.

“Um, no Geoff, that’s wrong”

Yes sorry. Actually, uppercase – Y – will not copy the text from the cursor to the end of the line by default. Y actually works exactly the same as yy… I don’t know why. This is actually one of the very few kind of annoying things about vim, so in a later post I’ll show you how to fix it so that Y works the same as D and C.

From here the edits are endless. Just about any motion you can perform in vim, you can prefix with an edit command and it will perform that edit, on that motion/object (incase it’s not obvious, the ‘object’ is the text that the cursor passes over as it moves from its initial position, to its end point). You can prefix just about any of these edits with a number and it will perform that edit the number of times you specify. Once you become familiar with the motions, then the edits will follow naturally. This doesn’t take long to get the hang of. Here’s a couple more just to show you how versatile vim speak really is.

y$ – yank from the cursor position to the end of the line (hint hint)

3yw – yank 3 words

3yy – yank 3 lines

yG – yank from the cursor to the end of the document

3p – paste what you’ve just yanked or deleted three times

d^ – delete from the cursor position to the first character on the line

“ap – paste the contents of register ‘a’ (don’t freak out, we’re getting there)

P – paste what you’ve just yanked or deleted in front of the cursor (instead of after it)… because vim really just has every base covered… it really does.

Once the motion commands become more intuitive (which is easier than you might think), editing with surgical precision becomes a cinch.

 

Visual mode

That could be enough for this post, but visual mode is not much more to add-on. Visual mode is accessed by pressing ‘v’ (you might have guessed that there are other ways to do this too). In visual mode your motion commands work as they do in normal mode, except that they will highlight the object text. So visual mode is just a more user-friendly way of making edits. It works very similar to ‘highlight’ in ordinary computer world, so you’ll be very familiar with it. Once you’ve selected the text you want you can then perform an edit, like ‘y’ or ‘d’ and it will perform the desired operation on the highlighted text.

Give it some time, give yourself a chance

Hopefully by now you’re starting to get a bit more comfortable in vim, and learning these edits will not be a huge leap forward from moving around and using insert mode, but when you’re first getting your head around them it can seem like a lot to remember.

Just remember that anything worth doing takes a little time, otherwise it wouldn’t be worth it.

Hopefully this isn’t too overwhelming. Give yourself an hour or two of punching away at your keyboard and you will pick it up quicker than you think.

By now you should be really getting your head around the basics of vim editing and understanding the potential power of vim. Everything we’ve learned in this post is done in normal mode (unless otherwise stated).

Normal mode is where you normally are, because whether you’re a programmer, a scientist who does a bit of programming, or a writer, you do not just sit down and punch out text without stopping. You write a bit, then you sit and think, then you edit, and think some more, then edit some more. You cut, copy, paste and duplicate.

Most importantly, no matter what you do, if you’re a digital knowledge worker, you think, a lot. Vim helps you to do this. When you learn to speak vim the way you speak your native tongue, you no longer have to ‘think’ about what your fingers are doing, anymore than you have to think about walking, or breathing, freeing up all your mental energy for what you’re working on. When you need to do some proper thinking for a particular edit in vim, then it will generally be something that will do a lot of work at once.

If you take what I’ve taught you here and go further with it, you’ll become a better, more productive editor than you will in most other editors and IDE’s. From here on out I’m giving you the cream on the vim cake. The next few posts are pure vim bling.

  • vimrc files
  • registers
  • macro recording
  • plugins
  • the leader key

… all will be revealed.

So that’s it for this post. If you enjoyed it then make sure you follow/subscribe to get notified of new posts (never more than once per week), and if you’re interested to learn more about this blog and why I started it then check out my pinned post and my about page.

Thanks so much for reading.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s