User:Spoonbender/Temp/Vi Editor.doc

Introduction to the vi Editor

The vi is a screen-oriented text editor present in almost all flavors and distributions of UNIX.

It is command driven and uses only basic keys in the keyboard, thus eliminating inconsistencies and incompatibility with other systems

vi would be the only available text editor when you set up and update configuration files in a text based UNIX system.

vi, which stands for visual editor and pronounced as vee-eye, is the standard text editor included in almost all distributions of the UNIX system.It is an interactive, screen-oriented text editor used to create and modify text files in a UNIX system. Basically, vi is the key in managing and administering a UNIX system because you will need to modify configuration files, and vi is the most common text editor available.

vi was created by William Joy at the University of California at Berkeley, and was designed to be terminal independent. Meaning, vi does not depend on the type of terminal you are using, and that the instructions that you need to use to manage files with vi are mapped into the keyboard. The vi editor was originally designed for teletypes which had neither special key functions nor cursor keys. That is why until now those keys are not significant when you are using vi.

Starting vi

You could start a vi session with an already existing file or with a new file from scratch.

Syntax:

vi [filename]

Examples:

$vi /etc/hosts 192.168.250.1steve 192.168.250.2mark 192.168.250.3big_bert 192.168.250.4little_john 192.168.250.5jamie ~ ~ ~ • This vi session was invoked to edit an already existing file /etc/hosts. Note that this file would be ready for modification only if you have entered the input mode.$ vi newfile

~

~

~

~

• This vi session was invoked to create a new file newfile. Notice that the unused spaces are marked with tildes ( ~ ). You would need to enter the input mode to insert text in the file

It is relatively easy to invoke a vi session. You simply need to key in the command vi followed by the name of the file you wish to edit. If the file does not exist, it will create a temporary file for you to edit. If the file already exists, then it will display the contents of that file.

When a vi session is started, whether the file exists or not, vi puts it in memory into a temporary file we call the edit buffer. This file is not necessarily written into the disk drive. Instead, it stays in memory until the user issues a write command. This is very useful when modifying existing files. The buffer is not written to the disk until the user is sure of his modifications. If the user changes his mind or have noticed irregularity in his modification, he could discard the changes any time and the original file would not, in any way be affected.

If a new file was invoked using the vi session, you would notice each line in the new file are filled with tildes (~ ) running down from the left column. These indicate that those lines in the file are still vacant and it is up to the user if he wants to add text to it.

The Three Modes of vi

There are three basic modes in vi. These are the Command Mode, the Last Line Mode and the Input Mode. Each mode has its own specific function.

Command Mode

• It is the initial mode of vi once you open a session
• It is where you enter the vi command to modify a file like insert, append, delete, etc.
• It is where we do cursor movements across the file
• Commands are not echoed on the screen
• Press the Esc key to go to the Command Mode

Last Line Mode

• Accepts ex and search commands from the user
• Commands are echoed on the lower portion of the screen

Input Mode

• Accepts keystrokes to insert in the file being modified

The vi editor has three basic modes. These are the Command Mode, the Last Line Mode and the Input Mode. Each mode has its own specific function. The Command Mode is where we enter the vi commands to manipulate the file we are editing. Some of these commands are used to move the cursor to the desired position, deleting, copying, pasting, replacing, and commands to enter the input mode. The Last Line Mode is where we enter commands to save the file, quit the vi session, search and other ex commands. The Input mode is where we input characters into our text file. This is where we edit our file and make modifications.

The ex is an extended line-oriented editor that is available to the user only when he is in the Last Line Mode. ex commands are entered through the colon prompt and submitted to vi by pressing the Return or Enter key. To enter the Last Line Mode, you need to hit the Esc key followed by the colon key. Unlike the vi commands, ex commands are echoed on the screen. These commands are commonly used for multi-line modifications and session customization like writing the buffer to the disk and for quitting the session.

The Esc key is one of the most important keys in any vi session. It is used to bring the user into Command mode. Make it a habit to hit the Esc key every time you are going to perform a vi command to make sure that you are in the Command mode.

Quiting vi

To quit a vi session, you could either type in the vi quit command or an ex quit command.

Syntax:

vi Quit Command:

Esc + ZZ

ex Quit Commands:

Esc + : + q!

Esc + : + wq

Once inside a vi session, it is important that you know how to get out. After completing your modifications, you need to quit vi. To quit vi, there are two possible ways. One is done by issuing the vi quit command, and the other is by issuing the ex quit command.

Given the syntax above, you could quit vi using the vi command ZZ. Remember that in order to issue this command, you need to be in the Command Mode. To do this, you need to hit the Esc key first, and then followed by the ZZ command. Think of the ZZ command like putting the vi editor sleep. This command will not be echoed on the screen, so do not be surprised if you weren’t warned.

