Move around like a Vi person

Fang Jin
5 min readAug 18, 2022

I thought I would never write about Vi, because it’s just an editor. However as I start to use it more and more professionally, for instance, all my work is done using Vi these days, I find Vi a bit more interesting than before. Medium doesn’t support Vi, such a shame.

So what is Vi? I said it wrong, it’s not an editor, instead it’s a way to enter characters. You can find it used in quite a few editors. Of course Vi itself is an editor. So a bit of confusing, but not really. Let’s try to get a bit more understanding by playing with it, shall we?

> vi

If you have a terminal, you can just kick it off by typing a vi. If you don’t have any Linux background, you just have to Google around to get a Vi running.

Moving around

For the first day in the series, we’re only going to cover the basic moving-around part, pretending you already have a paragraph below:

Code with syntax highlighting

The basic commands to move around are:

To move to the next character to the right, you use the key l , left with h, up with k and down with j. It all happens four keys are from the middle row of a keyboard, right below your right hand. Here’s an example of cursor position after using h.

You might be wondering why to use a letter key to move around instead of just using the regular arrow key. Isn’t all human using the arrow to move around? Well not all human apparently ;)

There’s a long story to it, but the short story is that this Vi method was designed for a keyboard before we have agree-upon arrow keys. Yeah this was designed back at the old time.

Of course there’s a better story, that is the reason why we are still using it: your hands can travel less while moving around. Without it, you need to find the arrow key, or even find the mouse to do a click. Considering how many times you perform this action, the travel time just can’t be ignored when combined.

Another way to look at this is that, Vi is created for someone who really loves typing keys. Just like someone really likes custom keyboards, there’s nothing that cool about each keyboard, but they just love having and showing them off. It’s just like that. For Vi people, we just love typing keys, especially when a key can pull a rabbit out of a hat sometimes.

Moving around quickly

If you think typing one key at a time is too slow, we could do the following to speed it up:

The number before h tells to repeat h five times. Cool? Maybe not, the thing is that you need to know how many lines you are moving down.

What if you don’t know the number? Well, then you need to figure that out, ex. playing with 2h, 4h, whatever, the point here is that, if you happen to know the distance between where you are and where you are going to, it’s kinda of neat that you can just add a number to the command.

You might wonder at this point, are all Vi people moving around like that? Is this boring and tedious? Well yes or no. Just to fill your curiosity, most time no, we move around with other commands.

If you haven’t noticed , we actually type commands, such as 5h. Which means, you literally can have millions of commands. The most efficient commands use one letter, such as h. In that case, we still have 26 choices (or more with other characters), not to mention, we can have capitalized letters. So overall we can have around 100 quick choices under our belt.

Moving around super quick

So show me how to move around super quick like a pro. Before I do that, one more thing, Vi is about the precision, if you really know the direction, you can get there very fast. But if you don’t, you can get lost very fast as well. So fasten your seat belt while playing with the new toys:

G  (shift+g)
go to the end of the file
gg
go to the start of the file
CTRL+f
page down
CTRL+b
page up
w
move a word to the right
b
move a word to the left
0
move to the start of the line
$
move to the end of the line
H
move to the start of the screen
L
move to the end of the screen
M
move to the middle of the screen

The cheat sheet above is to get you started comfortably inside the Vi. The sheet isn’t complete one for two reasons, a) there’re a lot more to it, remember I mentioned one hundred commands, I wasn’t kidding; b) you can mix each command with a number, so if you want to do two words to the right, you can do 2w.

One thing that gets me up in the morning is that, I can discover a new command to move around, or to do some cool editing tricks. It’s like Youtube guys like to play with their camera and edit their videos, we like type the same key over and over, and confidently know there can be something new to happen.

If you can master the above basic commands, you should be able to at least move around without getting stuck so that you don’t hate to pick Vi at the first place ;) This is important, since we all know how many times we quit doing something and come back to pick it up again. We don’t want to do that. Let’s try changing the way we do things, move it slowly and steady without too much randomly jumping all over the place.

Summary

That’s it for today, glad you had some fun reading it. It’s not a lot, but can be a lot to some of us new to Vi. All in all it does change the way we do things, so please give yourself sometime to digest these, especially considering you don’t know exactly what benefits that can bring it to you in the future.

Keep in mind practice is the key. If you can pick up a Vi, and start to move around for five minutes a day. By the end of the week, you should be able to move like a Vi person.

--

--

Fang Jin
Fang Jin

Written by Fang Jin

Front-end Engineer, book author of “Designing React Hooks the Right Way” and "Think in Recursion"

No responses yet