vi command

[cursor movement]
  • 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
[insert mode]
  • 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
[command 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
 [text]
  • :[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
[copy-cut-paste]
  • 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
[file]
  • :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
 [register]
  • [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
= example =
  • 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
[substitution]
  • :[range]s[ubstitute]/{pattern}/{string}/[flags] [count]
[@range]
  • % => 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
[@pattern]
  • ^ => 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
= example =
  • :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
  [global(g) and reverse(v) command]
  • :[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
= example =
  • :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'
[macro record and play]
  1. start recording by press 'q', followed by a lower case character to name the macro
  2. perform any typical editing, actions inside vim, which will recorded
  3. stop recording by pressing 'q'
  4. play the recorded macro by pressing '@' followed by the macro name
  5. to repeat macros multiple times, press : 'NNN @ macro name'.NNN is a number
= example =
  • 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

อ้างอิง:
--