Editing Text on the Command Line with vi

The discussion over which is the best command line text editor is one that has brought heated debate to the linux community since linux began. When I first started playing with linux I deftly managed to avoid both by using the nano text editor, which was very easy to use and did not require knowledge of various key commands. It is almost the standard text editor on Ubuntu servers, and besides using the normal, and familiar, arrow and page up/down keys to navigate, the only commands I found I needed to use were Save (Ctrl + O) and Exit (Ctrl + X).

When I began working with AIX Unix in my workplace, I found that nano was not installed and so had to learn how to use an alternative. vi (and now vim – ‘vi improved’) is the default text editor that comes with Unix. I found that learning a few basic commands enabled me to get started with it, and as time grew I was impressed with how it works. It has now become my go-to text editor on the command line.

vi Modes

While nano allows you to begin entering/editing text as soon as you launch it, vi has two modes to be aware of:

  • Command mode – the default mode, where keys cause actions to be taken on the file
  • Insert mode – the mode you enter to actually insert/edit text in your file

In Command Mode, when you press keys on the keyboard, you are performing actions on the file, rather than editing the file itself. These actions can be navigating around the file, copying or pasting text, entering Insert mode or saving the file.

In Insert Mode, the keys you press will be written to the file; you are editing the text in the file. To escape and go back to Command mode, press the Escape key.

Be aware that vi is case-sensitive, and uppercase keys do different actions to lowercase keys.

Below is a sample of the basic vi commands to get you started.

Getting In and Out of vi

At the command line, simply type vi and press enter. You can also add a filename. If that file exists in the directory you are in, vi will open that file. If the file doesn’t exist in the directory, vi will open a new file under that name

vi <filename> Run vi. If a filename is used, open that file, or create a new file under that name

When exiting vi, you have a choice on whether you want to save the changes you made to a file. You can save and quit, or quit without saving your changes. If you have been editing text, remember to press Escape first to go back to Command mode.

:w Save file (don’t quit/exit)
:wq Save and quit (exit)
:q Exit is not changes have been made
:q! Exit and undo any changes

Navigating Through a File

In online guides, a lot if mentioned about how to move around a file using the keys in vi. It is true that keys such as h, j, k and l can move your cursor up, down and left to right, but in the years that I have been using it, I have always found myself using the normal arrow keys for this.

Also, Page Up and Page Down work perfectly well also.

There are some useful keys for jumping around a long file though, and I think these are the best ones to focus on here. Again, these are all used when you are in Command Mode, so if you ever press a key and find it typing on the screen, you are in the Insert Mode. Delete the entry and press Escape

0 Jump to start of line
$ Jump to end of line
w Move the cursor forward one word at a time
b Move the cursor back one word at a time
1G Jump to line number 1, i.e. the top of the file. Jump to any line by using its line number
G Jump to the bottom of the file (the last line)

Editing Text

OK so now you need to start actually adding/editing text in your file. As vi starts in Command Mode, you need to enter Insert Mode to begin typing text. Once you are finished typing, press Escape to go back to Command Mode and save and/or exit

i Enter Insert Mode at the current cursor position
a Enter Insert Mode just after the cursor position
I Jump to the start of the line and enter Insert Mode
A Jump to the end of the line and enter Insert Mode
o Create a new line beneath where your cursor is and enter Insert Mode
O Create a new line above where your cursor is (move text down a line) and enter Insert Mode

To overwrite a single, or multiple characters:

r Overwrite/Replace a single character (no need to press Escape after)
R Overwrite/Replace multiple characters

To undo your last edit, press u. You can only undo your last edit, vi does not go back more than 1 edit. Pressing u again redoes the edit, i.e. undo the undo!

u Undo. Pressing this again redoes the last edit, i.e. it toggles the last edit back and forth

Leave Insert Mode:

Esc Go back to Command Mode, in order to save and/or exit the file

Deleting, Copying and Pasting Text

To delete text, use the following keys when in Command Mode:

x Delete a single character at a time (the character the cursor is over)
D Delete the rest of the line, everything after the cursor
dd Delete the entire line

To copy & paste text, use the keys below. For cutting a line of text, I tend to copy (yy) and then delete the line (dd):

yy Copy (yank) the current line
P Paste the copied line above the current line
p Paste the copied line below the current line

More Advanced Commands

Searching within files:

/<text> Search forwards through the file for <text>
?<text> Search backwards through the file for <text>
n When results are found, use n to jump forward through each result
N Jump backwards through each result

Note, the linux man pages use vi for displaying their content, so while you can’t edit/change the man pages, these same search commands can be used to search through the man pages.

Displaying Line Numbers:

:= Show the total number of lines at the bottom of the screen
:set number Turn on line numbering
:set nonumber Turn off line numbering

Running Linux commands from within vi

When you are running a command line based text editor, they use up the full screen. If you needed to run a linux command, to check the contents of a file or directory for example, you could either save and exit and then re-open your vi session, or use a screen session to have a second terminal window running, and switch back and forth between your vi screen and a normal command line.

There is a third option also, which allows you to run linux system commands from within the vi session. It might be useful to use the clear command to clear the results from previous commands from the results screen so that are cleared away and the display is not too cluttered.

:!<command> Run linux commands from within the vi session, e.g.
:!clear;ls –lhs ../

Recently I came across a guide to common vi commands which nicely captures the commands you will most commonly use. It is available at https://bootlin.com/doc/legacy/command-line/vi-memento.pdf.