- h => move the cursor left one character
- j => move the cursor down one line
- k => move the cursor up one line
- l => move the cursor right one character
- e => move to the end of a word
- w => move forward to the beginning of a word
- b => move backward to the beginning of a word
- $ => move to the end of the line
- 0 => move to the beginning of the line
- ^ => move to the first non-blank character of the line
- G => jump to end of file
- % => jump to corresponding item, e.g. from an open brace "{" to its matching closing brace
- i => insert at the cursor
- I => insert at the begin of line
- a => append after the cursor
- A => append at the end of line
- o => open blank line below current line
- O => open blank line above current line
- [Esc] => exit insert mode
- r => replace a single character
- R => replace characters
- J => join line below to the current one
- cw => change to the end of word
- ct[char] => change till character
- C => change to the end of line
- s => substitute a single character
- S => substitute current line
- u => undo
- . => repeat last command
- :[range]j => join lines in range
- J => join the next line
- d => delete
- c => change
- y => yank
- dw => delete a word
- dG => delete from current line to the end of file
- dgg => delete from current line to the top of file
- . => repeats the last change made in normal mode. (ie. use "dd" to delete line then use "." to repeat "dd")
- 5. => repeats 5 times the last change made in normal mode
- Ctrl-A => increment the next number at the cursor
- Ctrl-X => decrement the next number at the cursor
- yy or Y => yank a line
- 2yy => yank 2 lines
- p => put after cursor
- P => put before cursor
- x => delete a character
- dw => delete the current word
- dd or D => delete a line
- :e filename => open filename for edit
- :w => write file
- :w filename => write to filename
- :q => quit
- :q! => quit without saving
- :x => write file if changed then quit
- [count]"{a-z}yy => yank(copy) [count] lines to register {a-z}
- [count]"{a-z}dd => yank(copy) [count] lines to register {a-z} and delete [count] lines
- [count]"{A-Z}yy => append [count] lines to register {a-z}
- [count]"{A-Z}yy => append [count] lines to register {a-z} and delete [count] lines
- :[range]y {a-z} => yank(copy) lines in range to register {a-z}
- :[range]y {A-Z} => append lines in range to register {a-z}
- "{a-z}p => put lines from register {a-z} below current line
- "{a-z}P => put lines from register {a-z} above current line
- 3"ayy => yank 3 lines from current line to register 'a'
- 9,15d A => append line 9 to 15 to register 'a' and delete line 9 to 15
- "ap => put lines from register 'a' below current line
- :[range]s[ubstitute]/{pattern}/{string}/[flags] [count]
- % => perform the command all lines
- x,y => perform the command from line number x to line number y
- [@flags]
- c => confirm each substitution
- g => replace all occurrences in the line
- ^ => begin of line
- $ => end of line
- . => any character
- * => unlimit number repeated
- "\<" and "\>" => word boundary
- "\{" and "\}" => pattern repetition with number repeated, \{5\} repeat pattern five times
- "\(" and "\)" => define backreference
- "\1", "\2", ... => get first backreference, second backreference, and so on
- :s/\<his\>/her/ => substitute only the whole word "his" with "her" (will not change "history" to "hertory")
- :s/\(abc\)\{2\}/def\1/g =>substitute all ocurrences of pattern "abcabc" (two "abc") with "defabc" ("def" follow by first backreference)
- :%s/^foo.*bar$/baz/ => substitute all the lines have "foo" at begin of line, follow by any characters, "bar" at the end of line with "baz"
- :s/,B,/,^MB,/g => substitute all ocurrences of pattern ",B," with "," + new line + "B," <= ^M is new line (ctrl-v then ctrl-m)
- use sed to substitute all ocurrences of pattern ",B," with "," + new line + "B,"; use this command => sed 's/,B,/,\^JB,/g' filename <= ^J is new line (ctrl-v then ctrl-j), \ before ^J is line continuation character
- :%!sort => sort all line
- :%!uniq => remove duplicate line
- :%!uniq -c -d => count and print duplicate line only
- :[range]g/<pattern>/command => execute command on the line where pattern matches
- :[range]v/<pattern>/command => => execute command on the line where pattern not match
- :g/baz/s/foo/bar/g => substitute "foo" with "bar" all the lines have "baz"
- :v/baz/s/foo/bar/g => substitute "foo" with "bar" all the lines have not "baz"
- :g/^$/d => delete all blank lines
- :g/^/m0 =>move each line to begin of file
- :g/baz/m$ => move all lines have "baz" to the end of file
- :g/baz/t$ => copy all lines have "baz" to the end of file
- :g/baz/y A => copy all lines have "baz" to register 'a'
- start recording by press 'q', followed by a lower case character to name the macro
- perform any typical editing, actions inside vim, which will recorded
- stop recording by pressing 'q'
- play the recorded macro by pressing '@' followed by the macro name
- to repeat macros multiple times, press : 'NNN @ macro name'.NNN is a number
- qaYp<C-A>q => increasing numbers from "101 qa...q" at current line. <C-A> is Ctrl-A.
- 98@a => repeats the macro 'a' 98 times.
[ ตั้งค่าให้แสดงสี syntax บน vim ]
file: ~/.vimrc
content:
colorscheme koehler let &t_Co=256 let &t_AF="\e[38;5;%dm" let &t_AB="\e[48;5;%dm" syntax on
อ้างอิง:
- My vi/vim cheatsheet
- how to do a vim search inverse search for all lines with out text
- 130+ essential vim commands
- Search and replace - Vim Tips wiki
- Power of g - Vim Tips wiki
- Increasing or decreasing numbers
- Vi and Vim Macro Tutorial: How To Record and Play