Vi/Vim SURVIVAL GUIDE
- The cursor is moved by using either the arrow keys or the hjkl keys.
- h (left)
- j (down)
- k (up)
- l(right)
- To exit vim
:q! Type x to delete the character below the cursor, X deletes the character to the left of the cursor. - To insert or append text type:
- i type inserted text
insert before the cursor - I type inserted text
insert at the beginning of the current line - A type appended text
append after the line - a type appended text
append immediately after the cursor
- i type inserted text
- To insert an empty line below the cursor type and go to insert mode: o (lowercase o)
- To insert an empty line above the cursor type and go to insert mode: O (capital O)
- To delete from the cursor up to the next word type: dw
- To delete from the cursor up to the end of a line type: d$
- To delete a whole line type: dd
- To repeat an action prepend it with a number: 2w
- To move to the start of the line use a zero: 0
- To move to the end of the line use dollar sign: $
- To undo previous actions, type: u (lowercase u)
- To undo all the changes on a line type: U (capital U)
- To redo (undo the undo's) type: CTRL-R
- To put back a text line that has just been deleted, move the cursor on the line above the deleted line and type p.
- To replace a character,
move the cursor on that character, type r and then then the new character.
type R to go Replace mode and to replace more than one characters. - Type ce to change the characters in a word, ce deletes the word and places you in Insert mode. cne changes n number of words. c$ changes uptil the end of the line.
- CTRL-G displays your location in the file and the file status(in terms of percentage). :f provides the same result as CTRL-G.
G moves to the end of the file.
number G moves to that line number.
gg moves to the first line. - Typing / followed by a phrase searches FORWARD for the phrase.
Typing ? followed by a phrase searches BACKWARD for the phrase.
After a search type n to find the next occurrence in the same direction
or N to search in the opposite direction.
CTRL-O takes you back to older positions, CTRL-I(Capital "EYE") to newer positions. - Typing % while the cursor is on a (,),[,],{, or } goes to its match.
- To substitute new for the first old in a line type :s/old/new
To substitute new for all 'old's on a line type :s/old/new/g
To substitute phrases between two line #'s type :#,#s/old/new/g
To substitute all occurrences in the file type :%s/old/new/g
To ask for confirmation each time add 'c' :%s/old/new/gc - :!command executes an external command.
for example :!ls or :!rm FILENAME - :w FILENAME writes the current Vim file to disk with name FILENAME
- Type v to go to visual mode,to select some text, while in visual mode, type :w FILENAME saves the visually selected text in file FILENAME.
- :r FILENAME retrieves disk file FILENAME and puts it below the cursor position
- :r !dir reads the output of the dir command and puts it below the cursor position
- In visual mode, type y to copy selected text and p to paste it to a desired location.
- Typing :set 'xxx' sets the option xxx. Some options are:
ic (ignore case) ignores upper/lower case while searching
hls (highlight search) highlight all matching phrases.
cp (Compatible mode) :set [no]cp
Prepend "no" to switch an option off: :set noic - Type :help(within Vim) to open a help window, (:q to exit the help window)
- Type CTRL-W CTRL-W to jump to another window
2 comments:
Thanks! :)
User can type vimtutor on command prompt and can get a simple tutorial on vi editor. The facility it provides is that it gives inline practice/implementation space to try those commands.
Post a Comment