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
Scroll
【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
Scroll without moving the cursor
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
ggMove to the first line of the fileGMove to the last linegg=GReindent the whole filegvReselect 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 lineg_Move the last non-blank character of the line (but you remove trailing whitespace, right)g_lDDelete all the trailing whitespace on the lineeaAppend to the end of the current wordgfJump to the file name under the cursorxpSwap character forwardXpSwap character backwardyypDuplicate the current lineyapPDuplicate the current paragraphdatDelete around an HTML tag, including the tagditDelete inside an HTML tag, excluding the tagwMove one word to the rightbMove one word to the leftddDelete the current linezcClose current foldzoOpen current foldzaToggle current foldziToggle folding entirely<<Outdent current line>>Indent current linez=Show spelling correctionszgAdd to spelling dictionaryzwRemove from spelling dictionary~Toggle case of current charactergUwUppercase until end of word (u for lower, ~ to toggle)gUiwUppercase entire word (u for lower, ~ to toggle)gUUUppercase entire linegu$Lowercase until the end of the lineda"Delete the next double-quoted string+Move to the first non-whitespace character of the next lineSDelete current line and go into insert modeIinsert at the beginning of the lineci"Change what’s inside the next double-quoted stringca{Change inside the curly bracesvawVisually select worddapDelete the whole paragraphrReplace a character`[Jump to beginning of last yanked text`]Jump to end of last yanked textg;Jump to the last change you madeg,Jump back forward through the change list&Repeat last substitution on current lineg&Repeat last substitution on all linesZZSave 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
Some of the content is generated by AI, please be cautious in identifying it.