I'm currently writing a book to build your own Mouseless Development Environment, where I explain how to combine powerful tools to improve and customize your development worklow.
"Vim is not for me!"
I heard this sentence many, many times, when my colleagues dare to look at my screen. I'm doing everything in Vim, if it involves writing or coding.
Of course, I explain to them that learning the very basics of Vim can be very beneficial:
- They could run Vim everywhere they want.
- They could customize it like crazy, to match their way of working.
- They could edit easily files on remote systems or in docker containers when only Vi (the ancestor of Vim) or Vim are available.
- Many CLI use Vim-like key bindings. Learning the very basics of Vim can help you navigate in many other CLI application, like Less for example.
- Vim can edit very large text files without slowing down. Log files, anyone? In general, Vim is crazy fast.
- Everything they learn in Vim won't change from version to version, contrary to the majority of code editors or IDEs. How many times did I screamed to the sky, because the huge preference menu in PHPStorm changed, again?
- They could learn a new and really fun way for creating and editing any content. This is the main reason why I use Vim so much. It's basically the gamification of typing.
Still, they often don't even try to understand what Vim is about. How can somebody judge without seriously trying?
I can't blame them: I had exactly the same bias. However, when I tried to learn to use Vim, when I tried to understand how it works (not only learning two shortcuts randomly), I fall in love.
Today, I would simply like to share with you how I learned the very basics of Vim quickly:
- First, we will see that good typing techniques are essential if you want to unleash its full power. Heck, without even using Vim, rock solid typing techniques are very beneficial for any developer out there.
- Second, we will speak about the different Vim modes. This is one of the main reason why Vim is so unique, and so powerful.
- Third, I will give you the basic Vim shortcuts (keystrokes) for you to be efficient as quickly as possible, and some tips to remember them easily. Don't be afraid: they are very logical and, therefore, easy to remember.
After that, you can speak about Vim with experience and confidence, even if it's for saying: "naaaah, Vim is not for me!".
I won't ask you to replace your IDE with Vim from one day to another. Simply, try to use it to edit some configuration or other text files. Practicing what you will learn in this article is the key for you to really understand how Vim works and why it's so popular, even decades after its creation.
Now that we clarified some key points, let's dive into the wonderful world of Vim!
Prerequisites: The Power Is In Your Fingers
When I decided to learn Vim some years ago, I wanted to do it right. Vim allows you to forget your hands typing on your keyboard or grabbing your mouse, and let you really focus on the most important thing: the content you're creating.
The Mouse: Your False Best Friend
One of the advantage of Vim is to let your hands on the keyboard, without the constant need to grab your mouse.
I see you're afraid: your mouse is like your third hand! It's so useful and easy! Why would it be good not to use it?
I'm sorry to disappoint you. Your mouse is a bit like an implant a doctor would have put on your body at a very young age, telling your parent that it's the best device around there to do something on a computer. You like it because you're deeply used to it, for a very long time.
Ask yourself: why on earth, if the mouse was so perfect, your favorite IDE has 341324 keyboard shortcuts? Maybe because... using your keyboard is faster? Easier? More efficient? More comfortable?
Your mouse is not your best friend. It's just a friend. Your keyboard is the real brother-from-another-mother here. The power comes from it, and Vim is conceptually designed for you to harness and unleash this power.
That's why I deeply believe that before learning Vim, you need to learn the basic techniques how to use your keyboard as efficiently as possible.
The advantages are enormous, even without speaking about Vim:
- You will type faster and more accurately.
- The room for progression (speed and accuracy) will be enormous.
- You won't focus on your keyboard anymore. Not even a bit.
If you already use these techniques, that's great! You can directly go to the next chapter.
Efficient Typing: The Two Rules
We all agree that thinking, for a developer, is more important than knowing how to type. That said, it's still nice to feel in control of your tools, and obviously, the keyboard is one of the most important!
It's very fulfilling to see your typing improving day after days, months after months, even years after years. The room for progression is huge, even if it's pretty quick and easy to learn the basics.
The first rule you need to learn is placing your hand correctly. Since an image is sometimes better than a flow of words:
The keys a
,s
,d
,f
and j
,k
,l
,;
are called the row keys. They are the starting points for your hands. From there, you'll be able to grab any other key as efficiently as possible.
You'll notice that there are little bumps on the f
and j
keys on your keyboard: they are indicators for you to know where to put your indexes, without looking. When your indexes are at the good position, simply place the other fingers on the other row keys.
The second rule you need to train for is to try not looking at your keyboard, while you're typing. Of course, if you don't remember where a key is, look at it, but only after trying blindly where you think it is. Even if you have no idea where this damn key is. You will sometimes surprise yourself.
Before following these two rules, I was only typing with two fingers. It felt totally foreign to use these new hand placement. Now, I could not type differently: it's more efficient and more comfortable.
Don't be afraid! It's not hard to learn good typing techniques, I promise. It took me one to two weeks to really get it.
The First Week
When you decide to use the two rules I described above, you need to try to follow them all the time. We need 100% commitment here. If you surprise yourself using your bad technique again, which will happen, don't worry: simply come back to the good ones. This is part of the learning process, not a horrible failure.
The first three days are the most difficult. You will alternate between good and bad technique without even noticing it. You will do mistakes, and you will be slow.
Fortunately, at the end of the week, the amount of mistakes will decrease, and you will less and less need to watch your keyboards.
The Second Week
You will notice during the second week the amount of mistakes decreasing even more, and your need to watch your keyboard will disappear.
At the end of the week, you will see your typing speed already improving, compared to your old way of typing. The good feelings of reward will begin to please your brain.
Speed and Accuracy
During your two weeks of initial training, you shouldn't focus on speed or even accuracy. Just type, as much as you can, and don't worry about anything else yet, not even the mistakes you make.
After that, you can focus on speed and accuracy: how fast you can type, making as fewer mistakes as possible.
To train these good typing techniques, from the beginning of your learning experience to the end of your life, you can use typing software which can drastically help you.
Here's a list of my favorite ones:
Vim Or Neovim?
Now that we know the fundamentals for an efficient typing, let's install Neovim.
Neovim? What's this new weird thing, you might rightfully ask?
Neovim is a refactoring of Vim and it's compatible with everything Vim-related. I would definitely recommend using it, instead of the regular Vim, since it's optimized out of the box.
You can choose to use Vim as well, but bear in mind you might need to compile it with the options you want.
Here are the official links for both software:
Since Neovim and Vim are almost identical (with a different philosophy), I will continue to call these two software using the generic term Vim.
Vim's Configuration
One of the main advantage of Vim is its configuration. Everything is configurable. It's insane, I tell you. You can shape your editor according to your specific needs. You're in control, and it feels great.
For Neovim, the configuration file should be there: ~/.config/nvim/init.vim
.
Let's open it with your favorite editor (which might be soon replaced by Vim!) and let's add some basics things. I will explain later the purpose of it:
noremap <Up> <Nop>
noremap <Down> <Nop>
noremap <Left> <Nop>
noremap <Right> <Nop>
and
set clipboard+=unnamedplus
You can use in Vim's configuration a whole programming language, called Vimscript
, in order to shape your editor. When I was saying that Vim can adapt to your specific needs, I wasn't lying.
Don't worry though, you don't need to learn Vimscript
in order to use Vim.
Now, let's launch Vim and, without trying to do anything else, let's see the basics you need for you to get started.
First Step: The Modes
After launching Vim, you will see a welcome screen displaying the very basic commands you can use. This screen will disappear as soon as you begin to type some content.
Vim is not like many editors with GUI, where you can simply type on your keyboard and your content will magically appear on your screen. Try to type x
, for example: nothing seems to happen.
This is because Vim has modes: one mode is for inserting some content, another mode is for navigating and editing the same content. These modes are called respectively insert
mode and normal
mode.
This section is meant for you to understand the most important Vim modes. You will understand them better when you will try the main Vim keystrokes by yourself.
Normal Mode
You're currently in normal
mode, and, in this mode, you can't insert any content. However, you can use keystrokes to move where you want and edit what you want: inserting, changing or deleting a word, sentence or even paragraph. Without using your mouse once!
Think of it as a system of shortcuts which allows you to target exactly what you want to edit. Shortcuts are very efficient, even GUI editors or IDE allow you to use many of them, to improves your comfort and your efficiency.
The difference between your default GUI editor and Vim at the shortcut level is significant: the shortcuts in Vim make sense, most of the time. That's why it's so easy to learn Vim. As you will see, it's easy to remember these keystrokes since they follow a certain logic.
For example, the shortcut ctrl+shift+n
to find a file in your favorite GUI editor is difficult to remember because it's difficult to link what you know (opening a file) and what you want to learn (the keystroke).
We will come back to the normal
mode later, when we will learn the main Vim's keystrokes.
Insert Mode
Look at your cursor in Vim: it should normally be a square. Now, let's type our first normal
mode keystroke: i
. Bam! Magical things appear! Your cursor becomes a pipe, or a line. It's impressive, I know.
You can see as well that in the bottom left corner, the indicator --INSERT--
appeared. My friend, welcome to the insert
mode!
The keystroke i
means i
nsert. That's what I meant when I was saying that the shortcuts in Vim make sense. They are simple, too!
In insert
mode, you can finally type your content: go ahead, type anything you want. To come back to normal
mode and stop inserting, simply type esc
or ctrl-c
. The cursor becomes a square again, and the --INSERT--
indicator disappears.
That's how you work with Vim: juggling between normal
mode to place your cursor whenever you want and edit your content, and insert
mode in order to insert some new content!
Visual Mode
There is a third important mode in Vim you will use often: visual
mode. Its goal? Selecting your content. From there, you can modify, edit or copy your selection.
To enter visual
mode, you might guess it already, you need to type v
in normal
mode. You will see the indicator --VISUAL--
appearing at the bottom left corner of your Vim instance.
Second Step: The Basic Keystrokes
Now that we understand the general concept of Vim and its main modes, let's see the most important keystrokes in normal
mode you need to be aware of. I encourage you to try them in Vim as you read.
Many of these keystrokes, especially the movement keystrokes, work in visual
mode as well.
Writing Your Own Cheatsheet
You can find countless cheatsheets on the Internet which will give you as many Vim keystrokes as you want. When I was learning how to use Vim, I used one of them.
At the same time, I was writing as well my own cheatsheet.
Why? Personally, it helped me a lot memorizing these keystrokes. Writing is a powerful way to make the information yours.
Organize your cheatsheet as you like, and use whatever you want (paper, evernote, mindmaps...). For mine, I'm using Joplin, a free note taking system.
Bonus points if you write your cheatsheet... in Vim!
The rest of the article will give you the basics keystroke you need for using Vim. Be aware that Vim is full of subtleties which give you power and efficiency, depending on the context. It's a never ending learning process which makes this editor so interesting, so fun to use and highly rewarding!
As a friend of mine said once: "it's basically the gamification of writing!"
The ex command
You can compare the ex command as a menu on a GUI. A big and powerful menu.
If you type :
in normal
mode, your cursor will end up automatically at the bottom of Vim. From there, you can type any command.
Here are the most basic ones:
-
:help
to access Vim's help. This help is insanely complete. If you don't remember how to quit Vim for example, you can type:help quit
. -
:q
toq
uit Vim (:q!
to quit without saving. Imagine that you yell at your editor you want to quit, whatever the consequences!). -
:w
tow
rite (save the current file open). - You can even combine some ex command:
:wq
to write and quit. -
:e <path>
to edit a file. The path can be absolute or relative.
Searching
I wrote a whole article about searching in Vim, but, for now, the keystroke /
should be enough. If you use it, you will go again at the bottom of the Vim's instance. From there, type your search and press enter
.
You can go to the n
ext found occurrence by typing n
. To go to the previous one, simply use N
.
Forget the arrow keys, embrace hjkl
To be totally honest with you, this was the hardest part for me: not using the arrow keys to move your cursor.
As I said in the first part of the article, your fingers should be on the row keys. First, for your typing to improve, and second because the Vim's keystrokes you can use in normal
mode are especially designed for you to follow the good typing technique. In short, they are all around the row keys, to prevent your hands to move too much. Only your fingers should.
If you follow the typing techniques we saw above, using Vim normal
mode will be rewarding and efficient.
Now, try to reach the arrow keys from the row keys: yes, you need to move your hand! This is definitely not what we want.
That's why, instead of using the arrow keys, you should use the keys h
,j
,k
and l
to move respectively left, down, up and right.
It's difficult at first: you will try to use the arrow keys to move your cursor, and more than once. That's why we previously disabled them in the configuration!
How to remember what does h
, j
, k
and l
?
-
h
moves your cursor to the left, andl
moves it to the right. It makes sens, sinceh
is on the left of the sequencehjkl
, andl
is on the right. -
j
moves your cursor down. You can remember it sincej
looks like an arrow which points down (with a bit of imagination). Another mnemonic method: the keyj
has a little bump on the bottom of the key, which means the cursor will go down. -
k
is the only letter left, so it has to go up. I always imagine a Ninja Turtle jumping, saying "Kowabunga"! It's not even the good spelling (it would be "Cowabunga") but it works for me. Please, don't judge me.
I see your mind full of questions. Fear not, my friend! I've got you covered for this one, with a revolutionary AAA game everybody will speak about in twenty years. To play it, you must use hjkl
.
If you prefer puzzle games, try this wonderful sokoban. You can use hjkl
or the arrow keys this time. The goal, of course, is to use hjkl
as much as you can.
Insert Mode
We saw previously that the keystroke i
moved your soul in the reassuring world of insert
mode.
There are other handy keystrokes to do so, introducing welcome subtleties:
-
i
fori
nserting content before the current character. -
a
for inserting contenta
fter the current character. -
A
for inserting contentA
fter everything. It will move your cursor to the end of the line and enterinsert
mode. -
o
o
pen a new line, below the current one, and allow you to insert your content. -
O
O
pen a new line above the current one. -
r
r
eplace one character by another one. -
esc
andctrl-c
will bring you back tonormal
mode, if you are ininsert
mode.
Moving Horizontally
Moving is important: after all, targeting what you want to change is one of the main goal of the normal
mode. Here's how to move on a line:
-
w
move forward your cursor to the nextw
ord -
b
moveb
ack your cursor to the previous word -
0
will move your cursor to the beginning of the current line. -
ˆ
will move your cursor to the first non blank character on the current line. -
$
will move your cursor to the end of the current line.
Moving Vertically
You can move vertically simply by searching the word you want to move on (see above to learn how to search). There are other ways to move vertically, though:
-
ctrl-u
move your cursoru
pward half a screen. -
ctrl-d
move your cursord
ownward half a screen. -
gg
willg
og
o (go go?) at the beginning of the document. -
G
willG
o at the end of the document
Undo and Redo
What would we do without the essential undo and redo?
-
u
willu
ndo the last modifications you did. -
ctrl-r
willr
edo. Sincer
is forr
eplacing, we need to usectrl-r
forr
edo. You can thing of it as you being inctrl
of your content.
Third step: the Keystroke Language
In Vim, some keystroke can be combined to form sentences, describing an action. I know, it sounds weird, but it's brilliant: it will help you tremendously to remember all these keystrokes.
These sentences are so common that you will associate easily what you know already (the sentence) by what you need to learn (the keystrokes).
Even better: knowing that Vim has a keystroke language will push you to combine them instinctively to do what you need to do, and, in many cases, it will work!
It's magic, I tell you.
Let see some basic keystrokes which need to be combined:
-
d
ford
elete -
c
forc
hange
With these operators, you can act on text objects. Simply put, a text object is a set of character. In Vim, a word
is a text object, as well as a sentence
or a paragraph
.
Here are some combined keystrokes I use all the time:
-
diw
willd
eletei
nside thew
ord. It will delete the current word under the cursor. -
ciw
willc
hangei
nside thew
ord. It will delete the current word under the cursor and switch toinsert
mode. In short, you... change the word!
You can try to change a word or delete a word, it works as well and introduce some subtleties. I let you find what could be the keystrokes for these!
You can as well use y
(y
ank) as a basic combination keystroke, in order to copy some text. For example, you case use yiw
for y
ank i
nside w
ord, which will copy the word under the cursor.
To past, simply use p
or P
.
Finally, you can combine these keystrokes with some movements.
For example:
-
d$
willd
elete from your cursor to the end of line. You can use as well the aliasD
. -
dgg
willd
elete everything from the cursor to the beginning of the file. -
ggdG
will delete everything in the file!
That's all! With these keystrokes, you should already be able to do whatever you want in Vim! Be creative, try as many keystroke combinations as you like.
What's Next?
Congratulations! Now, you know the very basic of Vim.
This article, combined with a good cheatsheet will already bring you pretty far on the road of productivity.
You might think: "great, and what's the big deal?".
Vim has countless more tricks under its belt. As I was saying above, you can code its behavior using Vimscript
but you can as well use plugins created by the community, for any need you have. If you miss something not available in vanilla Vim, a simple search on the Internet will give you, most of the time, the plugin(s) you need!
However, I would recommend to use Vim's help before installing millions of plugins. Remember: by default, Vim can already do a lot.
To summarize what we learned with this article:
- Having proper typing techniques will make you faster, more accurate and will allow you to focus solely on your code, not on your keyboard or on your hands. Even more importantly, you will feel more in control.
- You need to use the row keys
h
,j
,k
andl
to navigate in Vim, to really appreciate its comfort, its power, and its fun! - Write your own cheatsheet as you learn new keystrokes, and try to find your own mnemonic to retain them. It will accelerate your learning process. To remember easily, associate what you already know with what you want to learn.
- Try for yourself the keystrokes explained here, for you to memorize them using your muscle memory.
Even if you still don't like using Vim at that point, at least you tried seriously to use it, and you will be able to do so when you don't have any other choice. You can now claim, as a man or a woman full of experience, that you don't like Vim... even if you tried to understand it!
What's next in your Vim learning journey?
- You should go through
vimtutor
. If you use Neovim, you just have to execute the ex command:Tuto
. If you use Vim, simply typevimtutor
in your shell. It will remind you some keystrokes from this article, and will teach you even more. - I would heavily recommend reading the book Practical Vim, from Drew Neil (see below). You will learn a lot from it.
- Vim is a game: the goal is to use as few keystrokes as you can to accomplish what you want to do. In that spirit, Vim Golf can be pretty fun.
- Look at the related resources at the end of the article: they will improve your Vim skills drastically.
From that point, you should have enough material to become a Vim Master!
Related Sources
- Learn Vim Progressively - Yann Esposito
- Vimcasts - Drew Neil
- Practical Vim - Drew Neil
- vim-galore - Marco Hinz
- 7 Habits of Effective Text Editing - Bram Moolenaar
- Learn Vim Script the Hard way - Steve Losh
Top comments (1)
I found vin is very useful on android phone in tmux where does not have arrowkeys. So vim with shortcuts is really perfect editor.