Text & Documentation / Perform Basic File Editing Operations Using vi
Subtitles of the Movie
Exam objective 1.103.8 has a weight of 1, and verifies that candidates are able to edit text files using VI. This includes VI navigation, VI modes, inserting, editing, deleting, copying, and finding text. You already have an editor that you prefer to use. Everybody has. But the VI Editor has the advantage that it is included in every Linux installation. If you need to make a quick change to a configuration file, VI is always there and ready. So, you need to know how to use enough commands to perform basic editing. VI is what is known as a modal editor. At all times it's operating in one of two modes. If The Editor is in command mode, it interprets your keystrokes as commands to navigate to another location in the text, or to switch to the other mode, which is the insertion mode. In the insertion mode, every keystroke is interpreted as a character to be inserted as text. You can leave insertion mode and return to command mode any time by pressing the Escape key. You can press the Escape key as many times as you like. It only switches to, or stays in, command mode. The rest of this lesson describes some of the commands you can use to insert or change text. To edit a file you enter the name VI followed the name of the file. The Editor will load the text and display the first page of it to you. At the bottom of the screen it tells you what line the cursor is on and it mentions the fact that the file has been loaded, but not modified. You move the cursor around the screen with the H, J, K, and L keys. The truth is, that you can use the arrow keys to do the same thing, but VI is written so all you need are the keys for ASCII characters. You use the slash character followed by a regular expression to search the text. This command will search forward for the word 'here'. The Enter key causes the search to start then the letter N will cause it to search again, and again, and again, and if the search hits the bottom of the file, it will loop back up to the top and continue the search. In the same way, the question mark causes a regular expression to search backwards in the text. The letter N will cause it will search again, and again, and again, and loop back around to the bottom if necessary. So far, everything I've shown you have been commands with lower case letters. But there are some upper case commands, too. For example, an upper case G will take you to the last line of the file. A number preceding an upper case G will take you to that line number in the file. For example, entering 4G will take you to line 4. The G command will scroll the text if necessary to put the cursor on the chosen line. An upper case H is the Home command. It will go to the first line being displayed on the screen. A number in front of the H will make it go to that line number on the display. For example, 11H takes you to line number 11 shown on the screen. An upper case L takes you to the bottom line, shown on the screen, and a number in front of that L moves the cursor to that number of lines from the bottom. There are several other navigation commands, but with these, you can get around a file and do what you want to do. In the long run, you will find a few of these commands that you like and stick with those. I always use 1G to put the cursor at the top of a file. Now, I need to show you some insertion commands. The simplest is the lower case letter i. Just put the cursor at the point you wish to insert text and then type i followed by the text you want to insert. Then, when you've got what you want, press the Escape key to return to the command mode. You can use C to change something. For example, you can enter cw to change a word. Notice that the last character changed to a dollar; that's to mark the end of what's being changed. Your changes can be longer or shorter than the thing you're changing. The text will adjust to fit. Once you've entered your desired change, you press the Escape key and the text updates itself. This is the sort of editing that you might need to do with a configuration file. By putting a multiplier in front of cw you can change more than one word at a time. For example, entering 4cw does this. Notice now that the dollar sign comes at the end of four words. And you can type in the new text. The Escape key changes the mode back. The lower case d key deletes things. For example a dw deletes a word. And dd deletes a line. When you delete a line it goes into what is known as the Yank buffer. You can reinsert it as the line below the cursor with the letter p. So, the combination ddp reverses two lines. You can copy a line to the Yank Buffer without deleting it by entering yy. Then every time you hit the letter p it will be reinserted. One more insertion. One that you will use a lot, is the letter o. It opens a new line on the line below the cursor and puts the cursor in it, and you're in insert mode; you can enter whatever text you like. And the Escape key leaves it there. When you have finished your edit, type in two upper case Zs and the text will be written to the file, and VI will close. If you want to save your edits and keep going, enter a colon followed by a w and an exclamation point. That will appear at the bottom of the screen and remain until you press enter. The exclamation point is there to force output, so it will write over the existing file. If you want to quit and abandon your edits, enter a q followed by an exclamation point. And when you press Enter, VI will exit. The exclamation is necessary in this case to get VI to abandon any pending edits. You need to work with an editor to find out how it does things. Create a new text file, or edit an existing file and try out all the commands. You'll find some that you personally prefer, but you need to know the basics explained in this lesson. Look at the help inside VI. It will be very valuable until you get to know a set of usable commands.
Tutorial Information
| Course: | Linux Professional Institute: Level 2 |
| Author: | Arthur Griffith |
| SKU: | 33894 |
| ISBN: | 1-934743-79-8 |
| Release Date: | 2008-07-21 |
| Duration: | 7.5 hrs / 113 lessons |
| Captions: | Available on CD and Online University |
| Compatibility: |
Vista/XP/2000, OS X, Linux QuickTime 7, Flash 8 |
VTC Sign up & Benefits
- Unlimited Access
- 98,729 Video Tutorials (23,265 free)
- Video Available as Flash or QuickTime
- Over 1026 Courses
- $30 for One Month Access
- Multi-User Discounts Available
United States 