Vous êtes sur la page 1sur 3

The Vi Editor

Introduction and getting started

Vi is traditionally one of the most widely-used text editors in UNIX, although some of its functionality is now thought slightly outdated. From a UNIX prompt, you can edit a new or existing le by typing % vi lename Sometimes you will have been disconnected from a vi session without having had a chance to save your changes. To recover your previous vi session type % vi -r lename

Basic Vi commands

Vi has two distinct modes of operation. You can either be in a mode where key-strokes are interpreted as editing commands and what you type will not appear on the screen or you are in an editing mode, where what you type appears on the screen and none of the editing commands will work. To switch from an editing mode to a command mode is simple press ESC. If you are ever in doubt about which mode you are in, hit the ESC key and then issue an command to enter an editing mode. You can switch into several editing modes, listed below. Vi editing modes command i a I A o O r R short description Insert mode: text inserted before cursor. Terminated by ESC. Append mode: text inserted after cursor. Terminated by ESC. Insert mode: text inserted at beginning of line. Terminated by ESC. Append mode: text inserted at end of line. Terminated by ESC. Open mode: Opens a new line below current line. Terminated by ESC. Open mode: Opens a new line below current line. Terminated by ESC. Replace mode: replaces the single character under the cursor with the next character typed. Replace mode: overwrites on top of characters. Terminated by ESC

Remember, to leave an editing mode, press ESC.

Here are some more basic commands that you will need. Moving around command short description j (down arrow) move cursor down one line k (up arrow) move cursor up one line h (back arrow) move cursor back a character l (forward arrow) move cursor forward a character :n move to line n b move cursor backwards to begininning of previous word w move cursor forwards to begininning of next word ^D (Pg Dn in vim/gvim) Scroll down a page. ^B (Pg Up in vim/gvim) Scroll up a page. :$ move to end of le Editing commands command short description [n]x Delete n characters after and including the one under the cursor. (You normally omit the number n) X Delete character before the cursor. u Undo last change. In vim and gvim you can do multiple undos ^R Recover an undo. If you like, undoes and undo. In vim and gvim you can do multiple recoveries. nn dd Deletes the next n lines including the one with the cursor. (Here the number n is useful for deleting a chunk of text) D Deletes from the cursor to the end of the line p Puts back the last deleted/copied text after the cursor/after the line P Puts back the last deleted/copied text before the cursor/after the line. J Join the next line to the end of the current line. cc changes the current line. nn yy Yanks (or copies) the next n lines including the one with the cursor. You can then move to another part of the text and press p or P to paste it back into the text. :set number Shows line numbers. :set nonumber Doesnt show line numbers. File commands command short description ZZ or :wq exit and save. :w save without exiting. :q! quit without saving. :w lename write to lename. :r lename insert contents of lename into line below current line.

After issuing any of these commands, you need to remember to issue a key-stroke to get into one of the editing modes to continue processing your text.

There are many other more advanced vi commands that can be used. These are only the basic commands, but they tend to be the only ones that youll need for eective editing. Some other very useful commands are listed below. Search and replace command short description /expr nds next occurrence of string expr (subsequently, you can use / to nd the next occurrence without having to type it every time) ?expr nds previous occurrence of string expr :%s/expr1/expr2/g replaces all occurrences of the string expr1 with expr2 :%s/expr1/expr2 replaces the rst occurrence in each line of the string expr1 with expr2 :.,$s/expr1/expr2/g replaces all occurrences of the string expr1 with expr2 from the point at which the cursor is to the end of the le

Using gvim

In many ways, using gvim instead of vi can be easier. It is a GUI, and so has drop down menus and a scrollbar built in and allows more intuitive movement through your document. This allows cutting and pasting to be done without learning all the dierent key strokes, although you will still need to know the basics of vi and it is certainly advisable that you try gvim only after you have a fairly good grasp of vi. If you want to use gvim just type % gvim lename from the command prompt.

Vous aimerez peut-être aussi