You could also quit vi using two ex quit commands. These are the wq and the q! commands. Remember that these are ex commands, and that you need to be in the Last Line mode to issue them. To use them, hit Esc key followed by the colon key. The wq command would first save or write the buffer into the disk before it quits. The q! command would quit vi without saving the buffers. Note that these commands are echoed in the lower portion of your screen.

It is important that you learn how to navigate when inside a vi session. Remember that vi was invented even before your keyboard contained cursor keys. So don’t expect that we will use those cursor keys in this lesson, though it is possible to use them.

Syntax:

• While in the Command Mode, you could use the following keys to navigate though the screen:

 h orBackspace Move to the left by one character j Move down by one line K Move up by one line l orSpace Move right by one character # w Move forward word by word (# indicates jump # words) # b Move backward word by word (# indicates jump # words) # e Move to the end of the next word (# indicates jump # words) $Move to the end of the current line ^ or0 Move to the beginning of the current line G Move to the end of the file #G Move to line number # : # Move to line number # Ctrl + g Displays current line Return orEnter Move to the first non-blank character on the next line Ctrl + b Scroll back to the previous window of text Ctrl + f Scroll forward to the next window of text Ctrl + u Scroll up half a window of text Ctrl + d Scroll down half a window of text L Move to the last line on the screen M Move to the middle line on the screen H Move to the first line on the screen Ctrl + l Redraw the screen The first thing that you need to learn in vi is how to navigate through it. It could get very confusing when navigating in a session. Remember that the vi program was written for the teletypes used in the early computers. These devices did not have cursor keys on their keyboard, and thus, the creator of vi had to make use of the basic keys in the keyboard. Though right now, most vi editors packaged with the UNIX distributions already support the cursor keys, it is still necessary to learn how to navigate through the character keys. This is because there are still UNIX machines that make use of old keyboards that do not have cursor keys. The very basic keys that you need to learn are the ones needed to move to the right, left, up and down. These are given in the summary above. They correspond to the cursor keys that were recently added to the UNIX keyboard. Remember these keys and never be tempted to use the cursor keys in your keyboard. Inserting and Replacing Text In order to insert and replace a text in a file, you need to be in the input mode. You need to know the keystrokes needed to toggle from the Command mode to the Input mode. Syntax: • To enter input Mode and insert text, position the cursor in the desired location and press any of the key that might fit your needs:  a Append new text after the cursor i Insert new text at the cursor position O Open a new line above the cursor position o Open a new line below the cursor position A Append new text at the end of the line I Insert new text at the beginning of the line • To replace text in Input Mode, position the cursor in the desired location and press any of the key that might fit your needs:  r Replaces the character at the current cursor position R Replaces all characters beginning at the cursor position until the Esc key is hit c In order for you to enter text in the file you are editing, you need to go to the input mode. To do this you need to hit any of the key provided in the summary above. Take note of the position of your cursor. It is important that you know where the cursor is because this will determine where the text you are to input will be added. Deleting, Moving and Yanking Text Just like any other text editor, the vi is used to delete, move and copy text within the text file. Syntax: • To delete text, you need to be in the command mode and have positioned the cursor in the proper location, then perform these simple commands;  #x Delete the character at the cursor position #d • Whenever you perform a delete command on the vi session, the deleted items are saved temporarily to the cut buffer that is why, you could move these items around the vi screen. To move them, place the cursor in the desired location and perform the following commands:  P (uppercase) Paste the contents of the cut buffer before the cursor P (lowercase) Paste the contents of the cut buffer after the cursor • Copying of text in vi is also called yanking and thus given the command y. When you want to copy or yank a certain text, it is important to place the cursor in the desired position and perform the yanking commands. Note that just like the delete commands, the items are copied to the cut buffer, and that to paste these copied text, use the p and the P commands.  #yw Yank the current word #yy Yank the current line yG Yank through the last line of the file y$ Yank to the end of the line y^ Yank to the beginning of the line

Using the certain commands in Command Mode, you could delete, move and copy text in the vi session. It is possible to delete a single character, a word, a line, or even multiple lines of text in the vi screen with the d (delete) command. When text is deleted, they are temporarily placed on what we call the cut buffer. The contents of the cut buffer could later be retrieved by the p (paste) command. Deleting and pasting the contents of the cut buffer is like cutting and pasting text on the file.

String Search

Often times, you may need to find a certain text in the file, especially if it is quite long, to perform some vi commands like deleting or copying.

Syntax:

