Using the Vim text editor in Linux
Vim is a very powerful editor with a huge set of features. This article covers the basic commands, which are enough to get started in vim.
It is very important not just to read the article, but in parallel to try all the actions on some test text file. To do this, create a text file and open it with the vim editor:
$ cd /tmp cp /etc/passwd /tmp/test vi ./test
There are two modes of the editor: edit mode and normal mode. The transition to the editing mode is carried out by various teams, which will be discussed further. Exit edit mode – <Esc>button
Move cursor commands
Move commands work normally. Press <Esc> to make sure you are in it.
The cursor can be moved with arrows on the keyboard or, much faster, using the h, j, k, l keys:
h – to the left
j – down
k – up to
l – right
other movement commands:
0 – Go to the beginning of the line
$ – Go to the end of the line
w – go to the beginning of the next word
e – go to the end of the word
gg – go to the beginning of the document
G – go to the end of the document
number G – go to the line number < number>
You can use the link <number> <move command>
3w – go to the beginning of the fourth word, counting from the current position
4e – go to the end of the fourth word, counting from the current position.
x – delete the letter under the cursor
number x – delete <number> letters, starting with the one under the cursor
dd – delete the line
number dd or d number d – delete <number> lines starting with the cursor position
You can combine delete and move commands :
dw – delete 1 word starting from the current position
d5w – delete 5 words
d $ – delete the line from the current cursor position to the end of the line
d0 – delete the line from the current cursor position to the beginning of the line.
In order to save the edited file you need:
- return to normal mode by pressing <Esc>
- enter the command : write <filename> <ENTER> ,
- or its shortened version : w <filename> <ENTER>
If no filename is present, the current file is saved.
To exit the editor, enter: quit <ENTER> or simply: q <ENTER>
Exit without saving changes-: q! <ENTER>
:wq – save changes to the current file and exit.
To switch to insert mode, press <i> .
A – go-to end of the line and start insert mode
a – go-to next character and start insert mode
o – start pasting under the current line
O – start pasting above the current
<Esc> – go to normal
Undo recent changes
u – undo last change
U – undo changes in the entire line
<CTRL-R> – undo undo.
Copy and Paste
yy – copy the entire line to the clipboard
p – paste the contents of the clipboard after the cursor
y5y or 5yy – copy 5 lines
yw – copy the word
The newly deleted text is also copied to the clipboard. For example, if you delete the string dd, then immediately after that you can insert it with p
r – replace the character under the cursor
R – replace the characters starting from the current position
c <number> <move command> – another replacement command.
cw – change the word.
c $ – change the line from the position of the cursor to the end.
cc – change the entire line.
/ – search in the forward direction
? – search in the opposite direction
n – go to the next found element
N – go to the previously found element.
Find the matching bracket],),}
Move the cursor in normal mode to the opening/closing parenthesis and press %. The cursor will go to the corresponding closing/opening bracket if any.
Positioning in file
To determine the current position, press <CTRL-G> in normal mode. A line will appear at the bottom of the terminal, containing information on the number of lines, current position, file name.
: s / old / new / g – replace all combinations of ‘old’ in the current line with ‘new’
: 10,20s / old / new / g – the same, but for lines from 10th to 20th
: help <command> <ENTER> – call the editor command help.
Execution of shell commands
! ls – execute the ls command in the current directory
: r <filename> – insert the contents of the filename after the cursor
Some editor settings can be changed without leaving the editor itself using the set command.
set ic – ignore case – ignore the case of letters (for example, when searching)
: set hls – highlighting search – highlight the match found during the search
: set nohls – do not highlight the match found during the search.
The script that is executed when running the vim editor is located in the user’s home directory and is called .vimrc.
In it you can set some additional settings, for example:
colo evening filetype plugin on set grepprg="grep -nH $*" filetype indent on
Examples of this file are supplied with the distribution kit of the editor.
Detailed information can be obtained from the online help using the command: help or on the man vi manual pages.