Sunday, December 6, 2015

vim notes

1. Movement:
  • 0 or HOME or| jump to the beginning of line
  • ^ or == jump to the first non-blank character of line
  • $ or END jump to the end of line
  • g0, 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.
  • w to the next word
  • W to the next space-separated word
  • b / e to the begin/end of the current word. (B / E for space separated only)
  • gg / G jump to the begin/end of the file.
  • % jump to the matching { .. } or ( .. ) 
A nice keymap is:noremap % v%
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
Movement in Visual Mode
  • 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
After selecting the text, try d to delete, or y to copy, or :s/match/replace/, or :center, or !sort, or...

  • 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/d delete selected text
  • 7) v {select} X/D delete 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
  • a append after cursor
  • A append at the end of line
  • i insert before the cursor
  • I insert before the first non-blank character of line
  • gI insert at the begin of the line
  • o/O open a new line below/above the current.
  • C change to the end of line(same as c$)
  • c^ change to the begin(first non-blank character) of line
  • s change one character(same as cl)
  • S change 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
  • r replace char under cursor, e.g ra will replace char under cursor to character 'a'
  • R Enter insert mode, replacing characters(starting from cursor) instead of inserting
  • ~ Switch case of character under cursor
  • g~~ or g~g~ Switch case of whole line 
  • gUU / guu Change whole line to uppercase/lowercase
  • gUw / guw Change word to uppercase/lowercase
  • J Join Lines without space
  • gJ Join Lines with space
2) Delete
  • Del or x delete character under cursor
  • X delete character before cursor
  • d {motion} delete text that {motion} moves over.
    e.g: dl delete current char, dh delete before char
           dt<char> delete to char
  • dd delete current line
  • D delete to the end of line
  • dw delete word(dw,3dw,d3w,3d2w,d$,d^,df)
  • diw delete word under cursor(excluding whitespace)
  • daw delete word under cursor(including whitespace)
  • dG / dgg delete until the end/start of file
Search and Replace:
  • / search forward
  • ? search backward
  • :s/search/replace replace only the first occurrence
  • :s/search/replace/g replace all occurrence of current line
  • :%s/search/replace/g replace all occurrence of the file
  • :%s/search/replace/gi replace all occurrences, case sensitive!
  •  :%s/search/replace/gic replace all occurrences, case sensitive and ask confirmation!
  • :5,12s/search/replace/g replace all occurrences between line 5 and 12
  • :'a,'bs/search/replace/g replace all occurrences between mark a to mark b
  • :'<,'>s/search/replace/g replace all occurrences within visual selection
  • :.,$s/search/replace/g replace all occurrences between current line(.) and the last line($)
  • :.,+2s/search/replace/g replace all occurrences between current line(.) and the next two lines
  • :g/^baz/s/search/replace/g replace 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

  • u undo one change a time
  • U undo all changes of current line
  • Ctrl R redo
Combining commands
Most commands accept a amount and direction, for example:
  • cW = change till end of word
  • ciW = change inner word within () or other brackets
  • BcW = to begin of full word, change full word
  • 3cW = change 3 words.
  • ci" = change inner between ".."
  • ci( = change text between ( .. )
  • ci< = change text between < .. > (needs set matchpairs+=<:> in vimrc)
  • 4dd = delete 4 lines
  • 3x = 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 q to 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