To launch the editor, just type vim
in a terminal. Unlike usual text editors,
vim
has 3 modes of operation: interactive mode, insertion mode and
command mode:
vim
or when
you are in another mode and press the ESC
key. In this mode, you can do
operations like moving in a file, copying and pasting, deleting text, replacing
text…i
key to switch to the insertion mode.vim
commands or system commands.
From the interactive mode, you have to press the :
key and then type the
command you want to execute.vim
in a terminali
to switch to insert modeC
programESC
(to exit insert mode) and then type :w test.c
to save
what you have written (:
is the command to enter command mode, w
is the
save -write command and test.c
is the file name)h
(left), j
(
down), k
(up), l
(right) keys. You can also use the arrow keys on the
keyboard.vim
, just type :q
or :q!
if you want to force the closure (by
default vim
does not close if there are unsaved changes).It is possible to save and exit with the command
:wq
.
In addition to the h
, j
, k
, l
keys, others are useful such as:
0
which automatically returns to the beginning of the current line$
which allows to go automatically to the end of the current linegg
which allows to go to the first line of the fileG
which allows to go to the last line of the filexG
where x
is a number allows you to go to the line x
e
which goes to the end of the current wordb
which goes to the previous wordw
which goes to the next word{
/ }
goes to the beginning / end of the current paragraph%
goes to the matching brace or parenthesis inside codex
or yx
where y
is the number of characters
to deletedd
or xdd
where x
is the number of lines to
deletedw
(delete word) or xdw
where x
is the number
of words to deleted0
.d$
.Same as cut, except the command is yy
to copy a line, yw
to copy a word,
y0
to copy…
To paste text, type p
or xp
where x
is the number of times to copy. Be
careful, the copy is done immediately after the cursor, there is no line break.
If you want to cut / copy a specific region of text, you can type v
in
command mode to enter visual mode and then by using arrows you can highlight
the region you want to edit. After that use cut / copy command to go back to
command mode.
rs
where s
is the new letter to be inserted:s/old/new
which replaces
the first occurrence of old
with new
, variants exist:
:s/old/new/g
: replaces all occurrences of the line where the cursor is
located:#,#s/old/new/g
: replaces all occurrences in lines # to # of the file:%s/old/new/g
: replaces all occurrences in the whole fileu
or xu
where x
is the number of times to go back/word
. To go to the
next occurrence, type n
or N
for the previous occurrence?word
, the rest is
the sameTo do this, just type the name of the command preceded by :!
, for example
:!ls
to display the contents of the current directory
In vim
just type :e filename
to open the file named filename
. You can
then navigate between files with :e #x
where x
is the file number.
There are lots of options in vim
to make it more user friendly (syntax
highlighting, line numbering, aliases…). Here are a few that you can write in
a .vimrc
file that you put in the root of your home directory (so you don’t
have to enable the options every time you open it).
set nocp
disables compatibility modesyn on
enables syntax highlightingset syntax =on
enables syntax highlightingfiletype indent plugin on
indent according to file typeset nu
displays the line numberset showmatch
show missing braces / parenthesisset tabstop =4
tab size in spacesset shiftwidth =4
size in tab spacesset softtabstop =4
size in tab spacesset expandtab
turns tabs into spacesset cursorline
highlights the line where the cursor is locatediab #i #include
example of an alias, now just type #i
then tab and it
will be replaced by #include
vim
with the command set spell spelllang=en
(
for english or fr
for french). When spelling is enables you can jump to
the spelling errors with [s (forward jump) ]s (backward jump).:sp
resp. :vsp
(and this can be done recursively). Once you have a split window, you can
navigate with CTRL w + arrow to navigate and CTRL w + c to close the
current split (and even CTRL w + o to close all the split except the
active one).There are many other shortcuts like bookmarks in file, multiple items in the clipboard, repeat the latest insertion command… but it is up to you to find the ones that makes you more efficient and productive in your workflow… bu remember practice is key !
vim
could be upgraded with many plug-ins that can help for the routine tasks.
A popular plug-in manager is Vundle
. One installed you can for instance have:
Plugin 'dense-analysis/ale'
is a syntax analyzer that highlights on the fly
errors in warnings while you are typing. This can helps you to correct many
errors before trying to compile your code. By default warning are
highlighted with --
while errors are highlighted with >>
. The status bar
gives some details about the error. Adding `nmap Plugin 'scrooloose/nerdtree'
allows you to have a sidebar with files in
your current folder. If you have the commande nnoremap <Leader>n
:NERDTreeToggle<CR>
in your .vimrc
, you can open / close the file sidebar
with the command \n
Plugin 'preservim/nerdcommenter'
allows you to (un)comment line(s). For
instance \cn comment the current line and \cu uncomment the current
line (you can comment / uncomment multiple lines in visual mode.To install vundle you should:
then edit your .vimrc
file with
then inside vim
type:
:source %
to reload you configuration file:PluginInstall
and you should be set-up. If you want to check other plugins, you can visit VimAwesome
In the terminal, run vimtutor
and try to finish the tutorial… have fun :)