Table of contents


Setup

Mode

  • i : insert mode
  • esc : back to normal mode
  • ctrl + [ : back to normal mode(fast)
  • : : command mode → open file,save file, …
  • gQ : Ex mode
  • v : visual mode → select text
  • a : append mode
  • r/R : replace/repace mode
  • o : open a new line
  • J : joint next line

Commands

Motions

  • h/j/k/l : left/down/up/right
  • 0 : line start
  • \^/$ : string start/end
  • ctrl + d/ ctrl + u : page up / down
  • G : end of file
  • gg : top of file
  • w/b : word / back word
  • n : move to n column
  • :n : move to n line
  • % : move to matched parentheses

Editing

  • :e : reload current file or load a file by path
  • :e \^D : list directory
  • c : change , c<Motions>
  • y : yank , y<Motions>
  • yy : yank current line
  • p : paste
  • c : change
  • d : delete = cut(put to clipboard)
  • ~ : change Capital
  • s : replace
  • ctrl + r : redo
  • ct<symbol> : change till <symbol>
  • « / » : left/right shift a tab

Search and Replace

  • //? : search/backwards search
  • n/N : next/previous occurance of search term
  • :set incsearch/noincsearch → turn on/off incremental search
  • */# : go to next/previous occurence of word uder cursor
  • f<symbol> : move cursor to symbol of current word
  • ;/, : next/previous occurence

Macros

  • q <macro name> : start/stop recording
  • <times>@<macroname> : play macro

Multi Files Editing

  • n edit next file
  • 2n edit next 2 file
  • e# / ctrl + \^ : edit next/previous file
  • b<filename or number> : edit that file
  • :ls : list file

Visual

  • v : v-mode
  • V : visual line mode
  • ctrl + r : block mode (a.k.a column mode)
  • shift + < / shift + > : shift a tab

Window

  • ctrl + w n : new window
  • ctrl + s : (:sp) split file into 2 window
  • ctrl + w o : this window only
  • ctrl + w j/k : go to previous/next window

Open files in tabs

  • :tabs - View a list of tabs that are open with the file names. Use the command ‘:tabs’ and Vim will display a list of all the files in the tabs. The current window is shown by a ”>” and a ”+” is shown for any modifiable buffers.
  • :tabc - Close the current tab.
  • :tabnew - Open a new file for editing in a separate tab.
  • :tab split - Open the file in the current buffer in a new tab page.
  • :tabn - Switching to the next tab page.
  • :tabp - Switch to the previous tab page.
  • :tabr[ewind] - Go to the first tab page. You get the same effect when you use the :tabf[irst] command.
  • :tabo - Close all other tab pages.

Shell

  • :! cmd : execute cmd
  • :!! : execute previous command
  • sh : execute shell

Key Mapping

sample

Abbreviate

  • abbr : using abbreviate in both editor and command line
  • cabbr : using abbreviate only in command line
  • iabbr : using abbreviate in insert mode only
  • una : remove abbreviate

Configuration

  • :set : display current settings(modified only)
  • :set all : display all settings
  • :scriptnames : display the locations of config file
  • :set spell/nospell : spelling checking
  • :colo[rscheme] {name}: load color scheme {name}.
    1. check here to preview color schema

settings sample

Tips

\^M mark in windows

Windows(DOS)產生的文件, 在unix環境下使用vim開啟, 每一行的行尾會出現\^M的Dos換行符號. 移除它的方法, 是利用字串取代:

:%s/\r//g

Reload vimrc file

Using source command to reload vimrc (or any vim script) file

source ~/vimrc

Session and Viminfo

Save

Using mksession <filename.vim> to save current session. Using wviminfo <filename.viminfo> to write current info.

Load

Using source filename to load saved session Using rviminfo to read saved viminfo.

Example

    :mksession vim101.vim
    :wviminfo vim101.viminfo

    :source vim101.vim
    :rviminfo vim101.viminfo

check here for more details.

Resources