Friday, August 30, 2013

My Own cheat-sheet of Vim commands

Shawn's Cheat sheet of Vim

[count] means starting with an number
[move] means a movement command follows
=========================================
CURSOR MOVEMENT
=========================================
[count]
Word movement:
w - forward begining of one word
b - backward begining of one word
e - forward end of one word

[count]
Line movement:
$ - go to end of this line
^|0 - go to start of this line
int+G - go to line number 'int'

[count]
Searching:
f(F)+char - forward(backward) serarch 'char'
t(T)+char - forward(backward) reach the character befor 'char'

Screen movement:
ctrl+f - scroll down a screen, page down
ctrl+b - scroll up a screen, page up
ctrl+u - scroll up half of a screen
ctrl+d - scroll down half of a screen
ctrl+y - scroll down one line
ctrl+e - scroll up one line
ctrl+m/n - scroll down a line
{/} - scroll up/dowm a paragraph
[[ - go to previous section of document, seperated by ctrl+L or {}
]] - go to next section of document

=========================================
TEXT EDIT
=========================================
[count][move]
Deletion:
d - delete characters specified by movement, e.g, dd - delete a line, d3w - delte following 3 words, df< - delete characters until the next <
c - delete and enter insertion mode (like 'change'), e.g, cc - delete a line and enter insertion mode, cw - delete a following word and enter insertion mode

Do/Undo
u - undo
U - undo for current line
ctrl + r - redo

[count]
Joint lines
J(upper case) - joint the current line with the next one.

[count]
Change case:
~ - change lower to upper and vice versa

[Insert] -- insertion mode
Auto word completion
ctrl+n/ctrl+p
========================================
MACRO
========================================
Macro is a seris of key strokes

[count]
press q + a to start recording macro in register a, press q to end recording
@ + a to apply macro stored in register a

System clipboard register
"* - In visual mode, highlight text and copy them into system clipboard using "*y. To paste text from system clipboard, use "*p

========================================
Digraphs
========================================
In insert mode, press ctrl+k and press two key combinations. Type :digraphs to view complete set of supported characters


========================================
Search
========================================
Search for string
/string - forward searching a string pattern.
?string - backwar searching a string pattern

[count]
n - find the next[count] (forward/backward) match
N - find the previous[count] (forward/backward) match

Set searching options:
:set (no)hlsearch - hightlight searching results
:set incsearch - performs incremental search

Regular experssions:
[\] begin with backward slash if want to use normal characters
^ - match beginning of a line
$ - match end of a line
. - match any single character

========================================
Cut,Paste and Copy
========================================
Cut
[count]
d,c - delete & store text into register

Paste
p - paste the deleted text after the cursor (processed by x,d,c command)
P - paste thr deleted text before the cursor

Copy
[count]
y - behaves like d,c with the exception that text is not deleted, can be recovered with p

