home | O'Reilly's CD bookshelfs | FreeBSD | Linux | Cisco | Cisco Exam    

Learning the vi Editor

Learning the vi EditorSearch this book
Previous: 1.2 Quitting Without Saving Edits Chapter 2 Next: 2.2 Moving the Cursor
 

2. Simple Editing

This chapter introduces you to editing with vi , and it is set up to be read as a tutorial. In it you will learn how to move the cursor and how to make some simple edits. If you've never worked with vi , you should read the entire chapter.

Later chapters show you how to expand your skills to perform faster and more powerful edits. One of the biggest advantages for an adept user of vi is that there are so many options to choose from. (One of the biggest disadvantages for a newcomer to vi is that there are so many different editor commands.)

You can't learn vi by memorizing every single vi command. Start out by learning the basic commands introduced in this chapter. Note the patterns of use that the commands have in common. As you learn vi , be on the lookout for more tasks that you can delegate to the editor, and then find the command that accomplishes it. In later chapters you will learn more advanced features of vi , but before you can handle the advanced, you must master the simple.

This chapter covers:

  • Moving the cursor.

  • Adding and changing text.

  • Deleting, moving, and copying text.

  • More ways to enter insert mode.

2.1 vi Commands

vi has two modes: command mode and insert mode. As soon as you enter a file, you are in command mode, and the editor is waiting for you to enter a command. Commands enable you to move anywhere in the file, to perform edits, or to enter insert mode to add new text. Commands can also be given to exit the file (saving or ignoring your edits) in order to return to the UNIX prompt.

You can think of the different modes as representing two different keyboards. In insert mode, your keyboard functions like a typewriter. In command mode, each key has a new meaning or initiates some instruction.

[i] There are several ways to tell vi that you want to begin insert mode. One of the most common is to press i . The i doesn't appear on the screen, but after you press it, whatever you type will appear on the screen and will be entered into the buffer. The cursor marks the current insertion point. To tell vi that you want to stop inserting text, press [ESC] . Pressing [ESC] moves the cursor back one space (so that it is on the last character you typed) and returns vi to command mode.

For example, suppose you have opened a new file and want to insert the word "introduction". If you type the keystrokes iintroduction , what appears on the screen is:

introduction

When you open a new file, vi starts in command mode and interprets the first keystroke ( i ) as the insert command. All keystrokes made after the insert command are considered text until you press [ESC] . If you need to correct a mistake while in insert mode, backspace and type over the error. Depending on the type of terminal you are using, backspacing may erase what you've previously typed or may just back up over it. In either case, whatever you back up over will be deleted. Note that you can't use the backspace key to back up beyond the point where you entered insert mode.

vi has an option that lets you define a right margin and provides a carriage return automatically when you reach it. For right now, while you are inserting text, press [RETURN] to break the lines.

Sometimes you don't know whether you are in insert mode or command mode. Whenever vi does not respond as you expect, press [ESC] once or twice to check which mode you are in. When you hear the beep, you are in command mode.


Previous: 1.2 Quitting Without Saving Edits Learning the vi Editor Next: 2.2 Moving the Cursor
1.2 Quitting Without Saving Edits Book Index 2.2 Moving the Cursor

The UNIX CD Bookshelf Navigation The UNIX CD BookshelfUNIX Power ToolsUNIX in a NutshellLearning the vi Editorsed & awkLearning the Korn ShellLearning the UNIX Operating System










??????????????@Mail.ru