This action might not be possible to undo. Are you sure you want to continue?
Academic Technology Support California State University, Los Angeles
JC - UVIBEG (v1.3)
General Information Terminal Type Mode Case Repeating Commands Creating a New File Calling vi Typing the File Leaving vi Bailing Out
Editing a File Positioning the Cursor Making Changes in the File Inserting Text Deleting Text Replacing Text Undo Moving Text within a File Moving Text between Files Command Summary
For additional written instructional guides, visit us @ http://www.calstatela.edu/handouts
most readers can safely skip to the section on vi’s Mode instead. Fortunately. check the manual for the hardware terminal or the communication software to determine the terminal type. For a complete reference to vi. vi has many other functions and commands than those described in this handout. it is likely because the system's information regarding your terminal type is incorrect. Otherwise. type set term=xxx or (Sun) (AT&T computers) TERM=xxx. export TERM where xxx is a terminal type. You can display the terminal type for your current session by typing set at the Unix prompt. A list of settings will be displayed. vt100 is the correct terminal type for most of the equipment on campus. a microcomputer. (When it cannot determine the terminal type you are using. the screen displayed by vi will be unreadable. If the display is scrambled when you access vi. This handout is an introduction to file editing with vi.) The following information is mainly useful to people using legacy hardware terminals and old telecomm software. You’ll need to check and alter the terminal type is this is the case. however. you must make sure that your terminal type is set correctly. General Information Terminal Type Before starting vi. otherwise. When using Procomm or Kermit communication software. such as a Teleray 100 terminal. It describes a minimum set of vi commands that should be sufficient for most editing purposes. To change your terminal type. Beginner's Guide to the VI Editor 2 . such as vt100. the system will normally automatically set the terminal type for you when you first log on to the system. see the Unix operating system manuals available in the instructional computing labs. or a Tab terminal. it will prompt you to enter one. One setting will show $TERM=xxx or term xxx where xxx is the terminal type.VI vi is a full screen editor available on computers running the Unix operating system.
just as is Unix. When using this handout. Beginner's Guide to the VI Editor 3 .Mode When you are using vi. When you are in the edit mode. There are several ways to enter input mode (described below). called a cursor. be sure to type the commands in the case in which they are shown.) where filename is the name you wish to give your new file. Repeating Commands For most of the vi commands discussed below. Case vi is case sensitive. the command vi should be typed in lowercase. deleting characters or lines. When you are in input mode. The file name may not include blank spaces or a forward slash (/). This means that vi does not see upper and lower case letters as the same letter. etc. After you type the above command. type vi filename (Remember. Notice that the cursor is now at the top of the file. For example. a number can be included at the beginning of the command. They are two different commands that perform two different operations. File names on Unix must be 14 or fewer characters. type in the file. Next. You will see on your screen a blinking white square. Creating a New File To create a new file. To vi. such as moving the cursor. When you first access vi. typing dd will delete a line and 6dd will delete 6 lines. you must exit vi and save your file. you must first call vi and name your new file. Calling vi To create the new file. you first call vi. all the characters you type will be inserted into your file until you end input mode and return to edit mode by pressing the Esc key. To do this. giving your file a name. Once you enter input mode. it is important to know which mode you are in: the edit mode or the input mode. all the characters you type will be inserted into your file. the keys you type are editing commands that perform operations on your file. vi will display an empty screen with a tilde (~) at the beginning of each line and the following line at the bottom of the screen: "filename" [New file] where filename is the name you gave the file when you called vi. The command will then execute several times as indicated by the number. you will be in edit mode. an "A" is not the same thing as an "a". Finally.
be sure this is in lowercase. you may have made so many mistakes in the file that you feel it would be easier to start over again. press the a key. make the change. This will take you into input mode. after you have created the file. For instance. Be sure to press the Return or Enter key at the end of each line. and will exit from vi. The keys for moving the cursor around the screen are: direction down up right left key j k l h Beginner's Guide to the VI Editor 4 . Or. Anything you type will appear where the cursor is located. be sure you exit vi by typing :wq to save the edited version of the file. Editing a File You may wish to make some changes in the new file you have just typed. saved it. It will be entered into your file as it appears on the screen. Bailing Out Occasionally you may wish to leave vi without saving the file. Then. When you have completed typing the information. first move the cursor to the place in the file you wish to change and second.Typing the File To enter text into your file. In such a case. type the information. giving it the name you provided when you first called vi. For each change.) Making a change is a two step process. Positioning the Cursor The first step in making any changes in your file is to move the cursor to the location of the text you wish to change.) This will save the file. type :wq (Again. (Call the file back into vi as described in the previous section. type :q! (The cursor will move to the bottom of the screen. press the Esc key to leave input mode. back to the Unix operating system.) This will exit from vi and will NOT save the file. and exited vi. When all the changes have been made. you may find that you wish to call the file back into vi to make changes. Leaving vi When you have finished entering the new file.
(Press the key labeled Ctrl and. type the characters you wish to insert. In some cases. depending on the configuration of the Unix host computer and the type of terminal or communication software used to access it. 1. There are several other commands for moving the cursor around the page and through the file. Type the string to be located. Therefore. If the string is found elsewhere in the file. you can move through the file until the line you wish to change is displayed on the screen. press the letter key D. with the line containing the text in the middle of the screen. l) to move the cursor to the place to be changed. Using these two commands.) To move backward one screen. in many cases. Some of these commands are listed in the Command Summary at the end of this handout.These keys should work on with any terminal and any Unix computer. press the Ctrl-U. Another way to move the cursor is to search through the file for a particular string of characters. the arrow keys will also work to move the cursor around the screen. j. The above keys allow you to position the cursor anywhere on the screen. your file will be longer than one screen. To move forward one screen. When you have finished typing the characters you wish to insert. Making Changes in the File The three most common types of changes are (1) insert needed text. such as moving forward one word. then use the above four characters (h. However. If the string is found on the current screen. you often need to move forward and backward through the file. Inserting Text: Move your cursor to the place you wish to insert the text. the screen will display the section of the file where the text was found. etc. (2) delete unwanted text and (3) replace text. while continuing to hold down the Ctrl key. press the Esc key to end input mode and return to edit mode. To do this. / ? to search from the current cursor location forward to the end of the file to search from the current cursor location backward to the beginning of the file The cursor will move to the bottom of the screen. the cursor will move directly to the string. k. Beginner's Guide to the VI Editor 5 . press Ctrl-D. type one of the following commands. Press one of the following letters to enter input mode a i o O A I to insert after the cursor to insert in front of the cursor to insert a line below the current line to insert a line above the current line to append to the end of the current line to insert at the beginning of the current line Next. moving to next line.
If your cursor is in the middle of the line and you type D. move the cursor to the character to be deleted and type x. Type dd and the complete line will be removed.2. The deleted text will be copied to the buffer automatically. you can press the u key to undo the last change you made in your file or you can press the U key to undo all the changes you have made on the current line. move the cursor to the line. Beginner's Guide to the VI Editor 6 . the cursor is moved to the new location. To do this. s ns S cw C to replace one character with several characters to replace n characters with one or more characters to replace a line to replace a word replace characters from cursor to end of line These five commands put you into input mode. Replacing text: To replace a single incorrect character. Be sure to end input mode by pressing the Esc key. You can also delete part of a line. Deleting Text: To delete a character. To delete an entire line. Undo vi has an undo command in case of mistakes. At any point. the text will be deleted from the cursor forward to the end of the line. Copy text to the buffer using one of the following: a) Delete the text to be moved using the delete commands described previously. 1. use one of the following commands. b) Copy the text to the buffer without deleting it using the following commands. Moving Text Within a File vi allows you to move lines of text from one location to another within the file. Move the cursor to the beginning of the text to be moved. and the text is copied from the buffer to the file. 3. move your cursor to the character to be replaced and type rx where x is the correct character to replace the current character. the text is copied to a buffer. 2. To replace more than one character.
3. Type p which will copy the text from the buffer into the file following the current line. For example. type :r filename (The cursor will move to the bottom of the screen.Nw filename (The cursor will move to the bottom of the screen. 3w file2 will copy line number 3 of the current file into another file named file2. and 6 of the current file into another file named file2. Moving Text Between Files You may also wish to copy text from one file to another. vi allows you to 1) copy part of the text from the current file into another file or 2) copy the contents of another file into your current file. Thus. There are two useful symbols you may use when designating line numbers. 2) From another file: To copy another file into your current file. The format of the command is :nw filename (the cursor will move to the bottom of the screen) where filename is the name of the file into which you wish to copy the text and n is the line number of the line you wish to copy.) where filename is the name of the file into which you wish to copy the text. n is the line number of the beginning line of the block of text you wish to copy. first move your cursor to the location in which the text from the other file should be inserted.$w file2 would copy all the text from the line where your cursor is located to the last line into the file named file2. designates the current line and a dollar sign $ designates the last line. Beginner's Guide to the VI Editor 7 . The contents of the file named filename will be inserted into the current file. The format of this command is :n. Move the cursor to the place you which to insert. For example. 4.) where filename is the name of the file which you wish to copy into your current file. following the line where your cursor is located. designate the line numbers of the lines you wish to copy. Then..nyw nYY nY) to copy n words into buffer to copy n lines into buffer to copy n sentences into buffer 3. 5. 4.6w file2 will copy lines number 3. 1) To another file: To copy text from the current file to another file. . A period . and N is the line number of the ending line of the block. You can also copy a section of text into another file by designating the line numbers of the first and last lines of the section to be copied.
COMMAND SUMMARY vi filename :wq :q! u U enter vi leave vi and save the changes leave vi without saving the changes undo last change undo all changes on current line Commands for Moving Around in the File h j k l Space Bar ^H + ^U ^D B W e w H M L G / ? left down up right forward one character backward one character beginning of next line beginning of previous line scrolls up scrolls down backward one word forward one word end of current word word after this word home screen line middle screen line last screen line go to specified line search forward for the string search backward for the string Commands for Making Changes ESC i a o O A I x dd rz s ns end the text to be inserted insert text before the cursor insert text after the cursor insert a new line after the current line insert a new line before the current line append at end of current line insert at beginning of current line delete the character on which the cursor is set delete a line replace the current character with z replace one character with several characters replace n characters with one or more characters Beginner's Guide to the VI Editor 8 .
cw cc C nYY p nw filename r filename replace a word with text you type in replace line with text you type in replace text from cursor to end of line copy n lines into buffer copy text from buffer into file copy line n into file named filename copy file named filename into current file Beginner's Guide to the VI Editor 9 .
This action might not be possible to undo. Are you sure you want to continue?
We've moved you to where you read on your other device.
Get the full title to continue reading from where you left off, or restart the preview.