When it comes to text editors in Linux there are varying opinions on which to use, and just like the spaces vs tabs discussion this can lead to arguments. I started out and still use nano as my Linux text editor, however today I want to look at Vi.
vi or vim?
Vi was created by Bill Joy in 1976, deriving its name from the word visual as it is a visual editor rather than a line editor which was more prevalent at the time.
Vim was created by Bram Moolenaar and released in 1991. Originally Vim was short for Vi Imitation, but later it became short for Vi Improved.
In current Linux systems typing:
generally launches Vim, as the vi command is symbolically linked to launch vim.
vim is a modular text editor with a command mode and an edit mode.
By default vim opens in command mode. In command mode the keys enter commands (not characters!).
To return back to the command mode from another mode press esc
Insert (Edit) Mode
This is the mode where the characters within a file (e.g. the text) can be typed. Characters entered in this mode appear before the cursor.
To enter Insert mode press the i key whilst in Command Mode.
Append (Edit) Mode
This mode is very similar to Insert mode, except that in append mode the typed characters appear after the cursor (instead of before).
To enter Append mode press the a key whilst in Command Mode.
Navigating a file is done in Command Mode with either the four arrow keys of the keyboard, or with the letter keys:
- h (same as left arrow key)
- j (same as down arrow key)
- k (same as up arrow key)
- l (same as right arrow key)
The above are all lower case letters h, j, k and l.
If you want to jump around a file then there are shortcuts available:
SHIFT H Jump to first line SHIFT G Jump to last line w Move forward by one word b Move back by one word 0 (zero) Jump to beginning of the line $ (dollar) Jump to the end of the line
In command mode typing / followed by a search word and then return (enter) searches the file for that search word and jumps the cursor to where it is.
Saving / Exiting
Saving and / or exiting is done from the command mode with the following commands:
:q quit :q! quit without saving (discard changes) :w write file (save) :wq write file (save) then quit
There are several options for deleting characters in a file all entered via the command mode:
d1G deletes from cursor position to beginning of file dG delete from cursor position to end of file dd delete the current line dw delete the word following the cursor X delete the character to the left of the cursor x delete the character under the cursor