Sharing Our Passion for Technology
& continuous learning
〈  Back to Blog

Vim in Ten Keys

Vim in Ten Keys

If you’re reading this, you’re curious about Vim. Maybe you’re a curious person. Maybe there’s some really annoying person on your team who will just not shut up about it, and you’re trying to placate them. Maybe you opened it by accident, can’t get out, and have accepted that this is your life now.

In any case, you’ve come to the right place.

I want you to do something for me. I want you to forget everything you know about Vim. Specifically I want you to forget about HJKL.

But how are you supposed to move around? Surely I’m not telling you to use the arrow keys, am I? We’ll get to that. For now just follow along and I’ll guide you through the wonderful world of Vim. Let’s start by opening it up. Simply type vim into your terminal, and you should be ready to go.

Key 1: i (insert)

First thing’s first. We should probably write something. We’re not going to get much done without writing something at some point.

Press i and you should be able write to your heart’s content.

Inserting text in Vim

Key 2: escape (get me outta here)

That was fun, wasn’t it? But what if we want to do something else? For right now just hit escape and then we’ll get into what just happened.

You see, Vim has several states that it can be in. We call these “modes”.

You’ve already seen two of these modes so far. Earlier when you hit i, you told Vim to change from normal to insert mode. Insert mode is where you can type text into your file.

The other mode you’ve seen is called normal mode. When you pressed escape, it told Vim to switch back to normal mode. Whereas insert mode was simple and works the way you probably expect, normal mode is a rather more complicated beast.

We’re going to spend the rest of this post talking about normal mode.

Key 3: w (word)

As we begin our exploration of normal mode, I don’t want you to think of Vim as a text editor, that’s what insert mode is for. Vim is an interactive environment for issuing commands to manipulate text. Mastering these commands is important, not for speed, but for eloquence. These commands transform a text file from what it currently is into what you have in your head.

When you’re operating at the edge of your mental ability, when you can’t afford to context switch even a little or lose your train of thought, that is when Vim is at its most useful.

Let’s start with a simple command. w moves the cursor to the next word. You can hit it as many times as you like, it will even wrap around to the next line.

Moving one word to the right That saved, like, 4 keystrokes

You can even type a number and w to move that many words.

Moving four words to the right 10 keystrokes... *mind blown*

Key 4: d (delete)

With this key, we get the final piece of the puzzle. Vim commands generally take the following form:

{command} {motion}

d is a command, and d {motion} will delete the text from the cursor to where the cursor would end up if we preformed the motion.

A little confusing? Let’s see an example with the motion we’ve already seen. The w key.

d tells Vim to delete something. w tells Vim to move to the next word. Combined, you delete the current word.

Deleting a word

Simple enough, right?

I know it doesn’t seem like much yet, but we’re done. This is Vim. Combining different commands and motions is the essence of using Vim.

There are some fancy tricks you can pull, but from day to day, this is it. So take a breath and join me in the next section when you’re ready.

Key 5: f (find)

Alright, time for another motion. We can use d{count}w to delete as many words as we want, but sitting there counting words is annoying. We could ballpark it, but I can’t estimate to save my life. We don’t want this, we want something easier to eyeball.

The f key can take care of this. f finds the next instance of a character and places your cursor there. Just type f and the character you want to move to. As you might have guessed, it also works with d. So, let’s say you need to remove a few parameters from a function signature. All we need are three keystrokes: df)

Deleting everything to the next closing parenthesis

Key 6: shift (changing gears)

Ok, so we can move rightwards in two different ways now, which is nice. But what happens if you need to move left?

That’s where the shift key comes into play. shift works with a bunch of different keys, it usually makes them “bigger” (more on this later), or makes them work backwards. f just so happens to be one of the keys that shift makes work backwards. F (shift + f) searches leftwards for a character.

Moving rightwards to the next d bi-directional movement, what is this madness?

That’s all well and good, but what does it mean to make something in Vim “bigger”. We’ve already seen a couple of commands that behave this way. Let’s take a look at them.

The d key, as we’ve seen, deletes things. D (shift + d) deletes everything from the cursor to the end of the line.

We learned that w moves you rightwards one word, however Vim’s definition of a word is rather strict. It will skip over alphanumeric characters and underscores. That’s it. W (shift + w) on the other hand will move until it finds a space or some other whitespace character.

