0% found this document useful (0 votes)
15 views

Using The VI Text Editor

Uploaded by

zebra.chan20
Copyright
© © All Rights Reserved
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views

Using The VI Text Editor

Uploaded by

zebra.chan20
Copyright
© © All Rights Reserved
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 5

Using the vi Text Editor

Using the vi Text Editor.


In this exercise, you will use a UNIX text-editing tool: the vi (pronounced "vee eye") 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.

Examples of these include scripts and environment control 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.

The vi editor operates in three basic modes:


 Command mode:
Which is used either for cursor movement throughout the textfile or to modify the textfile, by
either moving, changing or deleting text.

 Lastline mode:
Is used to quit the editor, save the textfile, set any editor preferences and search thru the textfile.

 The text insertion mode:


In which you type the characters you want to input into your 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)

1. Which vi mode is primarily used to enter new text? ____________________

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

Save and Quit Commands - These are used in lastline mode:


:w Write buffer (Save changes and continue working in vi Editor)
:w new_filename Write buffer to new filename (continue working in vi)
:wq Write buffer (save changes) and quit vi
:q! Quit without saving changes
:wq! Write buffer (save changes) and quit vi
(The ! will override Read only permissions but only if you are the owner of the file.)

Basic Text Deletion Commands - These are used in command mode:


x Delete character at the cursor
dw Delete word (or part of word to right of cursor)
3dw Delete three words
dd Delete line containing the cursor
3dd Delete three lines

Undo and Change Commands - These are used in command mode:


r Replace character at cursor with one other character
u Undo previous command

Copy and Paste Text - These are used in command mode:


yy Yank a copy of this line and place in the clipboard
yy 3 Yank a copy of next 3 lines and place them in the clipboard
yw Yank a copy of this word and place in the clipboard
yw 3 Yank a copy of next 3 words and place them in the clipboard
p Put (paste) yanked or deleted line below current line
P (upper case) Put (paste) yanked or deleted line above current line

Basic Search Commands- These are used in lastline mode:


G (upper case) Go to last line of file
:21 Go to line 21
/string Search forward for this string of text
?string Search backward for this string of text
n Find next occurrence of this string of text

Session Customizing Commands


:set nu Show line numbers
:set nonu Hide line numbers
:set showmode Display current mode of operation
:set noshowmode Turn off mode display
:set Display all vi variables set
:set all Display all possible vi variables and their current

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

vi Text Editor Exercises


The vi Editor is started from the command line. Whenever you invoke vi Editor, you are opening a file.
You can specify the name of the file you want to create or edit when you start vi Editor or you can open
a new file to be named later. It is common to start vi Editor and specify a file name. If the file exists, it is
opened for editing. If the file does not exist, it is created.

1. Open a new file called myvifile using the command: vi myvifile.

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? _____________________

7. Type a colon to go from Command mode to Last-line 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

11. Type cat myvifile

12. Open myvifile, vi myvifile.

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.

15. Enter about three lines of text this way.

16. Press the Esc key to leave Insert mode and return to Command mode.

17. Type a colon : to go from Command mode to Last-line 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.

29. Type cat myvifile

30. Can you see the changes you saved. YES or NO

31. Open myvifile, vi myvifile.

32. Move the cursor to the beginning of the 4th line

33. Type o then type in THIS IS HERE

34. Press the Esc key again to leave Insert Entry mode and return to Command mode.

35. Type yy – to copy this line.

36. Move to the line which contains your name.

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.

39. Move down to the bottom of the text file.

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.

42. Move to the line which contains your name.

43. Type dd – to delete this line.

44. Type u – to undo this action.

45. Type a colon (:) to go to Last-line mode

46. Then type q! to quit this file without saving any of your changes.

47. Type cat myvifile

48. Can you see the changes you made. YES or NO


4
Using the vi Text Editor

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.

Exercise 1 – Investigating the vi Editor.


1. Just type: vi
2. This will start the vi editor at it’s main menu.
3. You can use this main menu to learn more about the commands options available in this
texteditor it also has an inbuilt tutorial.

Exercise 2 - Create a New File with the vi Editor


1. Type: vi simple
2. Type: This is line1 - vi is simple to use, anyone can use it
3. Copy this line
4. Paste it in four more times.
5. Replace and line number so – it reads from line 1 fo line 5.
6. Replace simple into easy and fun.
7. Insert the word ‘Using’ in front of the word vi of the 3rd and fourth lines.
8. Save the file and call it simple.
9. Quit the vi editor
Exercise 3 – Editing an existing file
1. Reopen the file simple.
2. Resave the file with a new filename called exercise3 and quit in the same action.
3. Reopen the exercise3 file
4. Turn on line numbering.
5. Delete the 2nd and 3rd lines.
6. Add a line at the end of the file which says: I’m getting use to the vi text editor.
7. Add a line 2nd from the top which says: Using vi is very very easy.
8. Quit vi without saving any of your changes.

Exercise 4 – More editing an existing file


1. Change into the class-files directory.
2. Open the file vi-practise1 in vi.
3. Through out the poem - change Mary to your own name.
4. Through out the poem - change lamb to any other animal.
5. Change the colour of the fleece from white to purple.
6. Change school to TAFE.
7. At the end of the poem - Add 3 about what happened to the animal at TAFE.
8. Copy the first line and paste it at the end of the poem.
9. Copy the 3rd line and paste it immediately below itself.
10. Copy the entire poem and paste it as the second to last line.
11. Save the file as a new file called ‘my-mary-poem’.
12. Quit vi

Exercise 5– Editing an existing readonly file


1. Whilst in the class-files directory.
2. Open the file vi-practise2 in vi.
3. Through out the poem - change twinkle to shine.
4. Save the file - Use w! to force the read-only file to accept the changes.
- But without changing the files actual permissions.

You might also like