Text Editing With Vim / Vi (Linux)

Vi and VIM

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:

vi

generally launches Vim, as the vi command is symbolically linked to launch vim.

geektechstuff_vi_1
Vi and VIM

Modes

vim is a modular text editor with a command mode and an edit mode.

Command 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.

Moving Around

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

Searching

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.

/SEARCH_TERM
geektechstuff_vi_search_1
Using the /search function to search for main in Vi

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

Deleting

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

 

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.