Vim For Humans
Vim For Humans
Release 1.0
Vincent Jousse
CONTENTS
Preamble
1.1 Disclaimer . . . . . . .
1.2 Paid book but free price
1.3 Free license . . . . . . .
1.4 Thanks . . . . . . . . .
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
1
1
1
1
1
Introduction
2.1 For who? . . . . . . . . . . . .
2.2 What you will be learning . . .
2.3 What you will not be learning .
2.4 The hardest part is to get started
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
3
4
4
4
5
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
7
8
10
12
12
15
18
19
21
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
23
23
23
24
26
26
29
29
30
30
31
31
31
32
Essential plugins
5.1 Managing and switching between files : Lusty Explorer . . . . . . . . . . . . . . . . . . . . . . . .
5.2 Searching files on disk: Ack . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
5.3 Searching files on disk: Ctrlp . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
33
33
36
38
.
.
.
.
.
.
.
.
.
.
.
.
5.4
6
ii
Advanced plugins . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
39
41
41
41
41
41
42
42
42
42
42
43
43
45
CHAPTER
ONE
PREAMBLE
1.1 Disclaimer
Im not a native english speaker, so if you spot errors, feel free to tell me. Here is my email address: [email protected]
1.4 Thanks
A big thank to my lovely sister Sandra for her reviews!
Chapter 1. Preamble
CHAPTER
TWO
INTRODUCTION
When you need to write or to code, you have to choose a text editor, and a very good one. They are many text editors
available out there, but very few of them are more than 40 years old. Its the case of Emacs (http://www.gnu.org/
software/emacs/), Vi and its improved successor Vim (http://www.vim.org). They were created in the 70s and are still
used a lot nowadays. You may have already noticed that its not thanks to the beauty of their website or the efficiency
of their communication. Here are some reasons for their success:
Forever You learn them once and you use them forever. In a world where languages and technologies are constantly
changing, its a real chance to be able to invest on the long term.
Everywhere They are available for each and every possible platform : Mac Os X, Windows, GNU/Linux, BSD, . . .
and its always been the case.
Efficient Thanks to their features (like the extensive use of the keyboard), you can edit and write text as fast as your
thoughts.
For everything They allow you to edit everything and anything. When youll use another programming language, or
another markup language, youll not have to change your editor. Of course, this book has been written using
Vim (and the ReST Markup).
Yet, these text editors are difficult to learn. Not that they are harder than anything else, not that you cant handle it, but
rather because there is no smart way out there to learn them for now. So, here we are.
The aim of this book is to address this gap by guiding you through your discovery of Vim. Ill put Emacs aside from
now and Ill focus on Vim. If you want to know more about this Editor war, be sure to check the Wikipedia page.
This book doesnt claim to be a reference book about Vim. There are already a lot of good references on the subject
like A byte of Vim. However, it claims to reduce the entry barrier to get used to Vim. In my opinion, the most difficult
thing about learning Vim is not getting discouraged and finding a way to use it while learning it step by step. We all
have to get things done with our text editor on a daily basis, thats why losing all your productivity when switching to
Vim is not possible
Im sure youll find a lot of people who will tell you: Just do it cold turkey, Youll see, its hard at the beginning, but
time will help. True, but youll still have the problem of trying to remain productive on a daily basis. The approach
of this book is the following:
Have a modern Vim: syntax highlighting and nice colors.
Use Vim as any other text editor: easily edit code and switch between files using the mouse.
Learn keyboard shortcuts and go without the mouse step by step.
Install the best plugins to start using Vim to its full potential.
Starting from bullet number 2, youll already be able to use Vim on a daily basis without losing a lot of productivity.
Its where the magic will happen: if you can integrate Vim in your daily habits, you have won. Youll then have the
rest of your life to learn all the shortcuts and the tip and tricks of Vim.
Youre tired of trying a new editor each year? Youre tired of having to relearn everything from scratch every time?
Youre tired having to change your editor when youre using your Mac, Windows or Linux? So, just stop it, and join
the community of people happy with their text editor!
Chapter 2. Introduction
Mac OS X).
Youll not be learning how to use Vi (notice the lack of m). Ill only teach you how to be productive writing
text with Vim, Ill not teach you how to impress your friends with Vi (and anyway, Vim is enough for that). For
those who dont get what Im talking about, Vi is the ancestor of Vim (which stands for Vi - IMproved) and is
installed by default on all Unix-like systems (even on Mac OS X).
Youll not be learning to know Vim by heart: this book is not a reference its a pragmatic smart way to learn Vim.
Youll not learn how to pimp the colors of your Vim: Ill use the Solarized theme, its the best theme for your
eyes.
Chapter 2. Introduction
CHAPTER
THREE
This may be a surprising approach for you, but for me, the first thing to do is to have a Vim usable by a normal human
being. It seems that everybody agrees that Vim is a very powerful editor. And I think that you will agree too if I
say that, by default, Vim is totally unusable. Lets be honest, without a decent minimal configuration, using Vim is
counterproductive.
In my humble opinion, its the first obstacle to tackle before anything else. This is what all the trendy editors like
TextMate, Sublime Text, Notepad++ or Netbeans are proposing: a default environment usable as it is, even if we dont
use its full potential for now.
Here is what a default Vim is missing (and why most of people are giving up before they can really see the power of
Vim):
Default configuration You can configure Vim thanks to a file named ~/.vimrc. This file is, obviously, totally
empty by default. The first thing to do will be to have a ~/.vimrc file with a minimal configuration.
Syntax highlighting By default, Vim is white and ugly. To fix that, we will use the Solarized theme. If your goal is
to be efficient, its the best theme available out there (across all text editors), period. The beautiful image below
will give you an idea of what it looks like (with the dark and the light theme). Personally, Im using the dark
theme.
File explorer If your are using Vim along with a graphical interface (I suppose its the case for 99% of you) you will
by default have a File menu available. This menu should allow you to open a file. Its, for sure, a good start.
But having a file explorer a la Netbeans or Textmate can be very handy. To mimic the same behavior, we will be
using The NERD Tree. Be aware that, once you will have read this guide, you will not need the mouse anymore.
This chapter is mandatory if you have very few (or not at all) experience with Vim. By the end of the chapter, you will
have a Vim usable on a daily basis. It should be enough to then be able to learn it gradually. Because, of course, there
7
is no magic, you will have to practice to be able to learn Vim, the sooner, the better.
However, if you are already familiar with Vim and dont use the mouse anymore, you can skip this chapter. But be
sure to give Solarized a try, you would be missing something otherwise.
On a side note: if you dont really understand what you have done and Vim is displaying red messages at the bottom
left or doesnt seem to react as it should when you press the i key, dont panic. Pressing multiples times on the Esc
key (two times should be enough) should bring Vim to its default mode, the Normal mode. Then it should behave as
you would expect again.
You should know be able to write down the comment below:
" VIM Configuration - Vincent Jousse
You will have noticed that comments in VimL (the name of the Vim programing language) start with a ". Then press
the Esc key to come back to the default mode (the normal mode) of Vim. Thats all, you are done. Here is a screenshot
of what your Vim should look like now:
I can already hear you: all that fuss for that? Well, yes. And you even dont know how to save a file. But all these
things are logical that Im gonna explain to you. One of the advantages of Vim is that, usually, it is logical. Once you
will have understood the logic behind it, all will be crystal clear for you (at least I hope so).
By default, when you start Vim, you are presented with its default mode. This mode is called the Normal mode. The
purpose of this mode is not to write text (for that, you will have the Insert mode), but only to move the cursor and to
manipulate text. The power of Vim is coming from the combination of these two modes (other modes exist, but its not
the topic for now). You will need some time and some practice to realize the power its giving to you, so you will just
need to trust me in the meantime.
If you are asking yourself why those modes, why by default we cant even write down some text and why we are
making things more complicated than they should, the next chapter is for you.
So what do you have the right to do? Not a lot to be honest (but its for your own good), you can only leave your hands
on the home row as you can see on the picture below.
You will also find on each keyboard some marks on the letters F and J, the goal of these marks is to give a landmark
for the position of your fingers (the indexes) on the home row of the keyboard.
Trying to move as less as possible the hands from the keyboard is the reason for having a normal mode and an insert
mode in Vim. When switching from one to the other, the keys under your fingers will sometimes allow you to move
the cursor and to manipulate text: copy/paste, deletion, . . . (its the normal mode), sometimes they will allow you
to select some text (its the visual mode) and sometimes to insert some text (its the insert mode). And of course, all
of that is possible without the extensive use of keys combinations like Ctrl + key that are very bad for your fingers
(Emacs, this on is for you).
By default, we can switch from the insert mode to the normal mode by pressing the Esc key, but it will be one of the
first things we will change: the Esc key is to far from your fingers on current keyboards.
To switch from normal mode to insert mode, we can for example press the i key. We will later learn that there are
other ways to do so too. For example, to enter the insert mode and to then create a new line below the current one (no
matter where is your cursor on the line), we will use the o key while in normal mode.
I will talk again about this subject later in Learning how to move: the copy/paste use case, but if you are not ready,
at some point, to do without your mouse and the directional keys to edit text, I would recommend you to stop learning
Vim right now. Its as simple as that. You can leverage the full power of Vim only by getting rid of the mouse and by
moving your hand as little as possible.
If you want to go further, you can buy an orthogonal keyboard like TypeMatrix. Its the keyboard Im currently using,
11
13
14
Themes will allow you to have a nicer Vim than the default one. A theme will change the background color of Vim and
the colors used for the syntax highlighting. As I said earlier, we will use the Solarized theme http://ethanschoonover.
com/solarized (with dark or light background, it will be up to you).
To install it, you will first need to create a directory called .vim in the same directory than your ~/.vimrc (that is
to say, in your home directory). Note that when using Windows, the .vim directory is called vimfiles. Each time Ill
be speaking of the .vim directory, it will be the vimfiles directory for people using Windows. In this .vim directory,
create a sub directory named colors. Then, download the Solarized theme file https://raw.github.com/altercation/
vim-colors-solarized/master/colors/solarized.vim (its the same file for the light and the dark version) and copy it in
your vim/colors/ directory. You .vim directory should look like the picture below.
To test the light theme, you just have to change dark with light (for the background property).
Here is a preview of the two versions (personally, I prefer the dark one).
A bonus (if you dont use Vim directly in your terminal) would be to choose a font that suits your needs a little bit
better. This is of course optional, but I suppose that some of you are sensible to such things.
If your are using Mac OS X, I recommend the Monaco font that is quite friendly. Add the following lines to your
~/.vimrc to use it:
set guifont=Monaco:h13
set antialias
You can of course change h13 with h12 if you want a smaller font (or with h14 if you want a bigger one).
Under Linux I am using the DejaVu Sans Mono font:
set guifont=DejaVu\ Sans\ Mono\ 10
set antialias
You can of course change the font size as you want. To have the list of all the available fonts for your system type :set
guifont:* in normal mode.
You will find the full version of the configuration file for this chapter online http://vimebook.com/link/en/
syntaxhlconfig. I will not spend more time talking about the fonts as its dependant of your operating system and
not of Vim.
15
16
17
install our first plugin: a file explorer. We will use a two-step approach here: first we will install a plugin manager and
then we will install the plugin using this plugin manager. By default Vim doesnt come with a plugin manager and,
believe me, if you dont install one your ~/.vim directory will soon be a real mess. So lets get started.
18
From now on, we will install our plugins directly in the .vim/bundle directory that you will create right now:
# Create the bundle directory used by Pathogen
mkdir -p ~/.vim/bundle
All you have to do is to activate Pathogen in your ~/.vimrc file and voila. We will put the following lines at the
very beginning of the ~/.vimrc file, right after the set nocompatible line. It is mandatory to put the code at the
beginning of your ~/.vimrc file, otherwise it will not work.:
" Activate pathogen
call pathogen#infect()
Since charity begins at home, we will tidy up our install by using Pathogen with our Solarized plugin. We just have
to create a directory called solarized in your newly created .vim/bundle directory. You could name it as you want as
any subdirectory of the bundle directory will be considered as a plugin directory. Then, we will move our previously
created colors directory in the solarized directory as follow:
# Create the plugin directory for solarized
mkdir ~/.vim/bundle/solarized
# Move solarized to the bundle dir
mv ~/.vim/colors ~/.vim/bundle/solarized
Currently, Pathogen is still the most used plugin manager for Vim. But recently, a new challenger has arrived, its
called Vundle https://github.com/gmarik/vundle. I have chosen to use Pathogen here because its the one you will hear
about the most often. But you have to know that Vundle is an interesting alternative: its compatible with Pathogen
and you can manage your plugins directly from internet (from github, the vim website, . . . ). For those familiar with
Ruby, its Bundler (http://gembundler.com) for Vim.
Our Vim is almost ready to be used on a daily basis. We are just missing an handy way to explore the files of a project.
We will use The NERD Tree for that.
Then, download the latest .zip available on the plugin page http://www.vim.org/scripts/script.php?script_id=1658. At
the time of this writing, the latest version is the 4.2.0 that you can directly download using this url: http://www.vim.
19
org/scripts/download_script.php?src_id=17123.
Unzip the file and copy the content in the newly created ~/.vim/bundle/nerdtree directory. The directory
structure of your nerdtree directory should look like the one below:
nerdtree
|-- doc
|
`-- NERD_tree.txt
|-- nerdtree_plugin
|
|-- exec_menuitem.vim
|
`-- fs_menu.vim
|-- plugin
|
`-- NERD_tree.vim
`-- syntax
`-- nerdtree.vim
Then, you will need to activate the plugin. You can do it manually by typing :NERDTree in normal mode. If you
prefer to activate The NERD Tree every time you open your Vim, add these lines to your ~/.vimrc:
" Activate the NERDTree when launching vim
autocmd vimenter * NERDTree
I have to admit that this command is a little bit obscure. Actually it means: every time you open vim (vimenter),
no matter what is the type of the file (*), launch The NERD Tree (NERDTree).
Nothing special then, The NERD Tree will display the tree of the directory where youve launched Vim as you can see
on the picture below. You can use your mouse and/or your keyboard to move inside The NERD Tree.
20
You can also run commands (create, copy a file) by typing the m key when you are inside The NERD Tree. To switch
between the NERD Tree window and your file window with your keyboard, use Ctrl + w and then w. That is to say,
hold the Control (Ctrl) key and at the same time press the w key. You can then release everything and press w
again. This shortcut is valid to switch between any Vim window (its not a shortcut specific to The NERD Tree).
3.6 Here we go
Now, youve done the hardest part. Well, almost. Weve just covered what is sorely lacking in Vim: a sensible default
configuration. Im not saying that you now have the best editor out there, but at least, you should be able to use Vim
as any other normal text editor that you do not yet know all the possibilities. I recommend, at this stage, to start using
Vim in your everyday life. Feel free to use the mouse and the menus for now. The primary goal here is to reduce the
negative impact that Vim could have on your daily productivity, if not configured properly. You will gradually learn
the keyboard shortcuts when the time will come.
We will now discuss what makes the uniqueness of Vim: the way modes are handled and the shortcuts to manipulate
text. The ball is in your court now: either you are willing to change your habits and move to another level of efficiency
with Vim, or else using Vim as an improved notebook is right for you (in this case, you can stop there). Its up to you !
3.6. Here we go
21
22
CHAPTER
FOUR
I confess, I have some weird dreams. But hey, maybe my dreams are not as weird as they seem: dreaming of a tool
that can improve all the professional areas of my life as a coder, writer, teacher, . . . doesnt seem that weird after all.
The success of Vim is due to its ability to ease the text manipulations. Certainly, it will provide you with functionalities dedicated to specific tasks (often via plugins) as syntax highlighting, spell checking, . . . but in the end, its always
to write/fix/handle/move text that you spend most of your time.
This is where the difference lies between Vim and the IDE like Eclipse/Netbeans/PhpStorm and others. Such IDE will
put the focus on the particularities of your programming language while providing basic text manipulation functionalities. Vim takes the opposite approach: you will by default be very effective to manipulate/write text no matter what
kind of text. But then, when you will feel the need, you will be able to enrich Vim with plugins specific to your needs
and programming languages.
This chapter will cover how to use Vim the right way (you will begin to forget your mouse) and what is the logic
behind all these obscure commands. By the end of this chapter, you should be able to not use your mouse anymore
to edit/handle text. In any case, you should force yourself when learning Vim, its not that hard to only use the keyboard,
and it will make a huge diffrence in your day to day life.
4.1.1 Preamble
Now is the time to learn our first text manipulation: the famous copy/paste. I can already hear some of you saying that
it is useless: you already know how to do that. You switch to the insert mode, you use your mouse (or you move using
the directional keys while holding the Shift key) to select some text. Then you go to the edit menu and you select
copy. Then, edit menu and paste. Well, why not.
But if you have understood the Modes: the powerful Vim secrets section about the ideal position of your hands on
the keyboard, you should know that you did things you were not supposed to do:
You used your mouse
You moved your right hand a lot from its rest position, to press the directional keys that are far away from your
fingers
Certainly, it doesnt really matter, but its totally ineffective (using your mouse or moving your right hand toward
the directional keys is very slow) and harmful for your little hands. This is your last chance: if you are not ready to
23
force yourself to not do it, |vim| is not for you. Vim does a perfect job at keeping your hands on the keyboard and at
forgetting your mouse. If you dont do it, you will not be using Vim to its fullest. And, one day or another, you will
quit for another editor that was made to be used with a mouse. So, should we continue?
When there is nothing displayed at the bottom left, its because you are currently in normal mode. In order to quit a
mode to return to the normal one, you just have to press the Esc key. You may already have noticed that pressing the
Esc key is a pain for your fingers. No worries, its just a temporary thing, I will explain why in the Doing without
the Esc key section.
Lets say that you are currently in the normal mode and that you already have some text in you Vim. For example, it could be this beautiful quote from Mark Twain: They did not know it was impossible, so they did
it.. Your Vim should like the one in the figure below. Notice that there is nothing displayed at the bottom left.
24
The most intuitive way (but not the most efficient, we will see why a little bit later) to copy/paste the impossible
word is to move the cursor at the first letter of the word using the directional keys, to press the v key (to switch to the
visual mode), to move to the last letter of the word (you should have the word impossible highlighted) and then to
press the y key (the y key stands for yank). Youve just copied your first word using Vim. Hooray!
Then, move to the end of the sentence (in normal mode) and press the p key (the p key standing for
paste). The word should now be pasted at the end, and you should have something like the figure below.
25
We can see that Vim uses the mode switching trick (including the normal mode for moving) in order to not have
to use the mouse. When you will be used to switch quickly from one mode to another (and in order to do so, going
without the Esc key will be mandatory), using the mouse will appear like a pure waste of time. But obviously, you
will first need to train yourself.
26
As you can notice, those keys are located on the home row so that you dont have to move your hands. Your index
has two moves (left and bottom) while your auricular doesnt have any. You will see that its not a problem, its even
a feature: your index is stronger than your auricular. By checking the keyboard that was used to develop Vi in the
Doing without the Esc key section, you will understand why.
On a side note, once you will be used to Vim, you will not use the left and right moves a lot. You will primarily move
the cursor word by word, paragraph by paragraph or by using the search function. Here are some fast moves that I
otfen use:
Key
e
b
w
^
$
0
Move
to the end of the current word
to the beginning of the current word
to the beginning of the next word
to the first non white character of the line
to the end of the line
to the start of the line
This is the mininum to move your hands in normal mode. They are also commands allowing you to first move and
then to enter the insert mode directly. They are very handy because they will allow you to save a few keystrokes. Here
are some that I often use:
Key
i
a
I
A
o
O
r
Action
enter insert mode just before the cursor
enter insert mode just after the cursor
enter insert mode at the beginning of the line
enter insert mode at the end of the line
insert a new line below the current line
insert a new line above the current line
replace the character under the cursor by a new one
Lets discuss that a little bit. The secret of Vim relies in this chapter. There is one thing that you have to do when
learning Vim: use the hjkl keys to move. If you can manage to do that, you will learn everything else on the go.
Youll find a lot of websites with all the possible commands, combinations and so on. You will learn and forgot them
(depending on how useful they are to you). If you have a single effort to do: it is to use the directional keys and thus
to force you to use the normal mode. Everything else will then be perfectly obvious.
Here is the ultimate configuration that you will need to put in your ~/.vimrc to achieve your goal: disabling the
directional keys:
27
Here we are. Believe me, this will be a little bit hard at the beginning. It was the case for me during the first two days.
But then, you just forget and deal with it. Anyway, if you are not ready to struggle for two days in order to learn Vim
properly: what are you doing here?!
I will not go into details on all the possible keys to move inside Vim, other resources do a better job at it. Its the case
of A byte of Vim that you can freely download here: http://www.swaroopch.com/notes/vim/. But on the other hand,
you will learn in Combining keys and moves how to use them wisely.
Here is an handy graphical cheat sheet that you can download on http://www.viemu.com/a_vi_vim_graphical_cheat_
sheet_tutorial.html. I would recommend you to print it and to put it on your desktop: it helps a lot at the beginning.
Keep in mind that the main goal here is to increase your speed while keeping your hands on the home row and using
the normal mode. Get down to work!
28
So we agree that we need another key to exit the insert mode. There are many solutions, here are some possibilities
that you can try in your ~/.vimrc:
" Press the j 2 times in row
:imap jj <Esc>
" Press the j key followed by the k one
:imap jk <Esc>
" Press the i 2 times in row
:imap ii <Esc>
:imap ` <Esc>
" Shift-Space
:imap <S-Space> <Esc>
" Alt-Space.
:imap <M-Space> <Esc>
You can have a look at the discussion here if you want more information: http://vim.wikia.com/wiki/Avoid_the_
escape_key.
29
In Forgetting the directional keys we have seen that if we want to move to the start of the next word we just have to
use the w key. We will combine that with some new keys of the normal mode:
the d key is used to delete
the c key is used to delete and switch to insert mode
Something good to know: by default, everything that is deleted is placed in the clipboard. The delete is behaving like
a cut.
The particularity of these keys is that they are waiting for a move order to know what should be deleted. So you will
need to provide one of the keys that we have discussed in the Forgetting the directional keys chapter.
Here are some examples:
Action
the d key then the w key
the c key then the w key
the d key then the $ key
the d key then the ^ key
Result
deletes all the characters until the next word
deletes all the characters until the next word and switch to the insert mode
delete everything until the end of the line
delete everything until the start of the line
Result
copy the characters until the next word
copy everything until the end of the line
copy everything until the first non blank character of the line
All you have to do next is to press the p key to paste the text you have copied above. By default, the p key will paste
the text after the current position of the cursor. If you want to paste before the position of the cursor, use the p key.
From time to time, you may also want to be able to delete some text. . . ;) Here are some useful commands to do so:
Action
dd
x
X
Result
delete the current line and put it in the clipboard
delete the character under the cursor
delete the character before the cursor
Most of the moves can be prefixed by a multiplier number. Here are some examples:
Action
2dd
3x
3X
2yy
5j
Result
delete 2 lines
delete 3 characters forward
delete 3 characters backward
copy 2 lines in the clipboard
move 5 lines downward
30
4.5.2 Marks
When Im moving inside a file, I often need to go back to some previous points. For example, when I go to the
beginning of the file while I am working in the middle of it, I like to come back directly to where I was working before
moving to the beginning. Fortunately, Vim has everything for it through the use of markers. Markers are simply
bookmarks that allow you to move quickly through the file.
You can put a marker by pressing ma. To move your cursor to the position of the marker, just press 'a. You can place
as many markers as you want by changing a with any letter of the alphabet (this is called register in Vims language).
To place another marker you can for example use the letter d. Thanks to md you will put the marker and with 'd you
will move to the position of the marker.
4.5.3 Search
In normal mode, you can start a search by using the / key followed by the text you want to search and the Enter
key. Thanks to our Vim configuration you should see your search occurrences highlighted at the same time as you
type. By default, the search is not case sensitive (no difference between upper and lower case). However, as soon as
you will type a capital, the search will become case sensitive. You can move forward to the next search result thanks
to the n key. To move backward, use the N key.
As a reminder, here are the corresponding lines of your configuration:
" -- Search
set ignorecase
set smartcase
set incsearch
set hlsearch
"
"
"
"
"
Be careful, by default, the search is using POSIX regular expressions. If you want to search for characters commonly
used in regular expressions (like [ ] ^{ } $ /) do not forget to prefix them with \.
You can also search for the word that is directly under your cursor through the * key. the * key will search forward,
the # key will search backward.
31
When you are in normal mode, press the v key to switch to the visual mode. You will then be able to select
individual characters or entier lines thanks to the various ways of moving that you just learned above. You can then
copy the selected text with the y key and paste it with the p key. To cut it just use the d key instead of the y key.
In normal mode you will be able to use the v key to select line per line. And of course, use the Esc key or ;; to
switch back to normal mode.
Now we will go to the next level: the use of plugins, or how to make Vim indispensable.
32
CHAPTER
FIVE
ESSENTIAL PLUGINS
Lets be clear, using Vim without plugins is almost useless. Its the usage of plugins that will allow you to boost your
productivity. You dont need a lot of them, but you need the good ones.
Vim can of course be used without any plugin and it can be useful to know how to use it without needing to install
additional plugins. Indeed, on most servers, you will have zero plugin installed. Thats why, knowing how to open and
save a file, knowing how to switch between files just by using default commands is very useful. However, for your
writing or coding needs, plugins are mandatory.
If you have done everything right since the beginning, your .vim/ directory should now look like this:
.vim
|-- autoload
|
`-- pathogen.vim
`-- bundle
|-- lusty-explorer
|
`-- plugin
|
`-- lusty-explorer.vim
|-- nerdtree
33
|
|
|
|
|
|
|
|
|
`--
|-|
|-|
|
|-|
`--
doc
`-- NERD_tree.txt
nerdtree_plugin
|-- exec_menuitem.vim
`-- fs_menu.vim
plugin
`-- NERD_tree.vim
syntax
`-- nerdtree.vim
solarized
`-- colors
`-- solarized.vim
Lets see how to use it. If we take a look at the documentation, here is what we find:
<Leader>lf
<Leader>lr
<Leader>lb
<Leader>lg
Opens
Opens
Opens
Opens
filesystem explorer.
filesystem explorer at the directory of the current file.
buffer explorer.
buffer grep.
We can see that the documentation is referring to a key named <Leader> that you need to combine with keys like
lf, lr, lb et lg. The <Leader> key is a special key that we need to define in our ~/.vimrc. Almost each plugin
will need this special key to be defined, so we will use it a lot. This is a good way to avoid collisions with the default
shortcuts of Vim.
So, we need to choose a key to be our <Leader> key. By default, Vim uses \ as a <Leader> key. I dont know
about you, but for me this is not handy at all. I dont love to use my little finger too much. So I always replace the
default <Leader> key with the , key. You can of course choose another key, but lot of people are using ,: its up to
you. To tell it to Vim, you will need to add a line in your ~/.vimrc as follow:
let mapleader = ","
Once the modification made and taken into account by Vim (by restarting Vim or by typing :so
~/.vimrc or :so $MYVIMRC in normal mode), you should be able to do ,lr (if you choosed ,
as your <Leader> key) and you should see something like the picture below in your Vim.
34
The next thing to do is to deactivate The Nerd Tree by commenting the corresponding line like I have done on the
screenshot above. It will not be useful anymore as Lusty Explorer is a better replacement when using the keyboard.
You can see on the lusty screenshot that Lusty Explorer is made of two parts. The bottom part is about the current
directory youre exploring and the top part is the content of this directory. The current item is highligthed. For
example, on the lusty screenshot above, the current item is the .vim/ directory, highligthed in yellow (the color could
be different, it depends on your theme).
Lusty Explorer uses something called Fuzzy
file you want to select. This part can be
or just letters composing the file to select.
.viminfo will be selected without needing
key to open the corresponding file in Vim.
matching that will allow you to type only a small part of the
everything: the begining of the filename, the middle, the end
In the example above, if I enter .vimi in the Lusty window,
to specify the full name. Then I just need to press the Enter
You can see this particular example in the screenshot above.
35
doc
`-- ack.txt
plugin
`-- ack.vim
As always, be sure that your modifications are taken into account by restarting Vim or by entering :source ~/.vimrc
while in normal mode.
Then we will need to add some lines to our ~/.vimrc file to ease the use of the plugin ::
" Default params for ack
let g:ackprg="ack -H --nocolor --nogroup --column"
" Add a mark and search
nmap <leader>j mA:Ack<space>
" Add a mark and search for the word under the cursor
nmap <leader>ja mA:Ack "<C-r>=expand("<cword>")<cr>"
nmap <leader>jA mA:Ack "<C-r>=expand("<cWORD>")<cr>"
36
Ack will start the search from the directory of the file currently opened. Here are some examples (supposing that your
<Leader> key is the , key):
,j toto : will search for toto starting from the directory of the current file,
,ja with your cursor on a word will search for this word.
The results will be displayed in a window called the Quickfix Window, as you can see below.
37
As always, be sure that your modifications are taken into account by restarting Vim or by entering :source ~/.vimrc
while in normal mode.
Now we need to add the shortcut to our ~/.vimrc to invoke CtrlP like in the listing below. Personnaly, Ive chosen
,c, but you can choose whatever you want.:
let g:ctrlp_map = '<leader>c'
38
Launch it with ,c and then type the name of the file you want to search. When the searched file will be selected first,
you will just have to press the Enter key to open it.
CtrlP can be used for navigating through the opened files (like Lusty), but I find Lusty handier for that. You can also
use to navigate directly through your code by following your functions thanks to something called the tags (like you
can do when using Eclipse). Its a too big topic for the scope of this book, but if youre interested you can read this
blog article on the topic: http://andrew-stewart.ca/2012/10/31/vim-ctags.
39
ctags + ctrlp. Ctags is a small external program that will parse your source code and allow you to follow
your functions calls through your source code. Very useful to navigate into your source code. Used with ctrlp
described above, it will soon become a must. Everything is explained here: http://andrew-stewart.ca/2012/10/
31/vim-ctags.
40
CHAPTER
SIX
So here we go, we now have everything needed to make a good start with Vim. It should be enough to use it on a
daily basis. Its the most important thing to do if you want to successfully use Vim: developing the habit of using it
everyday. Once youll have this habit, everything should seem natural to you.
The aim of this chapter is to provide you with a reference for the most common things youll have to do with Vim so
that, when you are lost, you know where to search for help. This chapters contains two parts. The first one is a set of
Q/A covering the most common problems that newbies are facing. The goal is to answer this kind of question: f**k,
how can I do this, it was so simple with my old editor. The second is a non-exhaustive list of the most useful Vim
commands.
41
Paste after the cursor using the p key or before using the p key.
6.1.5 Undo/redo
To undo, just press the u key while in normal mode. To undo your undo (and so, to redo) press the Control key and
the r key at the same time.
6.2 Reminders
6.2.1 Files
Expected result
Save
Save as
Save as / open
Quit without saving (force quit)
Save and quit
Save as root
Action
:w
:w filename.txt
:sav filename.txt
:q!
:wq
:w !sudo tee %
Comments
Save as filename.txt but dont open filename.txt
Save as and open filename.txt
6.2.2 Moves
Expected result
Move one character left
Move one line down
Move one line up
Move one character right
Move to the end of the word
Move to the beginning of the word
Move to the beginning of the next word
Move to line 42
Move to the beginning of the file
Move to the end of the file
Move to the end of the line
Move to the first non empty character of the line
Move to the beginning of the line
Move one page down
Move one page up
Move to the first line of the screen
Move to the middle of the screen
Move to the last line of the screen
42
Action
h
j
k
l
e
b
w
:42
gg or :0
GG or :$
$
^
0
Ctrl+f
Ctrl+b
H
M
L
Chapter 6. Cheatsheet & examples
Action
i
I
a
A
o
O
C
r
x
X
dd
yy
p
P
~
>
<
d
c
y
u
Ctrl+r
Comments
Normal mode
Normal mode
Normal mode
Normal mode
Normal mode
Normal mode
Normal mode
Normal mode
Normal mode
Normal mode
Normal mode
Normal mode
Normal mode
Normal mode
Visual mode
Visual mode
Visual mode
Visual mode
Visual mode
Visual mode
Normal mode
Normal mode
6.2. Reminders
Action
/*toto
Comments
Search the toto string starting at the current cursor
position
n
Go to the next search result
N
Go to the previous search result
:s/toto/titiReplace toto by titi on the current line (once)
:s/toto/titi/g
Replace toto by titi on the current line (for all
occurences of toto)
:%s/toto/titi
Replace toto by titi on all the lines of the file (once per
line)
:%s/toto/titi/g
Replace toto by titi on all the lines of the file (for all
occurences of toto)
:s/toto/titi/i
Replace toto by titi on the current line, case insensitive
(once)
:s/toto/titi/gi
Replace toto by titi on the current line, case insensitive
(for all occurences of toto)
43
44
CHAPTER
SEVEN
genindex
modindex
search
45