You may already get tired of the nano text editor. It’s simple but not efficient nor sufficient. So we are introducing Vim – Vi Improve.
Vim is a powerful and highly customisable text editor. It’s based on and an improvement of Vi, another text editor included in Raspbian. The trade off for power is of course, the cost to learn. So I didn’t put this as a basic level of the tutorial, but as an advance.
To install vim, like all other packages:
To start:
Like nano, if file exist, vim will open that file, otherwise a new file will be created.
Directories are also a special file in Linux. However nano cannot open them but vim can. you can try to open a directory and see what happens. It acts as a document tree!
Now open a blank file and we’ll go though some basic of vim:
At the bottom we can see the name of file and it’s a new file, then two numbers 0,0-1, that’s the position of the cursor (line number 0, Position in the line 0). We ignore the All at the moment.
Vim as two modes, command mode and insert mode. The default is command mode which treat all keyboard input as commands. To enter insert mode to make direct changes, we need to switch to insert mode first by hitting i.
Now INSERT should be appear at the lower left and all inputs are actually recording. Exit insert mode by hitting esc, and the INSERT would disappear, means vim is switched back to command mode.
To save the file and exit vim, enter :wq under command mode.
Here are more commands that are often used, all command are used in command mode only and they are case sensitive:
Enter Insert Mode:
i – insert before the cursor
a – append, insert after the cursor
I – insert at the start of the line
A – Append, Insert after the end of the line
o – start a new line below and insert
O – start a new line above and insert
s – delete the cursor character and insert
S – delete the line and insert
r – replace cursor character
R – replace mode (insert mode but replace)
Move the Cursor and Navigate:
In insert mode, arrows are the only way to navigate, but there are a lot more ways in command mode apart from the arrows. Arrows can be repeated for a number of times (jump) if there’s a number before it, eg. 3h – move 3 characters to the left.
h – left arrow
j – up arrow
k – down arrow
l – right arrow
gg – go to start of the file
G – go to end of the file
<Line Number> G – go to line
0 (zero) – go to start of line
$ – go to end of line
% – find match bracket
. (dot) – repeat last command
Cut, Copy, Paste and Delete:
In vim, all deletes act as cut, saved to a buffer and can be paste later. Most of these commands can be repeated for a number of times if there’s a number before the command, eg 12x – delete next 12 characters, 3Y – copy next 3 lines.
x – delete (cut) the character under the cursor
X – delete (cut) the character before the cursor
d<h/l> – delete (cut) the character under/before the cursor, equivalent to x/X.
d<j/k> – delete (cut) the line above/below.
dd – delete (cut) the current line
y<h/l> – yank (copy) a character before/under the cursor
y<j/k> – yank (copy) the line above/below
Y – yank (copy) the current line
p – paste after the cursor (or new line below)
P – paster before the cursor (or now line above)
Undo and Redo:
u – undo
U – undo current line only
ctrl + r – redo
Find and Replace:
Find and Replace are also case sensitive. If a special character need to be enter, have a \ before it, eg. to find all !=, enter /\!\= instead of /!=
/<string> – find <string> (from top to bottom)
?<string> – find <string> in reverse order (from bottom to top)
n – next match
N – previous match
:s/<old>/<new>/g – substitute <old> with <new> in the current line
:%s/<old>/<new>/g – substitute <old> with <new> in the whole file
Open, Save, Quit:
These commands can be combined. Start with : (column).
:w – save file
:w <filename> – save as <filename>
:o/:e <filename> – open/new file
:q – quit (exit) vim
! – override warnings (ignore all warnings, normally used with :q! – quit without saving)
:! <command> – execute command using system shell
:n – next file (if open multiple files in shell)
:N – previous file
:help – help file
Here’s a cheatsheet for all commands from GitHub: Vim Cheat Sheet
Apart from these powerful commands, vim setting file also make an important role:
As a beginner, there lines are recommended:
syntax on
set nocompatible
tabstop=4
shiftwidth=4
set autoindent
set smartindent
If you prefer line number:
If you want to use your mouse pointer to navigate:
Save and exit, and open vim again to see changes.
You may seek more vimrc command to build your personal vim.
Last but not least, vim provides many useful plugins that are awaiting for you to discover!