Introduction
A range of simple editors, such as nano, are available for beginners. But, the real "Linux" users use VIM (don't cancel me, thanks).
Vim is the "modern descendant" of Vi and stands for Vi Improved. Vi was developed in the 1970s by Bill Joy, and Vim came into existence around 31 years ago. Despite its age, Vim remains a top choice of editor among many programmers and has a loyal following. It comes pre-installed on all Linux machines.
Vim is a powerful editor with many commands, too many to explain in a blog such as this. This blog is designed to describe enough of the commands that you will be able to use Vim as an all-purpose editor easily.
Who should read this blog?
- Programmers who want to begin using VIM.
- Programmers/Linux users can use this article for revision and as a cheat sheet.
- Myself
Why use VIM?
- Vim arrives pre-installed on any Unix or Linux box, from tiny IoT devices to supercomputers. The Single Unix Specification and POSIX require it.
- When you
ssh
into a machine at work, VIM will sometimes be amongst the only choices available for an editor (given that you are not allowed to install other packages). - It can be intimidating initially but eventually boosts productivity.
- It is the holy grail for Linux users. You cannot head to an interview and say that you don't know how to exit VIM.
Setup
Check the version of
vim
installed on your machine, type in the following command:vim --version
Download the book "Treasure Island" as a text file from this link (or anywhere else). We will use this file to experiment with the commands, making it easier to follow along with this blog.
Open up the file in Vim:
vim treasure-island.txt
Note: You need to execute the commands to learn them properly. If you only read the text, you will forget the commands!
Let's Go:
- There are two "modes" - with very different behaviours
- Little or nothing on screen lets you know which mode you're currently in!
The two modes are "normal mode" and "insert mode", and as a beginner, remember:
Press Esc twice or more to return to normal mode
The "normal mode" is used to input commands, and the "insert mode" for writing text - is similar to a regular text editor's default behaviour. There are other modes too, which we will look at later.
Basic Editing
Moving the cursor
The cursor can be moved using the arrow keys. But the suggested way to go is to use the
hjkl
keys. Hints:- The h key is at the left and moves left.
- The l key is at the right and moves right.
- The j key looks like a down arrow.
But why does Vim use the
hjkl
keys for navigation? You can read about it at this link.Moving word-by-word:
- You can use
w
to move ahead by one word to the start of the next word. - Also, use
e
to move to end of the next word. $
to move to the end of the current line.- To move to the start of the line use a zero:
0
Exiting Vim
First make sure that you are in the "normal mode" by pressing
<ESC>
twice or more times (you might hear a beep sound).- To trash all changes - Type
:q!
and press<ENTER>
. This exits the editor, DISCARDING any changes you have made. - To save the changes - Type
:wq
and press<ENTER>
- To trash all changes - Type
Text Editing - Deletion
If you exited Vim in the previous step, re-open the file by running
vim treasure-island.txt
.Move the cursor until it is on top of the character to be deleted and press the x key to delete the unwanted character.
Text Editing
- Insertion - Move the cursor on top of the character BEFORE which the text is to be inserted. Now, press
i
and type in the necessary additions. - Appending - It does not matter on what character the cursor is in that line, press
A
(note: capital A) and type in the necessary additions. This will add take the cursor to the end of the line and shift to the insert mode.
TypeTo insert or append text type: i type inserted text <ESC> - insert before the cursor A type appended text <ESC> - append after the line
a
(lowercase) to append text AFTER the cursor. - Open Command - Type the lowercase letter
o
to open up a line BELOW the cursor and place you in Insert mode. TypeO
to open a line ABOVE the cursor.
- Insertion - Move the cursor on top of the character BEFORE which the text is to be inserted. Now, press
Deletion Commands, Operators and Motions
Deletion Commands
1. To delete from the cursor up to the next word type: dw 2. To delete from the cursor up to the end of the word type: de 3. To delete from the cursor to the end of a line type: d$ 4. To delete a whole line type: dd
Operations & Motions
Many commands that change text are made from an operator and a motion. The format for a change command is:
- operation - is what to do, such as d for delete
- number - is an optional count to repeat the motion
- motion - moves over the text to operate on, such as w (word), e (end of word), $ (end of the line), etc.
A short list of motions:
- w - until the start of the next word, EXCLUDING its first character.
- e - to the end of the current word, INCLUDING the last character.
- $ - to the end of the line, INCLUDING the last character.
operation number motion Example: d2w - delete 2 words Here, d - operation 2 - number w - motion
Undo and Redo commands
- To undo previous actions, type: u (lowercase u)
- To undo all the changes on a line, type: U (capital U)
- To undo the undo's, type: CTRL-R
Replace, Put & Change Operations
The Put command (
p
)When you delete a line with
dd
, you can put back text that has just been deleted, by typingp
. This puts the deleted text AFTER the cursor (if a line was deleted it will go on the line below the cursor). It can also be used to put(paste) copied text.The Replace command (
r
)To replace the character under the cursor, type r and then the character you want to have there. For example, type
rb
to replace the character at the cursor withb
.The Change operator (
c
)The change operator allows you to change from the cursor to where the motion takes you. eg. Type
ce
to change from the cursor to the end of the word,c$
to change to the end of a line.Notice that
ce
deletes the word and places you in Insert mode.cc
does the same for the whole line.The change operator works in the same way as delete. The format is:
c [number] motion
The motions are the same, such as
w
(word) and$
(end of line).
More Navigation, Searching and Substitution
Navigation
CTRL-G
displays your location in the file and the file status.G
moves to the end of the file.number G
moves to that line number.gg
moves to the first line.
Searching
Typing
/
followed by a phrase searches FORWARD for the phrase.Typing
?
followed by a phrase searches BACKWARD for the phrase.After a search type
n
to find the next occurrence in the same direction orN
to search in the opposite direction.CTRL-O
takes you back to older positions,CTRL-I
to newer positions.
Matching Parentheses Search
Typing
%
while the cursor is on a (, ), [, ], {, or } goes to its match.The Substitute Command (
s
)- To substitute new for the first old in a line type
:s/old/new
- To substitute new for all 'old's on a line type
:s/old/new/g
- To substitute phrases between two line #'s type
:#,#s/old/new/g
- To substitute all occurrences in the file type
:%s/old/new/g
- To ask for confirmation each time add 'c'
:%s/old/new/gc
- To substitute new for the first old in a line type
Copy & Paste, Another method to Replace, Setting options
Replace text - Typing a capital
R
enters Replace mode until<ESC>
is pressed.Copy and Paste text -
- Start Visual mode with
v
and move the cursor and select the portion that needs to be copied. - The
y
operator yanks (copies) text,p
puts (pastes) it.Note: The visual mode can be used with the
d
command to cut text and then paste it usingp
. Typing ":set xxx" sets the option "xxx". Some options are:
- 'ic' 'ignorecase' ignore upper/lower case when searching - 'is' 'incsearch' show partial matches for a search phrase - 'hls' 'hlsearch' highlight all matching phrases
You can either use the long or the short option name.
- Start Visual mode with
Conclusion
This is as much you'll need to get started with using Vim as an all-purpose editor, but there's a lot more that you can do with it.
Remember the three basic tricks for a newbie to vim
:
Esc Esc
always gets you back to "normal mode"- From normal mode
:q!
will always quit without saving anything you've done, and - From normal mode
u
will undo the last action