Beginning Vim (and using Vim in other text editors)

Today, I want to write a bit about Vim, using Vim keybindings. and why you should use switch to Vim keybindings if you’re not already using them.

Me, as well as many others started hearing about this arcane text editor when we first started learning programming. What is it with this 30 years old editor, that makes it so intriguing?

First of all, about all Linux servers have Vim installed. This means if you were to SSH into a server, you would probably use Vim to open and edit files containing text, be it regular text files, source code, or what have you.

A lot of people like the Vim editor because it’s very hackable. You can build a customized fully-fledged IDE just by installing a few Vim-addons.

Now, I don’t use Vim as my main text editor anymore. Like so many others, I made a switch to Visual Studio Code. And I can tell you, I would’ve never made the switch if it wasn’t for Vim keybindings.

That’s one of the dangers by learning Vim keybindings. They’re just so good, you never want to go back to the wrong way of editing text.

Now, you’ve probably heard that Vim is incredible hard. That’s a lie. You can do some pretty amazing, very advanced editing in Vim. This isn’t something you usually need to use all the time. The regular coding, day to day work doesn’t require incredible advanced Vim-skills. It will just be a bit different than what you’re used to.

If you are running Linux or Mac OS, you can fire up Vim by typing Vim in the terminal. If you’re on Windows, I recommend you install gVim from www.vim.org

Table of contents

Vim modes

Doing a quick google search shows us what we need to go over first. There are plenty of memes about quitting a Vim session out there. If you’re unfamiliar about how Vim works, it’s understandable. If you are familiar with it, you wouldn’t want it any other way. (Looking at you, Google Docs!)

Let’s try opening Vim. Open up a terminal window, and enter vim

~ % vim
Image for post
Image for post

The famous Vim start-page.

One of the main reasons people are actually googling how to exit Vim, is because they’re not familiar to Vim’s modular approach.
When you first start up Vim, you are in Normal Mode. Vim har multiple different modes, where the most important ones are Normal Mode, Insert Mode and Visual Mode.

Normal mode overview

Normal mode is used for navigating through the file and doing edits. If you try to enter text in Normal Mode, nothing will be printed out.

When programming, most of the time you spend in the text editor is spent editing and looking trough the code, not actually writing code. This is one of the reasons it makes perfect sense to have Normal Mode as Vim’s default mode.
You return to Normal Mode by pressing Esc on your keyboard.

Insert Mode overview

Insert mode is used for inserting text into the document. It will behave as a regular text program. Which means you are also able to use backspace to delete text. If you have to delete more than a couple of characters, you should probably do that in Normal mode. We’ll get back to that shortly!
You enter Insert Mode by pressing i on your keyboard.

Visual Mode overview

Visual mode is used to highlight text, the same way you would do when you drag your mouse over a piece of text to copy it.
You enter Visual Mode by pressing shift+v or v on your keyboard.

Vim: Create new file, write , save, and close

Image for post
Image for post

Let’s see whats going on here.
From the terminal, I’m first opening Vim. I’m appending a -clean. This is just so that I’m able to show you Vim without any addons. My Vim has quite a few addons, which may take the focus off of the actual editor.

We can change from Normal Mode to Insert Mode by pressing the i key. Once pressed, Vim shows us that we’re currently in Insert Mode by displaying INSERT in the lower left corner.

Once we’re in Insert Mode, we can write text as usual. When we’re done writing text, we exit Insert Mode by pressing the ESC-key on our keyboard. Vim shows us that we’re currently in Normal Mode, by not displaying anything in the lower left corner.

Now comes an interesting part. To save the text as a new file, we use the write command. We type :w followed by whatever we want our file to be named. Once saved, we may exit Vim by using the quit command, :q

Note that if you try to exit Vim without saving your file, you’ll run into an error:

Image for post
Image for post

If you don’t want to save your work, just follow the instructions, and add a ! to override Vim’s safety net. :q!
Note that you can save and exit Vim by concatenating the commands:

:qw <filename>

or omit the filename if you’ve edited an already existing file

Moving around in Vim

Image for post
Image for post

You might think — What’s so special about this? We’re just using the arrow keys to move around. Well, not quite.

