Skip to content

Vim Tips & Notes

vim.png

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
/ 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

  1. gg Move to the first line of the file
  2. G Move to the last line
  3. gg=G Reindent the whole file
  4. gv Reselect the last visual selection
  5. `< Jump to beginning of last visual selection
  6. `> Jump to end of last visual selection
  7. ^ Move to first non-blank character of the line
  8. g_ Move the last non-blank character of the line (but you remove trailing whitespace, right)
  9. g_lD Delete all the trailing whitespace on the line
  10. ea Append to the end of the current word
  11. gf Jump to the file name under the cursor
  12. xp Swap character forward
  13. Xp Swap character backward
  14. yyp Duplicate the current line
  15. yapP Duplicate the current paragraph
  16. dat Delete around an HTML tag, including the tag
  17. dit Delete inside an HTML tag, excluding the tag
  18. w Move one word to the right
  19. b Move one word to the left
  20. dd Delete the current line
  21. zc Close current fold
  22. zo Open current fold
  23. za Toggle current fold
  24. zi Toggle folding entirely
  25. << Outdent current line
  26. >> Indent current line
  27. z= Show spelling corrections
  28. zg Add to spelling dictionary
  29. zw Remove from spelling dictionary
  30. ~ Toggle case of current character
  31. gUw Uppercase until end of word (u for lower, ~ to toggle)
  32. gUiw Uppercase entire word (u for lower, ~ to toggle)
  33. gUU Uppercase entire line
  34. gu$ Lowercase until the end of the line
  35. da" Delete the next double-quoted string
  36. + Move to the first non-whitespace character of the next line
  37. S Delete current line and go into insert mode
  38. I insert at the beginning of the line
  39. ci" Change what’s inside the next double-quoted string
  40. ca{ Change inside the curly braces
  41. vaw Visually select word
  42. dap Delete the whole paragraph
  43. r Replace a character
  44. `[ Jump to beginning of last yanked text
  45. `] Jump to end of last yanked text
  46. g; Jump to the last change you made
  47. g, Jump back forward through the change list
  48. & Repeat last substitution on current line
  49. g& Repeat last substitution on all lines
  50. ZZ Save the current file and close it
" 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
Feedback