Last updated on:

The VIsual Editor

Presentation

The VIsual Editor (a.k.a. VI) is a powerful text editor that operates in three basic modes :

Command

Ex-mode

Insert

In Command mode it is possible to use keys such as i to insert text.

In Ex-mode mode, all commands sent to VI must be preceded by the : key.

In Insert mode VI can be used to :

edit text,

add text,

search for text,

copy text,

paste text,

cut text,

replace existing text.

Creating, Opening and Closing files with VI

Commands

Command	Description
vi filename	Edit filename starting at line 1
vi	Edit a new file starting at line 1
vi -r filename	Recover filename that was being edited when system crashed
view filename	Open filename in read-only mode
:wq	Quit vi, writing out modified file to file named in original invocation and modify the mtime
:x	Quit vi, writing out modified file to file named in original invocation without modifying the mtime if the file was not modified
:q	Quit (or exit) vi if the file was not modified or quit view
u	Undo last command
U	Undo the modifications to the current line
:q!	Quit VI even though latest changes have not been saved

LAB #1 - Creating a new file with VI

Copy lines 1 through 25 below:

This is line 1
This is line 2
This is line 3
This is line 4
This is line 5
This is line 6
This is line 7
This is line 8
This is line 9
This is line 10
This is line 11
This is line 12
This is line 13
This is line 14
This is line 15
This is line 16
This is line 17
This is line 18
This is line 19
This is line 20
This is line 21
This is line 22
This is line 23
This is line 24
This is line 25

Create a new file called vitext using vi:

[trainee@centos7 ~]$ vi vitext

trainee@debian8:~$ vi vitext

trainee@ubuntu1604:~$ vi vitext

trainee@SLES12SP1:~> vi vitext

To do - Use the i key to change to insert mode. If you are using a terminal undre Linux to connect to your virtual machine, click the center button (wheel) of your mouse to paste lines 1 through 25. If you are using putty under Windows™ to connect to your virtual machine, click the right button of your mouse to paste lines 1 through 25. Hit the Escape key to move to Command mode. Now hit the : key followed by the X key to save the file and quit vi.

LAB #2 - Opening a file in read-only mode using view

Now open /home/trainee/vitext in read-only mode:

[trainee@centos7 ~]$ view vitext

trainee@debian8:~$ view vitext

trainee@ubuntu1604:~$ view vitext

trainee@SLES12SP1:~> view vitext

You will obtain a result similar to the following example:

This is line 1
This is line 2
This is line 3
This is line 4
This is line 5
This is line 6
This is line 7
This is line 8
This is line 9
This is line 10
This is line 11
This is line 12
This is line 13
This is line 14
This is line 15
This is line 16
This is line 17
This is line 18
This is line 19
This is line 20
This is line 21
This is line 22
This is line 23
This is line 24
This is line 25
~
"vitext" [readonly] 25L, 391C

To do - Re-size your window so that you can see just the 25 numbered lines.

Important - Note the [readonly] statement on the last line.

LAB #3 - Opening a file in read-write mode using VI

Quit using the :q command and open the file in read-write mode :

[trainee@centos7 ~]$ vi vitext

trainee@debian8:~$ vi vitext

trainee@ubuntu1604:~$ vi vitext

trainee@SLES12SP1:~> vi vitext

You will obtain a result similar to the following example :

This is line 1
This is line 2
This is line 3
This is line 4
This is line 5
This is line 6
This is line 7
This is line 8
This is line 9
This is line 10
This is line 11
This is line 12
This is line 13
This is line 14
This is line 15
This is line 16
This is line 17
This is line 18
This is line 19
This is line 20
This is line 21
This is line 22
This is line 23
This is line 24
This is line 25
"vitext" 25L, 391C 25,1 All

Important - Note that VI is launched in Command mode. Note that there are 25 lines and 391 characters.

The set Command

Commands

Command	Description
:set nu	Turns on line numbering
:set number	Turns on line numbering
:set nonu	Turns off line numbering
:set nonumber	Turns off line numbering
:set ic	Turns on independent case searching
:set noic	Turns on case dependent searching

LAB #4 - Turning on line numbering using set

Turn on line numbering with the command of your choice. You will obtain a result similar to the following example:

1 This is line 1
2 This is line 2
3 This is line 3
4 This is line 4
5 This is line 5
6 This is line 6
7 This is line 7
8 This is line 8
9 This is line 9
10 This is line 10
11 This is line 11
12 This is line 12
13 This is line 13
14 This is line 14
15 This is line 15
16 This is line 16
17 This is line 17
18 This is line 18
19 This is line 19
20 This is line 20
21 This is line 21
22 This is line 22
23 This is line 23
24 This is line 24
25 This is line 25
:set nu 25,1 All

1 This is line 1
2 This is line 2
3 This is line 3
4 This is line 4
5 This is line 5
6 This is line 6
7 This is line 7
8 This is line 8
9 This is line 9
10 This is line 10
11 This is line 11
12 This is line 12
13 This is line 13
14 This is line 14
15 This is line 15
16 This is line 16
17 This is line 17
18 This is line 18
19 This is line 19
20 This is line 20
21 This is line 21
22 This is line 22
23 This is line 23
24 This is line 24
25 This is line 25
:set number 25,1 All

Moving around within the file

Commands

Command	Description
h or &larr; or Backspace	Move cursor left one character
j or &darr; or ↵ Enter	Move cursor down one line
k or &uarr;	Move cursor up one line
l or &rarr; ou Space Bar	Move cursor right one character
b	Move cursor back to beginning of preceding word
w	Move cursor to beginning of next word
e	Move cursor to the end of the current word
H	Move cursor to top of screen
M	Move cursor to middle of screen
L	Move cursor to bottom of screen
G or :$	Move cursor to the last line of the file
1G or :0	Move cursor to first line in file
27G	Move cursor to line 27
Ctrl+f	Move forward one screen
Ctrl+d	Move down (forward) one half screen
Ctrl+b	Move backward one screen
Ctrl+u	Move up (back) one half screen

To do - Test each of the above commands. When you have finished, position the cursor at beginning of line 13. In order to understand why the H, J, K and L keys are used as arrow keys, please see this page.

Inserting Text

Commands

Key(s)	Description
i	Insert text before cursor
I	Insert text at beginning of current line
a	Append text after cursor
A	Append text to end of current line
o	Open and put text in a new line below current line
O	Open and put text in a new line above current line
Escape	Returns the editor to Command mode

LAB #5 - Inserting text

Insert a line under the line 13 using the o command. Note that you are now in Insert mode. Type the following text : Linux is super. You will obtain a result similar to the following example :

1 This is line 1
2 This is line 2
3 This is line 3
4 This is line 4
5 This is line 5
6 This is line 6
7 This is line 7
8 This is line 8
9 This is line 9
10 This is line 10
11 This is line 11
12 This is line 12
13 This is line 13
14 Linux is super
15 This is line 14
16 This is line 15
17 This is line 16
18 This is line 17
19 This is line 18
20 This is line 19
21 This is line 20
22 This is line 21
23 This is line 22
24 This is line 23
25 This is line 24
-- INSERT -- 14,15 Top

Warning - Do not change the size of the window. You only need to see the first 25 lines.

Switch back to Command mode using the Escape key and place the cursor on the last line of the screen using the L command. Move the cursor to the end of the line in Insert mode using the A command and once again type Linux is super. You will obtain a result similar to the following example :

1 This is line 1
2 This is line 2
3 This is line 3
4 This is line 4
5 This is line 5
6 This is line 6
7 This is line 7
8 This is line 8
9 This is line 9
10 This is line 10
11 This is line 11
12 This is line 12
13 This is line 13
14 Linux is super
15 This is line 14
16 This is line 15
17 This is line 16
18 This is line 17
19 This is line 18
20 This is line 19
21 This is line 20
22 This is line 21
23 This is line 22
24 This is line 23
25 This is line 24Linux is super
-- INSERT -- 25,30 Top

Switch back to Command mode using the Escape key and move the cursor to the first line of the screen using the H command. Move the cursor to the third word by pressing the w key 3 times. Switch to Insert mode using the i key and type Linux is super. You will obtain a result similar to the following example :

1 This is line Linux is super1
2 This is line 2
3 This is line 3
4 This is line 4
5 This is line 5
6 This is line 6
7 This is line 7
8 This is line 8
9 This is line 9
10 This is line 10
11 This is line 11
12 This is line 12
13 This is line 13
14 Linux is super
15 This is line 14
16 This is line 15
17 This is line 16
18 This is line 17
19 This is line 18
20 This is line 19
21 This is line 20
22 This is line 21
23 This is line 22
24 This is line 23
25 This is line 24Linux is super
-- INSERT -- 1,28 Top