When using Vim, we should not use the arrow keys. We use the keys h, j, k and l instead.

  • h to move left
  • j to move down
  • k to move up
  • l to move right

Now, this is some weird keybindings! And I totally agree. This is probably one of the weirdest things about Vim. These keybindings are actually quite common in the terminal world. If you were to use less or more to look inside a file, you could use the same keybindings to scroll through it.

Image for post
Image for post

Here, we can see a couple of other commands for moving around. Let’s go through them:

e

When pressing the e key, we move to the end of the word, or the end of the next word, depending on where the cursor is.

b
When pressing the b key, we move to the start of the word, or the start of the previous word, depending on where the cursor is.

$
A dollar sign will move you all the way to the end of the line.

0
A zero will move you back, to the start of the line.

Number before a move command
If you write a number before the move command, Vim will execute the command the number of times you’ve entered.
If you type 10e, Vim will move 10 words ahead. If you type 3j, Vim will move down 3 lines.

gg
This command will move you to the start of the document.

Shift+g
This command will move you to the last line of the document.

number + shift+G
Do you know which line number you’re moving to? Say you need to do some work at line 56. just type out 56, and press shift+G. Easy!

Let’s edit something

Image for post
Image for post

Editing is done in Normal Mode. We only enter Insert Mode to do small bursts of text insertions. There are an incredible amount of different commands for different edits. This topic goes so deep, entire books have been written on this subject. Let’s go through the ones I use the most throughout the day.

u
I wanted to show you this command first. This is equivalent to ctrl+z on Linux and Windows, and cmd+z on mac. Why it’s important is pretty self explanatory.

i
To enter Insert Mode, and start writing, we press i.

ctrl+r
Redo. Goes hand in hand with undo, or u.

daw
Stands for ‘delete a word’, and will delete the entire word.

ciw / caw
Stands for ‘change word’. the command deletes the word, and put you directly in Insert Mode. The difference between these commands, is that ciw will leave a space, while caw will will jump to the first character of the next word.
Try it yourself. Write out two words. Jump back to the first word, and try out both commands. Which one do you like best? If you ask me, ciw is the way to go. It might be one of the most useful commands in Vim.

cw
This command deletes a word from the cursor, which means everything before the cursor will be left alone.

CONSIDER THIS TEXT:
const newNumber;
CURSOR ON N:
const newNumber;
^
USE cw:
const new

As we can see from the example, the text got deleted at the cursor, and Vim put us in Insert Mode. as you may see, this makes for very quick text edits.

dd
This command will delete an entire line of text.

d$
This command will delete from the cursor, and to the end of the line.

d0
This command will delete from the cursor, and to the beginning of the line.

x
x will delete the character underneath the cursor.

di} di) di] di” di’
And so on, and so forth. These commands are very, very useful if you’re editing code. Think of it like this: ‘delete inside bracket’, ‘delete inside quotes’.

CONSIDER THIS MOCK VARIABLE
const variable = {...something, id: newId};
MOVE CURSOR SOMEWHERE INSIDE THE BRACKETS
const variable = {...something, id: newIid};
^
TYPE ci}
const variable = {};

The example above works with all types of brackets, single and double quotes. It is an amazing way to delete inside, and makes for very efficient text editing.

Image for post
Image for post

yy
y stands for yank. This command will duplicate a whole line.

p
paste (put). If you use it after yy, you paste the line you just duplicated. This command also works with deletion of words. So if you need to delete a word and paste it in another place, you delete it, move the cursor to where you want to paste it in, and press p.

yw / yiw
Works the same as deleting words, except these commands will copy them. You may use p to paste them again.

o
o will enter Insert Mode on a new line, underneath the cursor.

O
O will enter Insert Mode on a new line, above the cursor.

r
replaces the character underneath the cursor.

.
This command will repeat the last change you’ve made.

Remember, you can always add numbers to a command, to make Vim repeat it.

Visual Mode

Image for post
Image for post

Visual Mode will let you highlight text, the same way you often do with your mouse. What’s nice about Visual Mode, is that it can very easily copy or delete big sections of text.

As far as I’m concerned, there are two main ways of entering Visual Mode, and they work a bit different:

v
v will enter Visual Mode from where the cursor is, and let you highlight text relative to the cursors position when you entered Visual Mode.

Image for post
Image for post

