|
|
(11 intermediate revisions by 3 users not shown) |
Line 1: |
Line 1: |
| ==Insert/Command States==
| | Page moved to: [http://genomewiki.ucsc.edu/genecats/index.php/VI_quick_start VI quick start] |
| | |
| vi is usually either in an 'insert' state, or in a 'command' state. It starts up in the 'command' state.
| |
| There are other states that I do not use but I sometimes enter them accidentally and need to use the :q
| |
| to get out of those states.
| |
| | |
| There is a lot more to vi than indicated here. This is a very brief cheat-sheet, but even with this
| |
| small set of commands, vi is a powerful tool for managing documents.
| |
| | |
| To enter the 'insert' state from the 'command' state, issue some kind of insert editing command.
| |
| When you are in 'insert' state, whatever you type becomes text in the document. To exit 'insert'
| |
| state and return to 'command' state, press the 'Esc' key. When in doubt about the state,
| |
| press the 'Esc' key to get to 'command' state. Your terminal should indicate the INSERT
| |
| state with a message at the bottom of the screen:
| |
| -- INSERT --
| |
| | |
| The bottom line of your terminal when in 'command' state will indicate the line and column
| |
| position of the cursor, and a percent indication of how far down you are in your document.
| |
| | |
| ==Starting vi==
| |
| | |
| vi file.ext # start editing the file called file.ext - whether it exists now or not
| |
| vi -r file.ext # recover the edits to a file. The previous session to vi was lost due
| |
| # to login disconnect or system crash. This will resume close to where you where
| |
| # when vi died. After this recover and exit from vi, remove the .file.ext.swp file
| |
| # which is used by vi for its housekeeping.
| |
| vi -x secure.txt # edit a file in secure encoded form. You will need to establish a password to use
| |
| # this file in vi. The contents are only seen in a vi -x session, otherwise they
| |
| # are encoded and can not be read by others unless they know the password
| |
| | |
| ==Exit vi==
| |
| | |
| :wq # write file and quit vi - saves all edits to file being edited
| |
| :q! # abandon edits since last file write and quit vi
| |
| :w # simply write out file contents saving all edits to this point. Remain in vi
| |
| | |
| ==Cursor movement==
| |
| | |
| Cursor movement commands are given when in the 'command' mode.
| |
| | |
| It is vital to learn cursor movement keys to rapidly move around your document. You probably
| |
| shouldn't be using arrow keys repeatedly to move the cursor one character at a time unless that
| |
| is really what you need to be doing at that moment. It is much more efficient to move the
| |
| cursor based on objects in the document. Adding a number in front of any of these commands multiplies
| |
| their operation by that number.
| |
| | |
| nG # where n is a number of digits to number a line, G goes to that line number
| |
| G # go to the end of the document
| |
| 1G # go to the top (line 1) of the document
| |
| w # move forward to the beginning of the next word
| |
| W # move forward to the beginning of the next string
| |
| b # move back to the beginning of the current word
| |
| B # move back to the beginning of the current string
| |
| e # move to the end of the current word
| |
| E # move to the end of the current string
| |
| 0 # go to the beginning (column 1) of this line
| |
| $ # go to the last character on this line
| |
| <CR> # go to the beginning (column 1) of the next line
| |
| - # go to the beginning (column 1) of the previous line
| |
| <space> # move forward one character
| |
| Ctrl-h # move back one character
| |
| Ctrl-f # scroll to next page (one screen full minus two lines)
| |
| Ctrl-b # scroll back one page (one screen full minus two lines)
| |
| Ctrl-d # scroll down one half page (one half screen)
| |
| Ctrl-u # scroll up one half page (one half screen)
| |
| z. # scroll the document to move the line the cursor is on to the middle of the screen
| |
| % # move to associated ( ) { } [ ]
| |
| /string # go forward to the beginning of 'string' - great for moving around the document
| |
| # on the screen when it is somewhere distant from where the cursor is, especially when
| |
| # 'string' is short and unique
| |
| ?string # go backward to the beginning of 'string' - same benefit as above, but in reverse
| |