VI EDITOR University of Mississippi Vi Editor • What is Vi ? ▫ Vi is a screen based editor. ▫ The screen of your terminal will act as a window into the file you are editing. ▫ Most commands to Vi move the cursor around the file. ▫ Faster (than pico) for those who can type ▫ Available standard on most Unix systems Vi Editor • Starting the Vi editor ▫ vi lets you create new files or edit existing files. ▫ Command to start Vi : vi ▫ Open existing (or create new) file in vi: vi example1 Vi Editor • Modes of Operation ▫ Command Mode Allows the entry of commands to manipulate text Commands are usually one or two characters long ▫ Insert Mode Puts anything you type on the keyboard into the current file Vi Editor • Modes of Operation ▫ Command Mode Allows the entry of commands to manipulate text Commands are usually one or two characters long ▫ Insert Mode Puts anything you type on the keyboard into the current file Vi Editor • Vi starts in command mode by default • Most commonly used commands to get into insert mode are a and i • Once in insert mode, you get out of it by hitting the Esc key Vi Editor • From within vi: Hit i key to enter edit Insert Mode Type This is EASY. Then hit the escape key, for Command Mode Vi Editor • Saving files and exiting the Vi editor ▫ :q ▫ :q! Quit the editor Quit the editor without saving the changes to the file. ▫ :w Save the file you are editing ▫ ZZ The editing buffer is written to the file only if any changes were made. ▫ :wq :- Same effect as ZZ To ensure you are in command mode: Hit the Esc key twice. To save your file: Type :w to write (save) the file. To save your file under a different name: Type :w example2 To save your file and quite: Type :wq Vi Editor • How to type commands in command mode [count] command [where] count : Its a number where : Specifies how many lines or how much of the document the command affects. It can also be any command that moves the cursor. Vi Editor • Moving from Command to Insert Mode ▫ a ▫ i o Enter insert mode; the characters typed will be inserted after the current cursor position. If a count is specified, then the inserted text will be repeated that many times Enter insert mode, the characters typed will be inserted before the current cursor position. If a count is specified, the inserted text will be repeated that many times Enter insert mode; will start a new line beneath current line. Vi Editor • Moving the cursor, in Command Mode h l j k move cursor to the left move cursor to the right move cursor up move cursor down Use vi to edit example2 to have these 2 lines: This is not hard. This is VERY easy. This is not hard at all. Vi Editor • Some simple Vi Commands ▫r replace one character under the cursor. Specify count to replace that many characters. ▫R Starting from the current cursor position, replace the characters with the one typed on the keyboard ▫x Delete character under the cursor. Count specifies how many characters to delete Practice these commands to further edit example2 Vi Editor • Cutting text d^ Deletes from current cursor position to the beginning of the line d$ Deletes from current cursor position to the end of the line Dw Deletes from current cursor position to the end of the word dd Deletes one line from current cursor position. Specify count to delete many lines. Vi Editor • Yanking and Pasting yl yank a single character. Specify count to yank more characters yw yank a single word. Specify count to yank more words yy yank a single line. Specify count to yank more lines p m paste the text that was either deleted or yanked previously mark the current line Practice Yanking, Deleting, and Pasting Lines of Text Use vi to create a new file: example3, with these lines: One Two Three Four Five Six Seven Eight Nine ten Then, from command mode, enter :wq to save the file Vi Editor • To go to a specific line in the file :linenumber • To go to the last line in the file :G • String Search ▫ /[pattern] : search forward for the pattern ▫ ?[pattern] : search backward for the pattern • To undo the last command: More practice Use vi to edit existing file: example3 …