Third party cookies may be stored when visiting this site. Please see the cookie information.

PenguinTutor YouTube Channel

vi Text Editor Tutorial

New users will probably get started with the nano editor. Nano is an easy to use editor, but others prefer the power and flexibility that vi provides. This guide is for those wanting to go beyond the simple editor and unleash some of the power that vi holds.

vi is a very powerful text editor. It runs in a standard terminal and uses the standard keyboard (it is not reliant on the arrow keys, or the insert, home, pgup keys). This makes it equally as useful on a graphics workstation as on a minimal text based terminal or indeed via a telnet terminal. Most linux distributions include the VIM editor (Vi IMproved), which has the same basic functionality as vi, but with some extra features. These Linux distributions normally include a link so that if you enter the command vi you will use the vim editor instead. The examples I give in this guide will work equally well in vi and vim.

To someone new to UNIX the vi text editor can be very daunting. However learning to use vi can be one of the most useful tools that a UNIX user can know. Whilst anyone with a X-Windows system may prefer the ease of use provided by the GUI based editors such as gedit, or kwrite you cannot always guarantee to have them available. Whereas regardless if you're working on a graphics or text only terminal almost all UNIX and Linux systems will have the vi editor installed. Once you know the basics for vi you can use these in other circumstances, for example the korn shell can be setup to have the vi keys for command recall and edit or the same keys can be used to scroll through a file using the more command.

Rather than just describe what all the different keys do I've worked through a little example of creating and editing a text file. First it is important to understand about the different modes of the vi editor.

Editor Modes

If you have not used vi before, then having to switch between the different modes can seam a bit alien at first, however is actually quite simple in operation. The most common two are "Insert mode", and the "Command mode".

Whenever you start vi it will be in command mode. In this state whenever you type something, instead of being included in the file it will be used as a command for the editor. Example commands include moving around a document, deleting parts of the document, and file operations such as saving the document and exiting.

To add the text that you type in at the keyboard requires you to be in insert mode. When in insert mode any text entered at the keyboard is added to the document at the current cursor position.

To switch between the different modes involves pressing the appropriate keys. To move to command mode you would press the ESCAPE key. A useful feature of the ESCAPE key is that it still works when in command mode. Indeed if you forget which mode you are in just press the ESCAPE key and regardless it will put you in the command mode.

There are several different keys that allow you to go from the command mode to the insert mode. By using the appropriate key, the number of keystrokes required can be reduced. Some of the keys are listed below:
i Insert - any text entered will be put immediately before the current letter.
a After - any text entered will be put immediately after the current letter.
A After end of line - any text entered will be put after the end of the current line.

It is also possible to run ed commands by pressing colon ':' when in command mode. The ed commands are based on command line editing used by the "ed" command line editor. The vi editor is based on the ed program which is a basic command line editor.

Worked Example with VI

Following this example will introduce a lot of the functionality of the vi editor.

Starting vi

To start the editor enter vi followed by the filename.

vi sjayouth.txt

If the file already exists then it will be opened and displayed on the screen. If it is a new file then the initial screen will be blank. The editor is started in command mode. The file opened is shown below. It is an article from a St. John Ambulance Newsletter that I wrote in September 2000:

Mention St. John Ambulance and most think of adults trained in first aid

helping those in need. However St. John actually has one of the biggest youth 

organisations in the country, catering for all children from the age of 6 

upwards.



The youth area is split into two age groups. Badgers are for children from 

6 to 10 and Cadets are for members aged from 10 to 16. Both groups are open 

to boys and girls.



Badgers make friends and have fun. The badgers work towards 9 different badges 

in areas from first aid to hobbies. After gaining all 9 badges he / she can 

become a Super Badger.



Cadets play games and learn first aid, like the badgers. Cadets then get to 

practice the first aid on real casualties. There are adult members ready to 

give a helping hand whenever it's needed. The Cadet's work on an award scheme 

leading up to the Grand Prior Award.



For more information see www.sja.org.uk

Inserting a New Line

We will now add a line before the text with the title "Young Members". The cursor is located in the top left hand corner, which is exactly where we want to insert the text. When we started the editor we were in command mode so we now need to change to insert mode by pressing:

i

We can then type in the title

Young Members <Enter> <Enter>

Pressing the enter key moved the following text to the next line and then inserted a blank line.

Moving Around the File

Now imagine that I wanted to replace the word "practice" with "use". To move around the file it may be possible to use the cursor keys. Some terminals do not support the cursor keys and the alternative is to use four of the standard keys. These are
h - left
j - down
k - up
l - right
To use the standard keys as cursors requires that you are in command mode.

To move to the word "practice" first press
ESCAPE
to change to command mode.