• To search for text in a file in the vi session, you could use multiple commands. Note that when using these commands, you must be careful of the case because these are case-sensitive. The following vi commands could be used:

 /text Search for the text as indicated up to the end of the file, with wrap around ?text Search for the text as indicated towards the beginning of the file, with wrap around n Search for the next occurrence of the previously searched text in the same direction N Search for the next occurrence of the previously searched text in the opposite direction

• Sometimes, you need to search for a string of text but are unsure of the exact text. You do not know the exact case of the text, for example. It would be easier to create an expression that would match the pattern provided. The summary below gives the possible structure of our expression:

 /[a-zA-Z0-9] Searches for the pattern of characters that are provided. The square brackets denote that we are to choose a single character from the range that is provided /^text Search for the string text that is at the beginning of a line /text$Search for the string text that is at the end of a line /. {period} Search for any single character string /character* Search for zero or more occurrences of the string character Examples: Esc + /[Cc]enter • Search for any occurrence of the string Center or center Esc + /[Rr][Aa][Gg][Ee] • Search for the occurrence of the string rage in any case Esc + /^[123] • Searches for the occurrence of either 1, 2 or 3 in the beginning of a line Esc + /[123]$

• Searches for the occurrence of either 1, 2 or 3 in the end of a line

It is normal that when a text file is being edited, a search for the string that needs to be modified is usually made. If the text file were quite long, this would take a while to search for it. The vi editor provides such a mechanism for locating the desired string of text in the text file.

Search and Replace

Using the some ex commands, we could perform a global search and replace in vi.

Syntax:
Esc + :m,ns/old_pattern/new_text/g

 m and n Defines the lines that the command should be executed on s The substitute command old_pattern Defines the string of text to searched new_pattern Defines the string of text that would be substituted to the old one g Defines a global search within the line

Examples:

Esc + :1,10s/old/new

• Searches all the first occurrences in each line of the word old and substitutes the word new from the first line up to the tenth line only.

Esc + :.,\$s/[Rr][Aa][Gg][Ee]/rage/g

• Searches every occurrence of the wore rage in any case, including combinations of upper and lower case instances and replaces it with the word rage, from the current cursor position up to the last line in the file, globally within each line.

Usually, our goal for searching a string of text in a file is to replace it. To make it easier for the user, a global search and replace is also available in the vi editor. This is done through the ex line editor. Thus we need to be in the Last Line Mode to do this. The operation of searching and replacing of text in the file is shown in the summary above. Note that when we say global search, we mean that the search is made in both directions of the line indicated.

Note from the first example that we tried to search for only the first occurrence of the word old, as indicated by not adding the g option in the last portion of the command. The second example, on the other hand, performs a global search and replace, meaning, that it will search and replace all occurrences of the word sought for within the line because of the global option at the end of the command.

Undoing Instructions

If you have somehow made a mistake while using the vi commands, and start regretting what has happened, you need not worry because there is an undo command provided.

Syntax:

• The undo (u) provided in vi could be used while in the Command Mode. There are two versions of the undo command, which are given in the summary below:

 U (uppercase) Undo all the modifications done to the current line u (lowercase) Undo the last modification made

Since you are new to the vi editor, you are most probably to commit an error. You might delete a complete line that you should not have, pasted a string on the wrong position, or other improper modifications. That is but normal. Even professional vi users commit mistakes too. But not to worry, vi provides you with an undo mechanism. You could use the undo for your mistakes.

There are two variations of the undo command. One uses the uppercase letter U, and the other uses the lowercase letter u. The uppercase letter U is used to undo all the modifications made to the line while the lowercase letter u is used to undo the last modification made.

Remember to check for mistakes every time you modify a line, because once you moved on and modify another line, you cannot anymore undo the modifications made to previous line.

Saving the File and Other ex Commands

Saving the buffer to the disk is done by issuing another ex command, w. Also in this module, some additional ex commands are presented.

Syntax:

• The summary below presents some of the important ex commands that you could use while in the Last Line Mode using the ex line-oriented editor. One important command presented is the command use to write the contents of the buffer to disk.

 :w Write the contents of the buffer to disk or save your file :m, nw file Write only lines m to n to a file :w file Write the contents of the buffer to a file or save as… :e file Open the file file without saving the changes to the previously opened file :e! Reload the current file, discarding all the changes :r file Read in the contents of the file file after the current cursor position :! cmd Execute a shell command cmd :set all Display all the edit session options :set nu Turn the line numbering option on :map Display the keyboard mappings used in Command Mode :map! Display the keyboard mappings used in Input Mode

After the vi session, it is important to save your edit buffer. Saving the buffer means saving your work to the disk. This is done with the ex command w. Also, provided in the above summary are the other important ex commands that you could use.