Switch back to Commande mode using the Escape key and move the cursor to the first line of the screen using the H command.

Searching for Text

Commands

Key(s)	Description
/ string	Search forward for occurrence of string in text
//	Search forward for next occurrence of string in text
? string	Search backward for occurrence of string in text
??	Search backward for next occurrence of string in text
n	Move to next occurrence of search string
N	Move to next occurrence of search string in opposite direction
:g/string/s//string1/g	Search and replace string by string1

LAB #6 - Searching for and replacing text

Search the text for the string super by using the /super command. Now search the next two occurrences using the // command twice. Your cursor should now be at the beginning of the last word on the last line.

Now search backwards for the same string using the ?super command. Your cursor should now be on the line in the middle of the screen.

Now use the n command. Your cursor should be on the first line. Now use the N command. Your cursor should now be, once again, on the line in the middle of the screen.

Place the cursor at the beginning of the first line and search and replace the string super by the string wonderful using the following command :

:g/super/s//wonderful/g

You will obtain a result similar to the following example :

1 This is line Linux is wonderful1
2 This is line 2
3 This is line 3
4 This is line 4
5 This is line 5
6 This is line 6
7 This is line 7
8 This is line 8
9 This is line 9
10 This is line 10
11 This is line 11
12 This is line 12
13 This is line 13
14 Linux is wonderful
15 This is line 14
16 This is line 15
17 This is line 16
18 This is line 17
19 This is line 18
20 This is line 19
21 This is line 20
22 This is line 21
23 This is line 22
24 This is line 23
25 This is line 24Linux is wonderful
3 substitutions on 3 lines 25,1 Top

Deleting Text

Commands

Key(s)	Description
x	Delete single character under cursor
X	Delete single character to the left of cursor
5x	Delete 5 characters, starting with character under cursor
dw	Delete current word
5dw	Delete 5 words, starting with the word under cursor
dd or :d	Delete the current line
5dd	Delete 5 lines starting with the line under cursor
:5,7 d	Delete lines 5, 6 and 7

LAB #7 - Deleting lines

Place the cursor on the line containing 14 and delete it using the dd command. You will obtain a result similar to the following example :

1 This is line Linux is wonderful1
2 This is line 2
3 This is line 3
4 This is line 4
5 This is line 5
6 This is line 6
7 This is line 7
8 This is line 8
9 This is line 9
10 This is line 10
11 This is line 11
12 This is line 12
13 This is line 13
14 This is line 14
15 This is line 15
16 This is line 16
17 This is line 17
18 This is line 18
19 This is line 19
20 This is line 20
21 This is line 21
22 This is line 22
23 This is line 23
24 This is line 24Linux is wonderful
25 This is line 25
14,1 All

Note the contents of lines 4 through 6. Now delete those lines using the following command:

:4,6 d

You will obtain a result similar to the following example :

1 This is line Linux is wonderful1
2 This is line 2
3 This is line 3
4 This is line 7
5 This is line 8
6 This is line 9
7 This is line 10
8 This is line 11
9 This is line 12
10 This is line 13
11 This is line 14
12 This is line 15
13 This is line 16
14 This is line 17
15 This is line 18
16 This is line 19
17 This is line 20
18 This is line 21
19 This is line 22
20 This is line 23
21 This is line 24Linux is wonderful
22 This is line 25
~
~
~
3 fewer lines 4,1 All

Copy, Cut and Paste

Commands

Key(s)	Description
yy or Y or :y	Copy (yank) the current line into the buffer
V	Select a block of text
p	Put (paste) the line(s) in the buffer into the text after the current line
P	Put (paste) the line(s) in the buffer into the text before the current line
:2,3 co 7	Copy lines 2 to 3 to the line after line 7
:2,3 m 7	Move lines 2 to 3 to lines 6 and 7

LAB #8 - Copying, Cutting and pasting text

Move the cursor to line 3 and copy it using the yy command. Move to line 5 and paste the line using the p command:

1 This is line Linux is wonderful1
2 This is line 2
3 This is line 3
4 This is line 7
5 This is line 8
6 This is line 3
7 This is line 9
8 This is line 10
9 This is line 11
10 This is line 12
11 This is line 13
12 This is line 14
13 This is line 15
14 This is line 16
15 This is line 17
16 This is line 18
17 This is line 19
18 This is line 20
19 This is line 21
20 This is line 22
21 This is line 23
22 This is line 24Linux is wonderful
23 This is line 25
~
~
3 fewer lines 6,1 All