Once in command mode then press the 'j' key until the cursor is next to the line containing the word. Then subsequent presses of the 'l' key will move until the cursor is on they letter 'p' of practice.

Deleting Letters and Inserting a New Word

Now press the 'x' key, which will delete the letter under the cursor. Press they 'x' key 7 more times until the word has been deleted. Then press the 'i' key to return to insert mode and enter the word "use".

Save the Document

We will now save the file in case we accidentally make a mistake in future. To save the current document you need to change to command mode (ESCAPE). Then press the colon key (:) to create a command line and enter 'w' followed by the enter key.

The file will then be saved.

Replacing a Letter

Prior to the introduction of the children's act cadets would become adults at the age of 16, this age is now 18. Now we shall change the age from 16 to 18. We shall move the cursor and replace the figure 6 with the figure 8.

First press ESCAPE and use the 'k' key to move up the document up to the line containing the figure 6. Then move across the line using the 'h' and 'l' keys until the cursor is over the figure 6.

To replace the character press the 'r' key. Then pressing the '8' key will cause the number to be replaced with the letter 8. You will still be in command mode.

It would have been possible to have replaced multiple letters by using an uppercase letter 'R'. Instead of returning to command mode after a character has been replaced, it will continue to replace characters until the ESCAPE key is pressed.

Search and Replace

For this exercise we will now replace the occurrence of 18 back to 16, however this time we will do it using a ed command. We can find the occurrences of a word by using a regular expression. To search for 18 we would enter /18 followed by enter. This would search from the start of the file until it reached the first instance of 18.

If instead we wanted to replace 18 with 16 directly then the command that would be entered is :%s/18/16/g
This looks a bit overwhelming at first, but broken into the separate components it is fairly straight forward.
The colon tells the editor that what follows is an ed style command.

The next character(s) are used to indicate what data the command should be run against. In this case the percent sign (%), indicates that the current buffer is to be used (the whole file currently being edited). Another format often used is x,y (where x is the start and y is the end line number), which will only perform the operation between the chosen lines. The % could be typed as 1,$ instead, which will perform the action on all lines from the first one, until the last ($). The percent sign is 2 characters less, so we'll stick with that for now.

The next character indicates what command is to be run. In this case s indicates a search and replace command (this is the same as the letter s used in a sed search and replace syntax, which is also used by perl and some other programming languages).

The search and replace is now seperated between three '/' characters. The text between the first and second '/' character is the text that is to be searched for, and the text between the second and third '/' characters is the text that it will be replaced with. So in this case the /18/16/ indicates that 18 will be replaced with 16.

Finally any letters following the third '/' character are options that change how the search and replace will be performed. In this case the 'g' option makes the command work globally for every possible occurrence. Another common option is the 'i' character which indicates that the case will be ignored (although obviously not required in this case as we are using digits).

This may also replace other parts of strings such as 180 would be replaced with 160. So it is important to ensure that the command that is issued is what you really want it to do. There are other options that can be used inside of the search and replace parts of the the command, but these are beyond the scope of this guide. See the further reading section for more details.

Save and Exit

Now we will save and exit the document. Press the ':' key followed by 'w' (to write - save) followed by 'q' to quit the program.

This is then end of this example.

Repeating Actions

Another feature of vi is the ability to save typing by asking vi to repeat a command a number of times. This is done by prefixing the command character with the number of times we would like the command to be performed.

In the worked example above, we pressed the 'x' character 8 times when we wanted to remove the word practice. We could have saved some six key-presses by entering '8x', instead of repeatedly hitting the 'x' key.

There are also some other functions that can speed this up, for example we could have (in command mode) used the letters dw instead. The d indicates to delete, and w indicates we want to delete a word. You could also use d$ to delete to the end of the line, or dd to delete the whole line. Combining the repeat actions, with this we could have entered 4dd to delete four lines.

These are just a few examples of what can be achieved. The further reading section gives information where you can learn more about the different commands and shortcuts available in vi.

Preferences

Within vi it is possible to change some of the settings. These are done by issuing a ':' followed by the set command with the option as a parameter. For example to display line numbers in the editor you would issue the command:
:set number
This could then be turned off again by using the word "no" in front of the options. So to turn the number option back off you would issue:
:set nonumber

Other options that can be set include:

all		Display all options & current settings

errorbells	Sounds a bell sound whenever an error occurs

ignorecase	Makes search commands case insensitive

showmode	Shows the current mode in the bottom of the screen

These settings only last as long as the current session. To have these commands run whenever vi is run then the required commands should be put in a file called .exrc in your home directory. These should be entered without the colon e.g.

set number

set showmode

Previous Linux useful commands
Linux useful commands
Next Linux links reference guide
Linux links reference guide