VI/VIM, the basics.

Angel Ruiz
3 min readMar 7, 2021
Photo by Florian Olivo on Unsplash

VI is a powerful and light Unix text editor, although is considerably difficult to start using it and grasp all the features that you could use to improve your productivity.

VI has two modes called insertion and command mode. Every time you start working with VI the command mode is where it begins, in this mode navigation through the file, text or line deletion, pasting, searching and other features are available, through the use of commands. The insertion mode starts when an insertion or change command is used, and gives us the ability to start entering text. To exit insert mode ESC key needs to be pressed and we return to the command mode.

Start

To start vi, open your terminal and just type the command below. If the file exists vi will open it, if not it will create a new file for you.

$vi filename

Input commands.

These let you enter insertion mode to modify the files, use ESC to exit this mode.

i - Insert before cursor 
a - Insert after cursor
o - Add blank line after cursor
O - Add blank line before cursor

Change commands.

As the name suggested these are used to change text in the files, the majority of these will perform the action and then you enter into insert mode.

cw - Change word
cc - Change complete line
s - Substitute char
rT - Replace character with T (does not enter insert mode)
>> - Shift text to the right
<< - Shift text to the left

If you are in insert mode and want to undo the changes

<ctrl> u — Undo all changes from beginning of insert (only applies to one line)
<ctrl> w — Undo last typed word

File commands.

Use these commands on tasks related with the management of the file. Use them in command mode.

:w name - Write to file name
:wq - Write and quit.
:q! - Quit without writing changes

Navigation commands.

These let you navigate through your file.

arrow keys - Move down and up one line, move forward and backward one character
<ctrl> d - Move down half of screen
<ctrl> u - Move up half of screen
<ctrl> f - Page forward
<ctrl> b - Page backward
G - Go the last line of file
:n - Go to n line
H - Go to left top corner
M - Go to middle line
L - Go to left bottom corner
$ - Go to the end of line
w - Move one word formard
b - Move one word backwards
0 - Move to the start of line
% - Move to matching character( useful with ({[]}) )

Deletion Commands.

These commands do not enter insertion mode. Makes a deletion quicker than using insertion mode.

dd - Delete line
dw - Delete word
D - Delete to the end of line
x - Delete character

Undo commands.

U - Undo all changes on line
u - Undo last change

Copy/paste commands.

yy  - Copy a line
yw - Copy a word
y$ - Copy to the end of line
p - Paste after cursor
P - Paste before cursor

Selecting text.

In visual mode command you can mark lines, words and then apply a command like copy or shifting.

v - Enter visual mode
V - Linewise visual mode
<ctrl> V - Block visual mode

If you want to use shift command in visual mode only a > or < will be enough, do not use >> or << .

Search / replace.

/string - Find text string moving forward
n - After using forward search command , find next string match
?string - Find text string moving backwards
N - After using backward search command , find next string match
:%s/toreplace/word/g - Replace every toreplace match with word

Other commands.

:r file - Import a file into current file
:set number - Show line numbers
:set nonumber - Do not show line numbers
:set ic - Ignore case sensitivity on pattern matching
:set noic - Turn back on case sensitivity

This is only a short list of all the commands that I most frequently use in vi, but there are more advanced stuff you can do ( searching with regular expressions, markers, execute shell commands within vi, open two vi instances in same terminal, customizing the environment). Hope this list will help you in your first encounter with vi o as a reference to became more agile when using it.

--

--

Angel Ruiz

I write about software development, AI, ML and other stuff that I find interesting. || Software Developer and AI-ML enthusiast