Move your cursor to line 4 and copy it using the Y command. Now move your cursor to line 6 and paste the copied line using the P command. You will obtain a result similar to the following example:

1 This is line Linux is wonderful1
2 This is line 2
3 This is line 3
4 This is line 7
5 This is line 8
6 This is line 7
7 This is line 3
8 This is line 9
9 This is line 10
10 This is line 11
11 This is line 12
12 This is line 13
13 This is line 14
14 This is line 15
15 This is line 16
16 This is line 17
17 This is line 18
18 This is line 19
19 This is line 20
20 This is line 21
21 This is line 22
22 This is line 23
23 This is line 24Linux is wonderful
24 This is line 25
~
3 fewer lines 6,1 All

Now use the following command :4,5 co 15. You will obtain a result similar to the following example:

1 This is line Linux is wonderful1
2 This is line 2
3 This is line 3
4 This is line 7
5 This is line 8
6 This is line 7
7 This is line 3
8 This is line 9
9 This is line 10
10 This is line 11
11 This is line 12
12 This is line 13
13 This is line 14
14 This is line 15
15 This is line 16
16 This is line 7
17 This is line 8
18 This is line 17
19 This is line 18
20 This is line 19
21 This is line 20
22 This is line 21
23 This is line 22
24 This is line 23
25 This is line 24Linux is wonderful
:4,5 co 15 17,1 Top

Note that lines 4 and 5 have been copied to after line 16:

...
15 This is line 16
16 This is line 7
17 This is line 8
18 This is line 17
...

Now use the following command: :4,6 m 20. You will obtain a result similar to the following example:

1 This is line Linux is wonderful1
2 This is line 2
3 This is line 3
4 This is line 3
5 This is line 9
6 This is line 10
7 This is line 11
8 This is line 12
9 This is line 13
10 This is line 14
11 This is line 15
12 This is line 16
13 This is line 7
14 This is line 8
15 This is line 17
16 This is line 18
17 This is line 19
18 This is line 7
19 This is line 8
20 This is line 7
21 This is line 20
22 This is line 21
23 This is line 22
24 This is line 23
25 This is line 24Linux is wonderful
3 lines moved 20,1 Top

Configuring a Personalised Interface

VI can be configured by any user to suit his/her requirements. This is achieved by creating and editing the file ~/.exrc. The file is read by VI each time it is launched by that user and the commands contained in it are executed. The format of each command is the same as if it were typed by the user within VI except that the leading : character is omitted. For example the following .exrc file would tell VI to turn on line numbering and show hidden characters:

set nu
set list

To do - Save your vitext file and quit VI, copy the above text and paste it into a new file in your home directory called .exrc.

Open the /home/trainee/vitext file using VI:

[trainee@centos7 ~]$ vi vitext

trainee@debian8:~$ vi vitext

trainee@ubuntu1604:~$ vi vitext

trainee@SLES12SP1:~> vi vitext

You will see a result similar to that shown below:

1 This is line Linux is wonderful1$
2 This is line 2$
3 This is line 3$
4 This is line 3$
5 This is line 9$
6 This is line 10$
7 This is line 11$
8 This is line 12$
9 This is line 13$
10 This is line 14$
11 This is line 15$
12 This is line 16$
13 This is line 7$
14 This is line 8$
15 This is line 17$
16 This is line 18$
17 This is line 19$
18 This is line 7$
19 This is line 8$
20 This is line 7$
21 This is line 20$
22 This is line 21$
23 This is line 22$
24 This is line 23$
25 This is line 24Linux is wonderful$
"vitext" 26L, 442C 20,1 Top

<html>

Copyright © 2004-2017 Hugh Norris.<br><br> <a rel=“license” href=“http://creativecommons.org/licenses/by-nc-nd/3.0/”><img alt=“Creative Commons License” style=“border-width:0” src=“https://i.creativecommons.org/l/by-nc-nd/3.0/88x31.png” /></a><br />This work is licensed under a <a rel=“license” href=“http://creativecommons.org/licenses/by-nc-nd/3.0/”>Creative Commons Attribution-NonCommercial-NoDerivs 3.0 Unported License</a>

</html>