Note that it says ‘Visual’ instead of ‘Insert’, while in Visual Mode.

Shift + v
This will highlight line by line. This is a command I’m using all the time.
It makes it very easy to select chunks of text.

Image for post
Image for post

Vim MISC

We are getting near the end of this little Vim keybinding tutorial. There are a couple of other commands that I use frequently, that doesn’t belong in any of the sections above.

Search for text
In normal mode, there is a search function pretty similar to the one you you’re used to from web browsers, and such. By using a slash followed by the word you’re looking for, you’re able to search through the entire document, jumping from a matched word, to the next matched word.
/word-to-find

Image for post
Image for post

as you can see in the snippet above, I’m searching for the word ‘line’.
We can use n to search for the next word, and shift+n to search backwards.

Replace all characters

Say you did a mistake. You’ve created a bad variable name, and you need to change it. However, you’ve used the variable name in 10 different places. It could potentially take some time to replace them all. Or, maybe there was an easy way to do this? Well, this is Vim. Of course there is an easy, yet arcane way to do it.

Image for post
Image for post

:s stands for substitute.

:s/old-word/new-word
This command will replace the old word, with the new word, on the line which the cursor is currently on.

:%s/old-word/new-word/g
Note the percent character. This command will replace every old word with the new word, throughout the entire document.

Vim in other text editors?

As I wrote a bit earlier in this post, I don’t use Vim text editor at the moment. I use Visual Studio Code. Before that, I used Atom. I’m lazy, and it’s a lot easier to use one of these big, new shiny toys than building and customizing my own. I can guarantee that if it wasn’t for Vim bindings, I wouldn’t have touched any of the new text editors. You see, after using Vim and Vim keybindings for a while, you will get so used to them, to a point where it’s nearly impossible not to use them.

You don’t need to set up Vim, if you don’t want to. Just enable Vim Mode in the text editor you’re currently using. All the commands I’ve showed you so far, works in all of them.

Closing thoughts

If you’ve read all the way to here, I’m pretty sure you will give these amazing keybindings a try. Or even have a shot at the real thing. There are so many plugins for Vim, you wouldn’t believe it. The text editor itself is also actively maintained. Not bad for a 30 years old piece of software!

If you’re touch typing (as you should!), you’ll notice that many of the commands are laid out very conveniently. This means you dont have to move your hands that much while editing text. You don’t need to use your mouse either. You can keep your fingers at the correct position. This can shave off some time you would have spent on moving your arm to your mouse, and moving your hand over to the arrow keys.

If you are serious about learning Vim, but find the standard commands hard to remember, why not learn it by playing a game? There is a game called Vim adventures, where.. Well, I won’t spoil anything. It’s pretty amazing. It does cost money for a premium version, but you can pick up quite a bit if you only want to do the free one. Check out https://vim-adventures.com/

There are an incredible amount of commands, and ways of combining commands. A lot more than what I’ve written about. Entire books have been written on the subject. This blog post barely scratches the surface of what’s possible with Vim commands. But to be honest, these are the commands I use the most, and I like to think about my self as a fairly efficient Vim user. If I need to create a special macro for some weird and clunky editing task, I can always google it.

I hope this have been informative and that you find it helpful

Until next time
Stephan Bakkelund Valois

The Startup

Medium's largest active publication, followed by +771K people. Follow to join our community.

Stephan Bakkelund Valois

Written by

I’m a Norwegian developer and a motorcycle enthusiast. I love building cool stuff, and playing with exciting technology.

The Startup

Medium's largest active publication, followed by +771K people. Follow to join our community.

Stephan Bakkelund Valois

Written by

I’m a Norwegian developer and a motorcycle enthusiast. I love building cool stuff, and playing with exciting technology.

The Startup

Medium's largest active publication, followed by +771K people. Follow to join our community.

Medium is an open platform where 170 million readers come to find insightful and dynamic thinking. Here, expert and undiscovered voices alike dive into the heart of any topic and bring new ideas to the surface. Learn more

Follow the writers, publications, and topics that matter to you, and you’ll see them on your homepage and in your inbox. Explore

If you have a story to tell, knowledge to share, or a perspective to offer — welcome home. It’s easy and free to post your thinking on any topic. Write on Medium

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store