Vim Note

Quick guide for Vim ๐Ÿฆพ

Quit ๐Ÿƒ

  • :w save file,but don't quit
  • :wq write quit
  • :x same as :wq
  • q! quit without save
  • ZZ save file and quit

Cursor movement ๐Ÿ›ซ

  • H move to top of screen
  • M move to middle of screen
  • L move to bottom of screen
  • w jump forwards to the start of a word
  • e jump forwards to the end of a word
  • 0 go to start of line
  • $ go to end of line
  • b go to previous word
  • gg first line of file
  • GG last line of file
  • { jump to next paragraph
  • } jump tp previous paragraph
  • zz center cursor on screen
  • ctrl-d move foward 1/2 screen
  • ctrl-u move back 1/2 screen

Insert mode ๐Ÿ”Œ

  • i insert before the cursor
  • I insert at the beginning of the line
  • a insert (append) after the cursor
  • A insert (append) at the end of the line
  • o append (open) a new line below the current line
  • O append (open) a new line above the current line
  • Ctrl-o
    In insert mode, Ctrl-o escapes user to do one normal-mode command, and then return to the insert mode. The same effect can be achieved by <ESC>ing to normal mode, doing the single command and then entering back to insert mode.
    Eg : Ctrl-o a go to outside of the bracket

Short cut ๐Ÿ”ช

  • ctrl-p auto compelete previous words (ctrl-p for previous , ctrl-n for next)

Search & Replace ๐Ÿ‘€

  • /Word search for 'Word'
  • n move to next 'Word'
  • N move to previous 'Word'
  • noh[lsearch] remove highlighting of search matches
  • replace all abcd to 1234 in current line
:s/abcd/1234/g
  • relace all abce to 1234 in this file
:%s/abcd/1234/g
  • replace all abcd to 1234 in [5,12] line
:5,12s/abcd/1234/g

Visual mode ๐Ÿ—ณ

  • v Visual
  • V Visual Line
  • aw mark a word
  • a( mark a block with ()
  • a{ mark a block with {}
  • a[ mark a block with []
  • a< mark a block with <>
  • i( mark a inner block with ()

same as {} , [] ,<>, etc..

Cut & Paste ๐Ÿ”ช

  • yy yank (copy) a line
  • p paste

Editing โœ๏ธ

  • u undo
  • ctrl-r redo
  • r replace a single character.
  • R replace more than one character, until ESC is pressed.
  • J join line below to the current one with one space in between
  • c stand for change (delete and move to insert mode )
  • C change to end

Window ๐Ÿ–ฅ

  • sp file_name open a file in a new buffer and split window
  • :term open terminal in split window
  • vert edit all buffers as vertical windows
    Eg: vert sp a.cpp
  • below opem windows below
    Eg: below term
  • ctrl-ww switch windows
  • ctrl-wh switch to left windows (vertical split)
  • ctrl-wl switch to right windows (vertical split)
  • ctrl-wj switch to windows below(horizontal split)
  • ctrl-wk switch to windows above(horizontal split)
  • ctrl-wx exchange current window with next one
  • resize 20 resize height of window ,change the height to 20 rows.
  • res 20 short cut of resize
  • res +5 ,res -5 increase/decrease height
  • vert res 30 resize the width of window
    :Eg :term ctrl-wx res 35,open terminal and switch window ,resize window of Vim in to height of 35 rows.
  • bd buffer delete(close file)
  • ctrl \ n go to normal mode in terminal ( i for go back to terminal )

Tab ๐Ÿซ™

  • tab sp file_name open file in new tab
  • :tabs list all tabs including their displayed windows
  • tabn , gt go to next tab
  • tabp , gT go to previous tab
  • ctrl-PgDn , Ctrl-PgUp switch tabs
tags: Vim Note