Using The VI Text Editor
Using The VI Text Editor
A text editor is primarily used for creating and modifying files that customize your work environment
and for writing script files to automate tasks. System administrators use text editors to create and
modify system files used for networking, security, application sharing, etc. The vi Editor became a
part of the UNIX operating systems shortly after UNIX’s inception and is universally available with
UNIX systems including Linux. The vi Editor is a very flexible and powerful editor with many
options. These will be reviewed here with examples of their use.
For users learning to become system administrators, it is important to know how to use vi Editor.
It is sometimes the only full screen editor available to edit crucial system files.
Skill in using vi Editor is also needed if the GUI system is not available. The vi Editor is a useful tool
when working remotely on other Linux workstations or servers. Administrators routinely login
remotely or telnet to another Linux computer to perform maintenance and troubleshooting tasks using
the vi Editor. The availability and operation of vi Editor is consistent across all UNIX/Linux
platforms.
Lastline mode:
Is used to quit the editor, save the textfile, set any editor preferences and search thru the textfile.
When first started, the vi editor is in command mode. To enter the insert mode in order to begin typing
your text, press the i key. At this point, anything you type will appear on the screen just as if you were
using a typewriter. At the end of each line, just as on a typewriter, you should press the [RETURN] key.
When you are through typing and want to return to command mode, press the [ESC] key.
Moving between vi Modes
The table below shows how to switch modes and get from one mode to another.
Review the keystrokes required to move between modes and answer the following questions.
From Mode To Mode Commands / Keystrokes
Command Esc (Escape)
Last-line Colon :
Text Insertion i (input at curzor), o (open new line), a (after curzor)
2. Which vi mode is used to save your work and quit vi Editor? ____________________
3. When you start the vi Editor, which mode is the default? ____________________
1
Using the vi Text Editor
The attached vi Quick Reference sheets is a brief summary of some of the most available vi operations.
Use it to help you with these exercises - refer to it when needed.
Navigation Keys - These are used in command mode:
$ Move to end of line
0 (zero) or ^ Move to beginning of line
HINT:To return to command mode after any of these actions, press the [ESC] key again. When you
aren't sure of the current mode (command or insert), press the [ESC] key to make certain that you
are in the command mode.
2
Using the vi Text Editor
2. Press the lower case letter “i” to begin inserting text at the first line.
3. Is there any indication on the screen that you are in text insertion mode? _______________________
4. Type your name. If you make a mistake do not try to correct it at this time.
5. Press the Esc key to leave Insert Entry mode and return to Command mode.
6. Is there any indication on the screen that you are back in Command mode? _____________________
8. Are you now at the bottom left corner of the screen at a colon (:) prompt?
9. Type a lower case “w” (write - to save your new file), followed by a lower case “q” to quit
10. Type ls - can you see the file you created myvifile. YES or NO
13. Position the cursor at the end of your name and press the lower case letter a to append to the line
you typed earlier.
14. Type some text, and the press Enter for a hard return and then type some more text.
16. Press the Esc key to leave Insert mode and return to Command mode.
18. You should now be at the bottom left corner of the screen at a colon (:) prompt?
19. A the colon (:) prompt type set nu – to turn on line numbering
20. Press the Esc key to leave lastline mode and return to Command mode.
21. Type i to enter Insert mode, practise positioning your cursor (with the arrow keys) for additional editing?
22. Press Esc to enter Command mode, and continue to practise positioning your cursor (with the arrow keys).
3
Using the vi Text Editor
23. You can move your cursor with the arrow keys whilst in these two modes.
24. Press the up arrow to position the cursor until it is on line two.
25. Use the “j” (jump down) command to move down 3 lines then the “k” (kick up) to move back to line two.
26. Type a lower case “o” to open a new line below the line your cursor is on and enter some new text.
27. Press the Esc key again to leave Insert Entry mode and return to Command mode.
28. Type a colon (:) to go to Last-line mode then save and quit this file when you are finished.
34. Press the Esc key again to leave Insert Entry mode and return to Command mode.
37. Type P (capital P) – to paste in this line above the existing line.
38. Type p (little p) – to paste in this line below the existing line.
40. Type a colon (:) to go to Last-line mode then type /THIS to search for this text string.
41. Use the arrow keys to move through the items found.
46. Then type q! to quit this file without saving any of your changes.
Use the vi editor chapter as a reference to help you with these exercises - refer to it when needed.
Please create your own files with the necessary content.