Keys 7 & 8: y/p (yank and put)

The next two come as a pair. In this section, we’re going to look at a couple commands that, some days, feel like they make up the majority of development. That’s right, it’s copy and paste time.

y is a command just like d. It copies some amount of text. In the world of Vim this is called “yanking” the text.

You can yank a word with yw. You can tell Vim to yank text until it finds the letter t with yft. You can yank an entire line with yy.

Was any of that surprising? No? Good. There’s not much new here. One thing to note is that, due to historical reasons, Y (shift + y) will yank the entire line. This is annoyingly inconsistent, and often one of the first things devs re-map. But, really, it’s basically the same as using d.

The takeaway here is that Vim doesn’t have thousands upon thousands of commands for every situation. That’s much too complicated to ever truly master. Vim is simpler than that. You have commands that tell Vim what to do, and motions that tell Vim where to do it. You do this over and over until you stop thinking about it. You do this until the keys burn their way into your muscle memory. Eventually, you look at some text, think about what you want to do and it just happens. You mold the text file like a sculptor molds clay.

I can’t let you leave this section without something new, however. How about this? Some commands are even simpler than y and d. They don’t even need a motion. p is one of these commands.

p will put the text after the cursor. P (shift + p) will put the text before the cursor. This also works with text you’ve deleted with d.

I told you we were done with the complicated stuff.

Key 9: / (slashing through)

A common situation while editing text is to be in one part of a text file and want to be in a completely different part of the file.

The chief problem, of course, is that there’s a bunch of stuff in the way.

Like a hiker going through a dense forest, what you need is a machete to cut through the underbrush, so you can go straight to where you want to be.

Enter /.

/ is sort of like f on steroids. / lets you enter arbitrary text and jump to where it appears. Simple, yet powerful.

/ lets you jump exactly where you need to go, do what you need to do and be gone like some sort of special forces operative on a secret mission. It is the swiss army knife of movement that works when nothing else will.

Let’s stop for a second and look at what / brings to the table.

  • It takes you anywhere you want to go
  • It searches for text
  • It’s right next to where your pinky sits on the home row
  • It’s a lover
  • It’s a fighter
  • It also lets you search backwards in a file if you combine it with shift (?)

If this was all / did, it would be extremely useful, but we’re not done yet. No, we are not done.

/ is a motion, which means it works seamlessly with all of the other Vim commands. Remember our old friends d and y? They slot right in here to allow you to effortlessly and precisely wrangle the exact piece of text you’re looking for. Technically / is the only motion you need. The rest are just there for convenience.

This is how learning Vim tends to go, it’s weird at first, but every piece you pick up feeds back in and makes everything you already know even more powerful.

Deleting everything to the next closing curly brace Didn't need it anyway

And that, ladies and genlemen, is the power of /.

Key 10: [ (blocks)

Ok, so remember how I said there really wasn’t anything else to Vim besides commands and motions? I didn’t lie, but Vim might have somewhat of a broader notion of motion than you were thinking.

There are too many motions involving [ to cover exhaustively in this post, but there are a couple that come up frequently.

Simplest one first, { (shift + [) will move you up in the file to the last blank line. Really nice for zipping around a function definition.

Next one requires just a bit more explanation [[ will take you to the previous code section. What is a code section? Any line that starts with { or }. This is mostly useful for zipping around top level functions or class definitions.

The last one is something most people probably wouldn’t consider a motion, but Vim does. When you’re working with delimeters, you can reference the contents of the delimeters.

Wait, what?

I think an example is in order here.

Have you ever worked with array literals in JavaScript and realized you screwed everything up? I have. The first step is always to delete everything inside the [s.

Vim has a simple way to do that, di[

You see, i[ is a motion that tells Vim to preform a command on everything inside a matching [...] set. Vim understands quite a few delimeters: [...], {...}, <...>, (...), single quotes, double quotes, backticks, and html/xml tags.

Alright, I could keep going on, but I think that’s enough for one post. It’s time for you to go out there and get some practice. You’ll do great.

… You’re still here? Oh, right. Quitting. Typing :q and then enter will get you out of vim.



Still hungry for more vim? Check out our post on Vim splits.

〈  Back to Blog