Vim is a text editor with features that make it very particular:
- It doesn’t need a graphical environment. It works with a terminal emulator.
- It has different edit modes, which make it very versatile and powerful.
- It has a steep learning curve.
The benefits of learning Vim outweigh the time spent learning it, and it quickly becomes an essential tool, not only to edit text more efficiently but also to interact with other *nix command line tools.
In this post, I will guide you through the theoretical and practical knowledge that I think is needed to grok Vim, and add it to your toolbox.
Note: The intended audience of this document are developers, sysadmins and other power-users that want to learn Vim or expand their knowledge about the tool. It is also worth mentioning that the majority of the information presented is also applicable to vi, the older brother of Vim.
vimtutor
Vim is usually accompanied by vimtutor, an interactive tutorial to learn the editor basics.
I recommend to complete it. Once you learn the concepts that are presented, the hardest part of the learning curve will have been overcome.
To start the tutorial, go to your terminal and run vimtutor
.
Basics
To open Vim, type vim
in your terminal. This will open the editor without any file loaded. To quit the editor, press :q
.
To open a file, run vim file.txt
. You can also open multiple files using the -p
flag: vim -p file1.txt file2.txt...
, and move through the tabs pressing gt
and gT
.
Some useful commands:
:pwd
: Print the current directory name.:qa
: Close all the files open at once and exit the editor.:tabf file.txt
: Open a new tab page to edit an existent file.:tabnew newfile.txt
: Open a new tab page to edit a new file.
Before you start editing files, you need to learn some concepts:
Modes
Depending on the Vim mode in which you are, the editor will respond to your keystrokes in different ways.
Command mode, AKA Normal mode
In this mode you can run different commands, like insert, add, delete, replace, search text and move across the text file.
When we enter Vim, this is the active mode by default.
To enter the Normal mode, or exit any other mode, press the key ESC
.
Insert mode
In this mode, you can insert text, in a similar way as in other more conventional text editors like Sublime, Atom, Brackets, etc. Also, you can move across the text file using the cursor keys.
Insert mode can be reached in several ways, but some of the most common ones are:
i
: Insert before the cursor.a
: Append after the cursor.A
: Append at end of the line.I
: Insert at the beginning of the line (non-blank character).o
: Insert at the beginning of the next line (non-blank character).O
: Insert at the beginning of the previous line (non-blank character).C
: Change to the end of the line.
To exit the insert mode, press ESC
.
Visual mode
In this mode you can select text to later execute different actions on it.
The most common ways to enter visual mode are:
v
: Visual mode characterwise. Characters will be selected as we move the cursor.V
: Visual mode linewise. Lines will be selected as we move the cursor up and down.
To exit the visual mode, press ESC
.
Operator commands
Operator commands are generally used to delete or change the text.
Some of the most common are:
c
: Change.d
: Delete.y
: Yank.gu
: Make lowercase.gU
: Make uppercase.
Cursor motions
Cursor motions are used to move efficiently across the file.
Arrow keys or keys hjkl
(in Normal mode) move one character left, down, up or right respectively.
Word motions
A word consists of a sequence of letters, digits, and underscores, or a sequence of other non-blank characters, separated with white space (spaces, tabs, ). An empty line is also considered to be a word.
A WORD consists of a sequence of non-blank characters, separated with white space. An empty line is also considered to be a WORD.
w
: Move to the next word.W
: Move to the next WORD.e
: Forward to the end of the word. It does not stop in an empty line.E
: Forward to the end of WORD. It does not stop in an empty line.b
: words backward.B
: WORDS backward.
Left-right motions
$
,End
: Move to the end of the line.0
,Home
: Move to the first character of the line.^
: Move to the first non-blank character of the line.
Up-down motions
gg
: Go to the first line, on the first non-blank character.G
: Go to the last line, to the first non-blank character.
Various motions
%
: Find the next item in this line after or under the cursor and jump to its match. Items can be:([{}])
parenthesis or (curly/square) brackets./* */
start or end of C-style comment.#if
,#ifdef
,#else
,#elif
,#endif
C preprocessor conditionals (when the cursor is on the#
or no([{
following).
Editing
The effective combination of modes, operator commands and cursor motions is what makes the difference in Vim.
For example, you can:
- Delete two words with
d2w
. - Delete all the content of the file with
ggdG
:gg
goes to the first line,d
deletes to the end of the file (G
). - Change the parameter list of a function definition with
ci(
(change the text inside parenthesis).
The possible combinations are endless, and it is a matter of time and practice to learn them.
To save the changes in a file, press :w
. If you also want to close the file, press :wq
.
To save the changes in all the open files, press :wa
. If you also want to close all the files, press :wqa
.
Search and replace
In normal mode, you can search forwards by pressing /
and then type your search pattern. Press ESC
to cancel or Enter
to perform the search. The search pattern can be a literal search term or a regular expression. Press n
to search forwards for the next occurrence, or N
to search backward.
The :substitute
command searches for a text pattern and replaces it with a text string.
There are many options, but the most common form is :%s/foo/bar/g
. Find each occurrence of “foo” (in all lines), and replace it with “bar”. The g
flag means global (each occurrence in the line is changed, rather than just the first).
Configuration
Vim can be configured with a vimrc file. Typically, the file is located in $HOME/.vimrc
on *nix systems.
Some useful configurations are:
" Syntax highlighting enables Vim to show parts of the text in another font or color. syntax on " Vim will set the title of the window. set title " Tabs and space configurations, to insert spaces instead of tabs and handle indentation more correctly. set expandtab set tabstop=4 set softtabstop=4 set shiftwidth=4 set smartindent " Highlight the screen line of the cursor. set cursorline " When searching, the case of normal letters is ignored. set ignorecase " Override the 'ignorecase' option if the search pattern contains upper case characters. set smartcase " While typing a search command, show where the pattern, as it was typed so far, matches. set incsearch " When there is a previous search pattern, highlight all its matches. set hlsearch " Lines will not wrap and only part of long lines will be displayed. When the cursor is moved to a part that is not shown, the screen will scroll horizontally. set nowrap " Precede each line with its line number. set number " Show the status bar all the time. set ls=2 " Allows N tabs open. set tabpagemax=999 " Round indent to multiple of 'shiftwidth'. Applies to > and < commands. set shiftround " New horizontal splits are created below the actual split. set splitbelow " New vertical splits are created to the right of the actual split. set splitright " Minimal number of screen lines to keep above and below the cursor. set scrolloff=3
Documentation
Vim’s online documentation system, accessible via the :help
command, is an extensive cross-referenced and hyperlinked reference. It’s kept up-to-date with the software and can answer almost any question about Vim’s functionality.
Mappings
In the vimrc file, you can map keys to change their default behavior. For example, here are mappings to configure the tab handling:
map tn :tabn map tp :tabp map :tabn map :tabp map tm :tabm map tt :tabnew map ts :tab split
Plugins
Vim can be extended with plugins.
The recommended way of adding plugins is using a plugin manager.
Some of the plugins that I recommend are:
- Plugin manager: Vundle (https://github.com/VundleVim/Vundle.vim)
- Fuzzy file, buffer, mru, tag finder: CtrlP (https://github.com/ctrlpvim/ctrlp.vim)
- Commenter: NerdCommenter (https://github.com/scrooloose/nerdcommenter)
- Filesystem tree explorer: NerdTree (https://github.com/scrooloose/nerdtree)
- Status/Tabline: Airline (https://github.com/vim-airline/vim-airline)
- Class outline viewer: TagBar (https://github.com/majutsushi/tagbar)
- Better whitespace highlighting: BetterWhitespace (https://github.com/ntpeters/vim-better-whitespace)