Mark
Mark is a cursor place, can be used in combination with d,c command. Mark goes with text even if it is moved to another place.
m + a~z - mark the place (therefore totally 26 marks)
`+ a~z , '+ a~z - go to marking place (' goes to the marked line)
:marks - list all the marks


========================================
Filtering
========================================
Put part of text as input to a system command, and let the output of system command to replace selected part.
![range] motion - specify the end of selected part starting from current cursor. Then enter the system command to finish. e.g. !1Gsort<Enter>
!! - run current line through a filter. Let the system command output onto current line. e.g, !!ls, !!date

========================================
Multiple files
========================================
:vim + filename - open file
:view + filename - open file in read-only mode
:next - go to the next file
:previous - go to the previous file

For editing two files
ctrl_^ - alternate between two files


========================================
Multiple windows
========================================
Open new window
:split + filename - open a file in a new window
:split + [command] - open a new window and excecute an initial command
:new - open a new file in a new window

Alternate / close windows
:ctrl-ww - alternate between two windows
:ctrl-wj - move to bottom window
:ctrl-wk - move to top window
:ctrl-wc - close current window

Change window size
[count]Ctrl-w+/- - increase / decrease window size

[split]add 's' to the commands split a new window
Buffer:
The file being edited has a buffer.(Show on screen).Often used to edit across multiple files.
:buffers - list all buffers
:hide - hide current buffer
:buffer number/file - select a buffer by number or filename (number preceeds 'buffer')
buffer state
active - current buffer shown on screen
hidden - marks and edit retained
inactive - marks retained. Edit info. discarded

========================================
Visual Mode
========================================
Upper case command perform edit on full line
Enter visual mode
v - character-by-character visual mode
V - full line mode
ctrl+v - rectangular block mode

Deletion
d - delete highlighted text
D - delete highlighted line

Copy
y - copy highlighted text into register
Y - copy highlighted line into register

Change
c - change highlighted text and enter insertion mode. In visual block mode, changes apply at each line of highlighted block.
C - change highlighted line and enter insertion mode
r - replace current character with a new one.

Join
J - join selected lines onto a single line. Space added to separate lines
gJ - join selected lines without space

Insertion
I - insert at first line. Generate duplicates in visual block mode

Shift [visual block mode]
< - shift left
> - shift right



========================================
Commands for programmers
========================================
[visual] - in visual mode
Indent - :set shiftwidth
< - indent left
> - indent right
[normal]
<< - indent left
>> - indent right
>%/<% - indent texts within pairs
>i{/<i{ - indent texts within braces, but excluding braces

Go to ...
[visual][normal]
Ctrl+] - go to definition of selected function. Need ctags to preprocess all *.cor *.cpp files
ctrl+w] - split the window and go to the definition of function under cursor
% - go to matching pairs (braces, brackets, comments, #if,#endif) combined with other commands to select text within a pair,e.g. y%,d%,<%,=%
gd - go to local definition of a variable
gD - go to global definition of a variable
[+I/]+I - display word under cursor in current file or #include
[ctrl+I / ]ctrl+I - search word under the cursor in current file and all #include files
[ctrl-D/]ctrl-D - go to macro definition
[+D/]+D - display macro definition

Tag
:tag function - go to definition of a function. tag is the definition of a function or hyperlink in help system.
:tag /tag - go to the tag that matches regular expression 'tag'
:tags - list all the tags have been traversed through
ctrl+T - go back to previous tag
:stag tag - split a new window and go to the tag
:tselect - select tag in tag stack

Automatic indentation
C Indentation - :set cindent
Autoindent - :set autoindent
= + [range] - indent selected range, e.g. start from the first bracket, =% indents all within the matching brackets. This indention uses internal indent program, which is highly recommended.

Make
:set list - change to list mode. Tabs show up as ^I
!'a sort - use external command and specify the range. All ranges can be specified in visual mode.
:make - run make.If any error occurs,jump to the first error.

Compile errors
:cnext - go to the next error
:cNext\cprevious - go to the previous error
:cc - display current error
:clist - list all errors
:cnfile - go to the first error in the next file

Search with regular expression
:grep pattern file_name - run external grep program for file_name
:cnext - go to the next match
:cprevious - go to the previous match

========================================
: Command line mode - all commands in ex mode can preceed with a range specified by line number L1,L2
========================================
- Use auto completion with Tab
- type ? to query the value of attribute

Save your own setting
:mkvimrc file - save current settings
:source file - load settings from a file
:version - show version info

Enter ex command line mode
Q/:ex - enter ex command line mode
All ex commands begin with range for line numbers to indicate range the command operates on

[range] specified with line number. % is short for the whole text
Replace
:range s(substitute) /from /to /flags - in ranges, change /from string to /to string. Useful flags : /p print changes, /c ask for comfirmation, /g change all occurence on one line

Move
:range move - move text within range to the current cursor location

Write & Read
:range w(write) file - write ranges of file to a new file
:r(read) file - read from a file and append to the current cursor

Shell
:shell - enter shell command prompt
ctrl+z - suspend the vim editor and enter command prompt



========================================
Abbreviations & Key Mapping
========================================
:abbreviate/ab FS Shuang Feng (abbreviate expansion) - record abbreviation and invoke with <space>
:abbreviate/ab - list all abbreviations
:map - map a seris of command onto a single key

========================================
Autocommand - command executed automatically in respose to some event
========================================
:autocmd group events file_pattern nested command - register command to a trigger event
:augroup - give definition of auto command group
:doautocmd group event file_name - pretend an event happens on 'file_name'. Invoke some autocommand manually.
:autocmd - list all auto commands




========================================
Automatic completion
========================================
[insertion]
ctrl+p/ctrl+n - automatic completion
:set dictionary=file,file... - specify a dictionary when used for word completion
ctrl+x - enter submode to search for specific types
ctrl+x ctrl+d - search for macro definition
ctrl+x ctrl+] - search for tags (definitions of C functions),show entire tag, :set showfulltag
ctrl+x ctrl+f - search for file names
ctrl+x ctrl+e - scroll one line up
ctrl+x ctrl+y - scroll one line down

========================================
Command line arguments
========================================
vim -R file_name : open a file in a read-only mode
vim -x file_name : open a file with the encryption
:x - set encyption for current file
vim -e file_name < batch_file : run batch file for file_name under ex command line mode
:set backup : manually backup file
vim -r file_name : recover & open a file
:recover file_name - recover a file
vim -r : list all swap files
:set updatetime= - set internals between auto write
:set updatecount= - set count of words befor auto writing
:set swapfile - use swap file for recovery
:preserve - write all the changes to swap file until you use :write to save changes to actual file. With :preserve file can be recovered with only swap file, otherwise both original and swap files are needed for recovery

File encoding - character set support
:set fileencoding=prc - for Chinese Simplified
:set fileencoding=taiwan - for Chinese Traditional