[NOTE: This module is due for a major update (mainly with respect to the terms used) -- will push it in early 2018.]
Home | Previous module | Next module
This is the module that introduces what makes Vim the most unique and amazing text-editor -- the normal mode.
The Vim philosophy dictates that most of the editing should be done in normal mode, not in insert mode (hence the name normal). Vim makes this possible by introducing its own unique, flexible language. Many of the letters in the Vim alphabet have several contextual meanings. Let us begin with some of them:
a
: around, append;A
: append at the end of lineb
: beginning of wordc
: change (delete and switch to insert mode);C
: change till the end of lined
: delete;D
: delete till the end of linee
: end of current wordf
: find in current lineh
: alias to left arrowi
: inside, switch to insert mode;I
: insert at the beginning of linej
: alias to down arrowk
: alias to up arrowl
: alias to right arrown
: next search item;N
:n
in reverse ordero
: open a new line below current line;O
: open a new line above current linep
: paste after the cursor position;P
: paste before the cursor positionr
: replace current character;R
: switch to replace modet
: tillu
: undo (<C-r>
is for redo)v
: visual mode;V
: visual-line mode;<C-v>
: visual-block modew
: next wordx
: cuty
: yank (copy)
Some of the commands have a two-letter combination. For example:
dd
deletes the current linecc
changes the current lineyy
copies the current line
Note: The above list is far from being comprehensive; we will keep learning Vim alphabets and their usage till the time we keep learning Vim. Similar to learning any new language (programming or otherwise), learning Vim keystrokes and their usage takes time. However, once mastered, it enables blazingly-fast editing, which cannot be achieved using any other editor.
Most of the normal-mode commands can be prefixed with a number n
to perform them n
times.
For example, 3dd
will perform the dd
command 3 times; thus, deleting 3 lines at a shot!
We will now learn some common usage patterns of the Vim alphabet using example scenarios. I strongly recommend you open your Vim and keep testing the same, in parallel.
Requirement | Command |
---|---|
Move 2 lines up | 2k |
Move 10 lines down | 10j |
Cut current letter | x |
Cut 5 letters | 5x |
Jump to next word | w |
Delete till next word | dw |
Delete 2 words | 2dw |
Delete current line | dd |
Delete 6 lines | 6dd |
Copy current line | yy |
Copy 5 lines | 5yy |
Change word | cw |
Delete till next k | dtk |
Delete including next k | dfk |
Change till next " | ct" |
Delete the word inside which the cursor is | diw |
Delete inside parentheses | di( or di) |
Change inside quotation marks | ci" |
Change including quotation marks | ca" |
Delete till ; | dt; |
Copy word | yw |
Copy 4 lines | 4yy |
Paste copied line below | p |
Paste copied line 5 times | 5p |
Replace till you press Escape | R |
As there can be an infinitely large number of sentences formed with English alphabet, so can be using the Vim alphabet. Keep practicing by asking yourself the question "Can I do this in Vim?", and then trying the combination that comes to your mind and observing the effect(s). This will be a truly rewarding experience.
Vim offers a visual mode to visually select regions of text, before applying a normal-mode command. For example,
vw
followed byy
selects till next word and copies selection.vt"
followed byd
selects till next " and deletes the selection.
There is also a convenience visual-line
mode that lets you select line-by-line.
For example,
V
(capitalV
) followed byy
selects the current line and copies it.V
followed by2j
followed byd
selects the current and the next two lines, and deletes them.
Further, there is a visual-block mode (triggered using <C-v>
) that lets select text vertically.
This is very helpful for deleting say a column in a table.
Just select the column visually and press d
.
Vim provides an easy way to toggle the case of typed text.
We can use ~
(tilde) to toggle the case of a single character, or select some text (using visual-mode) and use ~
to toggle the case of the selection..
To use numerical values for, say, moving k
lines down, we usually need to mentally count the number of lines.
However, in Vim 7.4+, there is a very useful option relativenumber, which can be set to explicitly see the line numbers relative to the current line.
To always use it, set the following in the vimrc file:
set relativenumber
To temporarily disable relative numbers in the current file, type:
:set norelativenumber
- You can scroll down and up in Vim with
<C-f>
and<C-b>
, respectively. - You can jump to the beginning and end of a line with
0
(zero) and the$
(dollar) keys, respectively. Do you think now thatD
(in the alphabet listing above) is actually a shorthand ford$
?
Vim provides an easy way to search content in the current buffer (well, searching across buffers is also easy; we will learn it in the next module). Type the following in normal mode:
/searchterm<CR> " search forwards
?searchterm<CR> " search backwards
* " search current word forwards
# " search current word backwards
After searching something, jump to the next and previous occurrences of the search-term using n
and N
, respectively.
Vim also provides a sophisticated mechanism to replace occurrences of a word in the current buffer:
:%s/oldword/newword/c " replace one-by-one
:%s/oldword/newword/g " replace all occurrences at once
To perform replacement within a block of text:
- First select the text using visual mode.
:s/oldword/newword/c
or:s/oldword/newword/g
Note that oldword and newword can even be regular expressions, thus opening a plethora of opportunities to ease searching and replacing.
The following option highlights all the occurrences of the searched word:
set hlsearch " highlight searches
The search highlights can be cleared using :nohlsearch
or :noh
.
There is one more interesting option related to searching in Vim:
set incsearch " highlight matches as you type
This option can be used to see which word(s) related to our search-term actually exist in the buffer.
We can paste text copied from outside Vim using the corresponding terminal emulator's keys for pasting:
Command-V
for macOS, and Ctrl-Shift-V
for Linux.
However, this may sometime randomly indent the pasted text (because of syntax-based indentation for the current file-type).
To avoid this, Vim provides a special paste mode that can be enabled in such scenarios:
:set paste " enable paste-mode
:set nopaste " disable paste-mode
I recommend you avoid setting paste-mode in your vimrc and use it only when you need (as paste-mode disables several indentation features). We will learn how to map keys to perform such small tasks in the next module.
The most powerful (yet simple) command in Vim is the repeat (or dot) command.
It can either be used to repeat the previous normal-mode command, or to repeat whatever you did in insert mode till you pressed the <Esc>
key.
For example,
5dd
-->.
will delete 10 lines./searchword
-->cw
--> enter new word --><Esc>
-->n
-->.
will change first word, and its next occurrence.
Notice how much the dot command simplifies the repetition. And we keep performing repetitive tasks all the time, isn't it?
In this module, we learned a plethora of tricks to make us adapt to the Vim way of editing text. Even though the learning curve is a bit steep, the joy that it will give you will be overwhelming. In the next module, we will tighten our belts and learn how the coder-you-envy writes code at the speed of thought using Vim. Till then, keep practicing.
Home | Previous module | Next module
Star this repository on GitHub if you like the tutorial.