Main commands of VI / VIM text editor

The text editor vim, created on the basis of the older vi. It is one of the most powerful text editors with the full freedom for configuring and automation that are possible thanks to extensions and add-ins. By default, it is included in a scope of any Linux distribution.
Open a file using  vi / vim:

vim test.txt
vi test.txt

Main operation modes

“Standard mode” — movement in a file, text deletion and other editing features. This is the main mode, which is used for immediate switching to other modes. In order to return to the main mode from any other mode:

<ESC>, sometimes two times;
<Ctrl-[>

 “Input mode” - text input. After the text is entered, the standard mode is immediately returned. Please, note that the text is deleted and entered in two different modes. Switch to it from the standard mode:

i
<Insert>

“Command mode” - Commands (operations with a file, search and replacement, editor configuring…). Switch to it from the standard mode:

:

“Search mode” - input of a search request. Switch to this mode from the standard mode

/ ,  Search forward for (N-th) matching line
? ,  Search backward for (N-th) matching line

 “Visual mode” - text highlighting mode:

"v" and right or left arrows;
Shift+v mark line;
Ctrl+v rectangle, mark part of the text

Movement in a file

After loading of Vim, a part of the downloaded text file will be displayed on the screen. After loading, Vim is in the “command mode”, which is one of main modes. It means that if you press on <l> button (lowercase L), you will see that a cursor is shifted on one character to the right instead of displaying “l” in the cursor place. In the command mode, characters, entered using the keyboard, are used as commands for Vim, but not as characters, placed in the text. Movement commands are one of the most important command types. There are several of them:

For the cursor movement press keys h,j,k,l as shows below:
               ^
               k
           <h     l>
               j
               v

<Ctrl-f> - Forward  one window
<Ctrl-b> - Backward one window
<Ctrl-d> - Forward  one half-window
<Ctrl-u> - Backward one half-window
<Ctrl-y> - Backward one line
<Ctrl-e> - Forward  one line
0 (zero) - Move cursor to start of line
$        - Move cursor to end of line
w        - Move cursor right one word
b        - Move cursor left one word
W        - Move cursor right to the space after word
B        - Move cursor left to the space before word
gg       - go to the start of a file
G        - positions at the end of the file
<n>G     - Cursor goes to the specified (n) line
/<text><CR> - search for a "text"
?<text><CR> - search backwards for a "text"
n        - find the next occurrence of the same string
N        - repeats the last search the opposite direction


Text input

The followinf commands switch the editor into the input mode:

i — Insert text before the current cursor position
a — Append text following current cursor position
I — Insert text at the beginning of the cursor line
А — Append text to the end of current line
o — Open up a new line following the current line and add text there
O — Open up a new line in front of the current line and add text there
s <n> - Change one character
S - Change a whole line
R — Begin overstrike or replace mode. Use ESC key to exit
r — Replace one character at the cursor position

Deletion and insertion

The main text deletion and insertion commands are listed below:

x — Delete character under the cursor
X — Delete character left of the cursor
d — command may be followed by any motion command, and it deletes from the current location to the place where the cursor winds up. For example:
d4w - deletes four words
diw - delete word arrownd cursor
dd — delete a whole line
d<n>d или <n>dd — deletes n line
db - delete word backward
d0 - deletes character from the begining to cursor position
d$ or D - Delete to end of the line
с — The change operator
сс - changes a whole line
C - Change to end of the line
yy (or Y) — yanks current line (VI's copy commmand)
y<n>y — copy "n" lines from current to the clipboard
p — paste below cursor
P — paste above cursor
J — Join next line down to the end of the current line


Change cancellation

u — Undo last change
U — Restore line


Search

Go to a line:

/abcdef - Find the first string "abcdef"
n - Find the next "abcdef"
N - Find the previous "abcdef"


Exit

There is a pair of commands, which you should know:

:q! - Quit (no warning)
:wq - Write file to disk and quit the editor

Main features, required for operation, are described in this article. Almost all Linux distributions are described in the manual on operation with the editor. Just enter command vimtutor in the command line.

Help

In order to call the help, containing the information about the editor, enter the following command in the terminal:

man vim