!!!VI Editor Standard visual text editor available on all Unix / Linux computer systems. It also comes with Mac OS/X, and is available for Windows. The VI editor has three main modes: * command mode * edit mode (insert or overtype text) * ex mode When you first enter VI you are in command mode. Edit mode is used to insert or overwrite new text. Command mode is used for almost everything else. Knowing which mode you are in at any point is critical. You can always hit the Esc key to go from edit mode to command mode. Hitting Esc when you are in command mode does nothing so if you are confused just hit the Esc key and you'll know you are in command mode. __Edit mode__ -> __command mode__ = Esc __Command mode__ -> __ex mode__ = : Commands can be proceeded with a number. This causes that command to be executed the specified number of times. For example 5dd will delete 5 lines. ^ means control, so ^f means control-f Commands are case sensitive so that j and J are different commands. !!Commands (run from command mode) !!Saving, exiting, and file commands |ZZ|save and quit |:q|quit without saving (only works of file had not been edited) |:q!|quit without saving (even if file had been changed - lose changes) |:w|write (save) file but don't quit |:w newname|write (save) file to new file named newname |:wq|write (save) and quit |:r filename|read (insert) file filename into the current buffer after the current line !!Cursor movement (The letters hjkl on the keyboard form a sort of arrow keys)\\ |h| left one character |j| down one character |k| up one character |l| right one character See Searching below. |return|first character of next line |+|first character of next line |-|first character on previous line |w| word right |b| back a word |0| beginning of line |$| end of line |G| goto end-of-file |1G| goto beginning of file |15G| goto line 15 (basically you can preced G with any line number) |H|home - top of screen |M|middle of screen |L|last line on screen !!Screen movement (scrolling) |^f| forward a page |^b| back a page |z return|scroll current line to top-of-screen |z.|scroll current line to middle-of-screen |z-|scroll current line to bottom-of-screen !!Text editing (all text entry except r ends by hitting the Esc key) |i| inserting text starting before the cursor position |a| append text after the cursor position |r| replace a single character |R| replace or over-type (doesn't require Esc key to end) |A| append starting at the end-of-line |o| add text after the current line (open a line) |O| add text before the current line |cw| change word |S| replace the current line with entered text !!Deletion |x| delete one character |dd| delete current line |dw| delete word |d$| delete to end-of-line |D| delete to end-of-line (Remember that any command can be preceded by a number so things line 5dd means delete 5 lines, 7x means delete 7 characters) !!Searching |/xxxxx<return-key>| search forward for regular expression xxxxx |?xxxxx<return-key>| search backwards for regular expression xxxxx |n| repeat previous search Vi's regular expression syntax corresponds to grep's basic regular expression syntax. See [grep] !!Cutting, copying and pasting Cutting is done with the delete commands listed above.\\ |yy| yank (copy) line |yw| yank (copy) word |y$| yank (copy) to end-of-line |p| Put or paste. Basically it pastes the last line (or group of lines) after the current position. |kp| Useful after a delete if you didn't really want to delete but just copy the text. The kp puts it back where it was but remembers what was deleted. Remember that these commands can be proceeded with a number to get a group of words or a group of lines. !!Misc |J| join the next line to the end of the current line |u| undo last edit |:e!|undo all changes since last save |.| repeat last text-changing command |:.=| display current line number |:set autoindent|turn on auto-indent mode |:set noautoindent|turn off auto-indent mode |^L|redraw screen |! cmd|run cmd in shell and insert result into buffer ---------------------------------------------------------------------- !!!Advanced Commands !!Using variables (or named buffers) Copying and deleting text into variables so that you can have several pieces of text you are moving around. It's like being able to delete or copy text into a variable that you name, and then being able to pasted text that was put into a variable. Variable names are always single, lower-case letters so that you can only have 26 variables. In what follows X will be used to signify the variable name you are using. So, for example when you see X in a command below you will really put in your variable name (a through z). All copy "y" and delete "d" commands can be proceeded by "X\\ That is double quote and your variable name. So, for example |"wdd| would delete the current line and put it in the variable named "w". |"v5dd| would delete the next 5 lines and put it into the variable named "v" |"r3yw| would copy 3 words and put them in the variable named "r" |"vp| would paste the contents of the variable named "v" !!Marking positions Marking a position allows you to save your current position into a variable. You can later cause the system to return to that exact position. |mX| save the current position in variable named at X |`X| return to position saved at variable named X for example: |mh| save my current position in variable named "h" |`h| return to position saved in variable named "h" !!Copying text between two files # Use any of the delete or copy (yank) commands with or without named variables. # Save the current file with :w # Edit another file with :e newfile # Paste as normal !!Search and Replace (Substitute) In what follows:\\ |s| substitute |g| global |%| all lines |c| confirm !Commands: |:s/OLD/NEW/| substitute the first occurrence of OLD with NEW on current line |:s/OLD/NEW/g| substitute all occurrences of OLD with NEW on current line |:%s/OLD/NEW/g| substitute all occurrences of OLD with NEW on all lines |:50,100s/OLD/NEW/g| substitute all occurrences of OLD with NEW on lines 50-100 |:%s/OLD/NEW/gc| substitute all occurrences of OLD with NEW on all lines with confirmations !!Multiple Files Vi can edit multiple files. When initially calling up VI, it may be called with multiple file names on the command line. VI supports the following commands to handle multiple files: |:n|next file is switched to |:w|save the current file |:e newfile|edit file named newfile