Vim Tips & Notes

Vim Hotkey
Edit Commands
i insert before the word
a insert after the word
I insert at the beginning of the line
A insert at the end of the line
o open a new line to edit
O open a line above
Insert
10 i * 【ESC】 insert 10 *
25 a xx 【ESC】 insert 25 xx
J Join two lines
Move
4l move 4 characters to the right
0 move to the beginning of the line
^ move to the beginning of the line
$ move to the end of the line
2w move forward two words
3W move forward three words (count only spaces)
2b move backward two words
5B move backward five words (count only spaces)
e move to the end of the word
Move within the screen
H move to the top of the screen
M move to the middle of the screen
L move to the bottom of the screen
nH move to the nth line of the current screen (upward)
nL move to the nth line of the current screen (downward)
Move between sentences, paragraphs and sections
( move to the beginning of the sentence
) move to the end of the sentence (beginning of the next sentence)
{ move to the beginning of the paragraph
} move to the end of the paragraph
[[ move to the beginning of the section
]] move to the end of the section
【Ctrl】+F scroll down one screen
【Ctrl】+B scroll up one screen
【Ctrl】+D scroll down half screen
【Ctrl】+U scroll up half screen
Search
/ XX search XX
/【Enter】 search downward
?【Enter】 search upward
f XX search within the line (forward)
F XX search within the line (backward)
R replace mode
s delete and insert the current word
S delete the whole line and insert at the beginning
~ change case
p put
2p put twice (paste)
xp transpose two characters
d delete
2dd delete two lines downward
d3w delete three words forward
d3W
d2b
d2B
y yank
yy copy the whole line
y2w copy two words forward
y2b
c change
cw change the whole word
c2w change two words forward
c3B
r replace
5rT replace next 5 characters with T
【Enter】 move to the first character of the next line
+
- move to the first character of the previous line
^ move to the first non-space character of the line
n| move to the nth column of the line
gg move to the top of the document
G move to the bottom of the document
10G move to line 10
【Ctrl】+G show line status
zz save and exit
:e! clear all editing results
:q! exit without saving
. repeat the last command
u undo
【Ctrl】+R redo
z 【Enter】 put the line at the cursor to the top of the screen
z . put the line at the cursor to the middle of the screen
z - put the line at the cursor to the bottom of the screen
Top 50 useful Vim Commands
gg
Move to the first line of the file
G
Move to the last line
gg=G
Reindent the whole file
gv
Reselect the last visual selection
`<
Jump to beginning of last visual selection
`>
Jump to end of last visual selection
^
Move to first non-blank character of the line
g_
Move the last non-blank character of the line (but you remove trailing whitespace, right)
g_lD
Delete all the trailing whitespace on the line
ea
Append to the end of the current word
gf
Jump to the file name under the cursor
xp
Swap character forward
Xp
Swap character backward
yyp
Duplicate the current line
yapP
Duplicate the current paragraph
dat
Delete around an HTML tag, including the tag
dit
Delete inside an HTML tag, excluding the tag
w
Move one word to the right
b
Move one word to the left
dd
Delete the current line
zc
Close current fold
zo
Open current fold
za
Toggle current fold
zi
Toggle folding entirely
<<
Outdent current line
>>
Indent current line
z=
Show spelling corrections
zg
Add to spelling dictionary
zw
Remove from spelling dictionary
~
Toggle case of current character
gUw
Uppercase until end of word (u for lower, ~ to toggle)
gUiw
Uppercase entire word (u for lower, ~ to toggle)
gUU
Uppercase entire line
gu$
Lowercase until the end of the line
da"
Delete the next double-quoted string
+
Move to the first non-whitespace character of the next line
S
Delete current line and go into insert mode
I
insert at the beginning of the line
ci"
Change what’s inside the next double-quoted string
ca{
Change inside the curly braces
vaw
Visually select word
dap
Delete the whole paragraph
r
Replace a character
`[
Jump to beginning of last yanked text
`]
Jump to end of last yanked text
g;
Jump to the last change you made
g,
Jump back forward through the change list
&
Repeat last substitution on current line
g&
Repeat last substitution on all lines
ZZ
Save the current file and close it
vimrc recommended setting & Config
" syntax
syntax on
colorscheme desert
" history : how many lines of history VIM has to remember
set history=2000
" filetype
filetype on
" Enable filetype plugins
filetype plugin on
" filetype indent on
" base
set nocompatible " don't bother with vi compatibility
set autoread " reload files when changed on disk, i.e. via `git checkout`
set shortmess=atI
set magic " For regular expressions turn magic on
set title " change the terminal's title
set nobackup " do not keep a backup file
set novisualbell " turn off visual bell
set noerrorbells " don't beep
set visualbell t_vb= " turn off error beep/flash
set t_vb=
set tm=500
" show location
set cursorcolumn
set cursorline
" movement
set scrolloff=7 " keep 3 lines when scrolling
" show
set ruler " show the current row and column
"set number " show line numbers
set nowrap
set showcmd " display incomplete commands
set showmode " display current modes
set showmatch " jump to matches when entering parentheses
set matchtime=2 " tenths of a second to show the matching parenthesis
" search
set hlsearch " highlight searches
set incsearch " do incremental searching, search as you type
set ignorecase " ignore case when searching
set smartcase " no ignorecase if Uppercase char present
" tab
" set expandtab " expand tabs to spaces
set smarttab
set shiftround
" indent
set shiftround
set shiftwidth=4
set tabstop=4
set softtabstop=4 " insert mode tab and backspace use 4 spaces
" encoding
set encoding=utf-8
set fileencodings=ucs-bom,utf-8,cp936,gb18030,big5,euc-jp,euc-kr,latin1
set termencoding=utf-8
set ffs=unix,dos,mac
set formatoptions+=m
set formatoptions+=B
" select & complete
set selection=inclusive
set selectmode=mouse,key
set completeopt=longest,menu
set wildmenu " show a navigable menu for tab completion"
set wildmode=longest,list,full
set wildignore=*.o,*~,*.pyc,*.class
" others
set backspace=indent,eol,start " make that backspace key work the way it should
set whichwrap+=<,>,h,l
set background=dark
" set mark column color
hi! link SignColumn LineNr
hi! link ShowMarksHLl DiffAdd
hi! link ShowMarksHLu DiffChange
" status line
set statusline=%<%f\ %h%m%r%=%k[%{(&fenc==\"\")?&enc:&fenc}%{(&bomb?\",BOM\":\"\")}]\ %-14.(%l,%c%V%)\ %P
set laststatus=2 " Always show the status line - use 2 lines for the status bar
autocmd FileType python set tabstop=4 shiftwidth=4 expandtab
autocmd BufRead,BufNew *.md,*.mkd,*.markdown set filetype=markdown.mkd
" disable auto wrap and auto comments
set formatoptions-=cro
" https://github.com/ryanpcmcquen/fix-vim-pasting
let &t_SI .= "\<Esc>[?2004h"
let &t_EI .= "\<Esc>[?2004l"
inoremap <special> <expr> <Esc>[200~ XTermPasteBegin()
function! XTermPasteBegin()
set pastetoggle=<Esc>[201~
set paste
return ""
endfunction
https://github.com/groenewege/vimrc/blob/master/vim_cheat_sheet.txt
http://blog.g-design.net/post/4789778607/vim-cheat-sheet
http://www.keyxl.com/aaa8263/290/VIM-keyboard-shortcuts.htm
http://jmcpherson.org/editing.html
http://www.fprintf.net/vimCheatSheet.html
http://www.ouyaoxiazai.com/article/24/654.html
http://bbs.it-home.org/thread-80794-1-1.html
http://www.lpfrx.com/wp-content/uploads/2008/09/vi.jpg
http://michael.peopleofhonoronly.com/vim/
https://github.com/skywind3000/awesome-cheatsheets/blob/master/editors/vim.txt