ICSA700 Intro. to the unix editor - vi Phil White To edit a file vi filename To exit vi and save changes ZZ or :wq To exit vi without saving changes :q! vi has three modes mode enter exit ---------------- ------------------ --------------------- command mode [esc] any method of entering the other two modes edit mode i, a, o, O [esc] command line mode : (from command mode) [return] [esc] When vi begins editing a file it starts in command mode Edit mode only lets you edit one line at a time (can't use arrow keys to move around in the file). To move around, you have to be in command mode. In command mode to move around you use the following commands: h or left arrow move left one character j or down arrow move down one line k or up arrow move up one line l or right arrow move right one character $ move to end of current line 0 (zero) move to beginning of current line w move to beginning of next word b move to beginning of previous word ^f (control f) move forward one screen ^b move backward one screen G move to the last line of the file From command mode, to enter edit mode use one of the following commands: i insert text before cursor a insert text after cursor o insert text on a new line below where the cursor is O (cap. "O") insert text on a new line above where the cursor is r replace one character at the cursor (place you back in command mode) R keep replacing characters until [esc] is hit To exit edit mode from edit mode hit [esc]. In command mode to delete text: x delete one character dd delete the current line dw delete the current word To copy and paste lines use: yy to copy the current line dd to cut the current line (same command as delete) p paste cut or copied line(s) before current line P paste cut or copied line(s) after current line mx will mark a line with the character x 'x will move the cursor to mark x y'x will copy lines from the current line to mark x d'x will cut lines from the current line to mark x NOTE The mark (x above) can be any letter a-z and is not written to the file From command mode to enter command line mode type : enter command line mode From command line mode you can execute the following commands wq save current file and quit w save the current file without quitting w filename save the current file as a new file called "filename" q quit without saving (if you have made no changes) q! quit without saving changes r filename read in the file "filename" and put it into current file after the line with the cursor # (number) move to line number # When you leave command line mode you will be placed back in to command mode. To find a word use / text to search for the word "text" forward in the file ? text to search for the word "text" backward in the file