or0orHOMEjump to the beginning of line|^or == jump to the first non-blank character of line$ or ENDjump to the end of lineg0, g<HOME>, g^, g$, and g<END>tackle line wrap-/+cursor move to first non-blank of previous/next line*and#search for the word under the cursor forward/backward.wto the next wordWto the next space-separated wordb/eto the begin/end of the current word. (B/Efor space separated only)gg/Gjump to the begin/end of the file.%jump to the matching { .. } or ( .. )
This way, whenever you type % you jump to the matching object, and you visually select all the text in between. It's useful for indenting a C/C++ method/class: go to opening/closing brace, and type =%
{/}jump to next blank line.'.jump back to the first character of last edited line.- g; jump back to last edited position.
- gi jump back to last edited position and enter into Insert Mode.
- `. jump to the last edit position
- `` jump to the previous position you were at
- f<char> / F<char> jump to the next/previous occurring char on the current line, and ;/, to repeat forward/backward
- t<char> / T<char> jump to one position before the next/previous occurring char of the current line, and ;/, to repeat forward/backward
- Ctrl-o / Tab jump to previous/next location
- Ctrl-g where I am in the file
- z-Enter top the screen on current line
- :set scroll=10
:set scrolljump=5
:set scrolloff=3
Screen movement
- z-z / z-. center the screen on the cursor
- z-t top the screen on the cursor(the line will at the top of the screen)
- z-b bottom the screen on the cursor(the line will at the bottom of the screen)
- Ctrl-y Moves screen up one line
- Ctrl-e Moves screen down one line
- Ctrl-u Moves screen up ½ page
- Ctrl-d Moves screen down ½ page
- Ctrl-b Moves screen up one page
- Ctrl-f Moves screen down one page
- o/O If you've got a visual selection and want to adjust it, o/O will hop to the other end.
Selection in Visual Mode
- 1) v select any range
- 2) V select entire line
- 3) Ctrl-v select columns
- 4) gv select previous selected block
- 1) Select all text in (), {}, [], <>, '', "", etc. block, vi<(>, ( could be any of those characters.
- 2) Especially, for () and {}, we can use vib and viB to select text in these blocks if our cursor is in the block.
- 3) If cursor is just on ( or ), v% will select text between and also ( and )
- 4) vap selects the entire paragraph
- 5) ggVG selects the entire file
- 6)
v {select} x/ddelete selected text - 7)
v {select} X/Ddelete selected lines - 2) Delete Selected Block: v<SelectedBlock> d.
- 3) Delete Selected Block and then Insert from here: v<SelectedBlock> di. A much quicker way of doing this is v<selected_block>c
- Especially, if you are going to change the text between (), {}, [] <>, '', "", it is more straightforward to just do ci<(> , here ( can be any of the above characters. No need to select first if you are going to replace it.
sumerizaztion:
- vi(, vi[, vi{, vi", vi', viw: all these will not include
- va(, va[, va{, va", va', vaw: all these will include
2. Editing(selection, edit, delete):
1) Insert/Change-Edit
1) Insert/Change-Edit
aappend after cursorAappend at the end of lineiinsert before the cursorIinsert before the first non-blank character of linegIinsert at the begin of the lineo/Oopen a new line below/above the current.Cchange to the end of line(same as c$)- c^ change to the begin(first non-blank character) of line
schange one character(same as cl)Schange a whole line(same as cc)
ct<char>change to char, not include char- df<char>, dF<char>, delete include char
- dt<char>, dt<char> delete not include char
rreplace char under cursor, e.g ra will replace char under cursor to character 'a'REnter insert mode, replacing characters(starting from cursor) instead of inserting~Switch case of character under cursorg~~org~g~Switch case of whole linegUU/guuChange whole line to uppercase/lowercasegUw/guwChange word to uppercase/lowercaseJJoin Lines without spacegJJoin Lines with space
2) Delete
Delorxdelete character under cursorXdelete character before cursord {motion}delete text that {motion} moves over.
e.g: dl delete current char, dh delete before char
dt<char> delete to chardddelete current lineDdelete to the end of linedwdelete word(dw,3dw,d3w,3d2w,d$,d^,df)diwdelete word under cursor(excluding whitespace)dawdelete word under cursor(including whitespace)dG/dggdelete until the end/start of file
Search and Replace:
Edit File:
/search forward?search backward:s/search/replacereplace only the first occurrence:s/search/replace/greplace all occurrence of current line:%s/search/replace/greplace all occurrence of the file:%s/search/replace/gireplace all occurrences, case sensitive!-
:%s/search/replace/gicreplace all occurrences, case sensitive and ask confirmation! :5,12s/search/replace/greplace all occurrences between line 5 and 12:'a,'bs/search/replace/greplace all occurrences between mark a to mark b:'<,'>s/search/replace/greplace all occurrences within visual selection:.,$s/search/replace/greplace all occurrences between current line(.) and the last line($):.,+2s/search/replace/greplace all occurrences between current line(.) and the next two lines:g/^baz/s/search/replace/greplace all occurrences starting with 'baz' in each line
Edit File:
:e [otherfile]edit current file if not specify otherfile:e! [otherfile]edit current file if not specify otherfile, discard all changes:r [otherfile]insert otherfile to the cursor position
Undo/Redo
uundo one change a timeUundo all changes of current lineCtrl Rredo
Most commands accept a amount and direction, for example:
cW= change till end of wordciW= change inner word within () or other bracketsBcW= to begin of full word, change full word3cW= change 3 words.ci"= change inner between ".."ci(= change text between ( .. )ci<= change text between < .. > (needsset matchpairs+=<:>in vimrc)4dd= delete 4 lines3x= delete 3 characters.3s= substitute 3 characters.
Useful programmer commands
- Ctrl+A / Ctrl+X increments/decrements a number.
.repeat last command (a simple macro)==fix line indent>indent block (in visual mode)<unindent block (in visual mode)
Macro recording
- Press
q[ key ]to start recording. - Then hit
qto stop recording. - The macro can be played with
@[ key ].
q: It opens the "command window" and shows your most recent ex-mode (command-mode) commands. You can move as usual within the window, and pressing
<CR>executes the command. You can edit, etc. too
No comments:
Post a Comment