Global

  • :h[elp] keyword - tra cứu một lệnh, ví dụ: :h dd
  • :sav[eas] file - lưu file với tên mới, ví dụ: :sav new_file_name
  • :clo[se] - close current pane
  • :ter[minal] - mở cửa sổ Terminal cùng với của sổ vim.
  • K - mở manual page cho từ khóa ở vị trí con trỏ.
Tip Chạy lệnh vimtutor trong terminal để học những lệnh Vim đầu tiên.

Cursor movement

  • h - di chuyển con trỏ qua trái một ký tự
  • j - di chuyển con trỏ xuống dưới một hàng
  • k - di chuyển con trỏ lên trên một hàng
  • l - di chuyển con trỏ qua phải một ký tự
  • gj - move cursor down (multi-line text)
  • gk - move cursor up (multi-line text)
  • H - di chuyển con trỏ lên đầu màn hình
  • M - di chuyển con trỏ về giữa màn hình
  • L - di chuyển con trỏ xuống cuối màn hình
  • w - di chuyển qua phải, đến ký tự đầu tiên của từ kế tiếp
  • W - di chuyển qua phải, đến ký tự đầu tiên của từ kế tiếp (trong từ có thể chứa các ký tự đặc biệt và dấu câu)
  • e - di chuyển qua phải, đến ký tự cuối cùng của từ
  • E - di chuyển qua phải, đến ký tự cuối cùng của từ (trong từ có thể chứa các ký tự đặc biệt và dấu câu)
  • b - di chuyển qua trái, đến ký tự đầu tiên của từ
  • B - di chuyển qua trái, đến ký tự đầu tiên của từ (trong từ có thể chứa các ký tự đặc biệt và dấu câu)
  • ge - di chuyển qua trái, đến ký tự cuối cùng của từ trước đó
  • gE - di chuyển qua trái, đến ký tự cuối cùng của từ trước đó (trong từ có thể chứa các ký tự đặc biệt và dấu câu)
  • % - di chuyển đến dấu đóng mở ngoặc tương ứng (hỗ trợ mặc định: '()', '{}', '[]' - gọi lệnh :h matchpairs để biết thêm chi tiết.)
  • 0 - di chuyển về đầu hàng
  • ^ - di chuyển đến ký tự non-blank đầu tiên của hàng
  • $ - di chuyển về cuối hàng
  • g_ - di chuyển đến ký tự non-blank cuối cùng của hàng
  • gg - di chuyển đến hàng đầu tiên của file
  • G - di chuyển đến hàng cuối cùng của file
  • 5gg or 5G - di chuyển đến hàng thứ 5 của file
  • gd - move to local declaration
  • gD - move to global declaration
  • fx - jump to next occurrence of character x
  • tx - jump to before next occurrence of character x
  • Fx - jump to previous occurence of character x
  • Tx - jump to after previous occurence of character x
  • ; - repeat previous f, t, F or T movement
  • , - repeat previous f, t, F or T movement, backwards
  • } - di chuyển đến khối văn bản tiếp theo.
  • { - di chuyển đến khối văn bản trước đó.
  • zz - di chuyển màn hình để con trỏ nằm ở trung tâm màn hình.
  • zt - position cursor on top of the screen
  • zb - position cursor on bottom of the screen
  • Ctrl + e - di chuyển màn hình xuống dưới một hàng (không di chuyển con trỏ).
  • Ctrl + y - di chuyển màn hình lên trên một hàng (không di chuyển con trỏ).
  • Ctrl + b - di chuyển lên trên một màn hình.
  • Ctrl + f - di chuyển xuống dưới một màn hình.
  • Ctrl + d - di chuyển xuống dưới 1/2 một màn hình.
  • Ctrl + u - di chuyển lên trên 1/2 màn hình.
Tip Sử dụng tiền tố là chữ số cùng với lệnh để lặp lại lệnh đó. Ví dụ, 4j là di chuyển xuống dưới 4 hàng.

Insert mode - inserting/appending text

  • i - chèn vào phía trước con trỏ
  • I - chèn vào đầu hàng
  • a - chèn vào phía sau con trỏ
  • A - chèn vào cuối hàng
  • o - chèn một hàng mới ở bên dưới hàng hiện tại
  • O - chèn một hàng mới ở bên trên hàng hiện tại
  • ea - chèn vào cuối từ
  • Ctrl + h - trong chế độ Insert, xóa 1 ký tự nằm bên trái con trỏ, tương đương với backspace.
  • Ctrl + w - trong chế độ Insert, xóa các ký tự nằm bên trái con trỏ, tính từ vị trí con trỏ đến ký tự đầu tiên của từ
  • Ctrl + j - trong chế độ Insert, chèn một hàng mới ở bên trên hàng hiện tại
  • Ctrl + t - trong chế độ Insert, di chuyển cả hàng qua phải 1 Tab.
  • Ctrl + d - trong chế độ Insert, di chuyển cả hàng qua trái 1 Tab.
  • Ctrl + n - insert (auto-complete) next match before the cursor during insert mode
  • Ctrl + p - insert (auto-complete) previous match before the cursor during insert mode
  • Ctrl + rx - insert the contents of register x
  • Ctrl + ox - Temporarily enter normal mode to issue one normal-mode command x.
  • Esc or Ctrl + c - thoát khỏi chế độ Insert

Editing

  • r - thay thế một ký tự với một ký tự khác, ví dụ: ra, rx,...
  • R - thay thế nhiều ký tự cho đến khi ESC được nhấn.
  • J - nối hàng hiện tại và hàng bên dưới, khoảng trắng giữa 2 hàng được chuyển thành 1 space.
  • gJ - nối hàng hiện tại và hàng bên dưới, giữ nguyên khoảng trắng giữa 2 hàng.
  • gwip - reflow paragraph
  • g~ - chuyển đổi giữa lowercase và uppercase cho các ký tự nằm trong vùng di chuyển của con trỏ.
  • gu - chuyển thành lowercase cho các ký tự nằm trong vùng di chuyển của con trỏ, ví dụ: gu Enter là chuyển cả hàng thành lowercase.
  • gU - chuyển thành uppercase cho các ký tự nằm trong vùng di chuyển của con trỏ, ví dụ: gUiw là chuyển cả từ thành uppercase.
  • cc - thay đổi nội dung của một hàng
  • c$ or C - thay đổi nội dung của hàng, tính từ vị trí con trỏ đến cuối hàng.
  • ciw - thay đổi nội dung của cả một từ.
  • cw or ce - thay đổi nội dung của một từ, tính từ vị trí con trỏ đến ký tự cuối cùng của từ.
  • s - xóa một ký tự và vào chế độ Insert.
  • S - xóa một hàng và vào chế độ Insert. (tương đương với lệnh cc)
  • xp - hoán đổi vị trí 2 ký tự (delete and paste)
  • u - undo
  • U - restore (undo) last changed line
  • Ctrl + r - redo
  • . - thực hiện lại lệnh gần nhất.

Marking text (visual mode)

  • v - chế độ đánh dấu, dùng để bôi đen ký tự, sau đó thực hiện các command như copy hoặc xóa.
  • V - chế độ đánh dấu, dùng để bôi đen theo hàng.
  • o - move to other end of marked area
  • Ctrl + v - start visual block mode
  • O - move to other corner of block
  • aw - mark a word
  • ab - a block with ()
  • aB - a block with {}
  • at - a block with <> tags
  • ib - inner block with ()
  • iB - inner block with {}
  • it - inner block with <> tags
  • Esc or Ctrl + c - exit visual mode
Tip Instead of b or B one can also use ( or { respectively.

Visual commands

  • > - shift text right
  • < - shift text left
  • y - copy những ký tự đã được bôi đen.
  • d - xóa những ký tự đã được bôi đen.
  • ~ - chuyển đổi giữa uppercase và lowercase cho những ký tự đã bôi đen.
  • u - chuyển thành lowercase cho những ký tự đã bôi đen.
  • U - chuyển thành uppercase cho những ký tự đã bôi đen.

Registers

  • :reg[isters] - show registers content
  • "xy - yank into register x
  • "xp - paste contents of register x
  • "+y - yank into the system clipboard register
  • "+p - paste from the system clipboard register
Tip Registers are being stored in ~/.viminfo, and will be loaded again on next restart of vim.
Tip Special registers:

0 - last yank
" - unnamed register, last delete or yank
% - current file name
# - alternate file name
* - clipboard contents (X11 primary)
+ - clipboard contents (X11 clipboard)
/ - last search pattern
: - last command-line
. - last inserted text
- - last small (less than a line) delete
= - expression register
_ - black hole register

Marks and positions

  • :marks - list of marks
  • ma - set current position for mark A
  • `a - jump to position of mark A
  • y`a - yank text to position of mark A
  • `0 - go to the position where Vim was previously exited
  • `" - go to the position when last editing this file
  • `. - go to the position of the last change in this file
  • `` - go to the position before the last jump
  • :ju[mps] - list of jumps
  • Ctrl + i - go to newer position in jump list
  • Ctrl + o - go to older position in jump list
  • :changes - list of changes
  • g, - go to newer position in change list
  • g; - go to older position in change list
  • Ctrl + ] - jump to the tag under cursor
Tip To jump to a mark you can either use a backtick (`) or an apostrophe ('). Using an apostrophe jumps to the beginning (first non-black) of the line holding the mark.

Macros

  • qa - record macro a
  • q - stop recording macro
  • @a - run macro a
  • @@ - rerun last run macro

Cut and paste

  • yy - copy 1 hàng tại vị trí con trỏ.
  • 2yy - copy 2 hàng, tính từ hàng chứa con trỏ.
  • yw - copy từ vị trí con trỏ đến trước ký tự đầu tiên của từ tiếp theo.
  • yiw - copy cả từ tại vị trí con trỏ
  • yaw - copy cả từ tại vị trí con trỏ, kể cả khoảng trắng phía trước hoặc sau nó.
  • y$ or Y - copy từ vị trí con trỏ đến cuối hàng.
  • p - paste vào vị trí phía sau con trỏ.
  • P - paste vào vị trí phía trước con trỏ.
  • gp - put (paste) the clipboard after cursor and leave cursor after the new text
  • gP - put (paste) before cursor and leave cursor after the new text
  • dd - xóa 1 hàng.
  • 2dd - xóa 2 hàng.
  • dw - xóa từ vị trí con trỏ đến trước ký tự đầu tiên của từ tiếp theo.
  • diw - xóa cả từ tại vị trí con trỏ.
  • daw - xóa cả từ tại vị trí con trỏ, kể cả khoảng trắng phía trước hoặc sau nó.
  • :3,5d - delete lines starting from 3 to 5
Tip You can also use the following characters to specify the range:
e.g.

:.,$d - From the current line to the end of the file
:.,1d - From the current line to the beginning of the file
:10,$d - From the 10th line to the beginning of the file

  • :g/{pattern}/d - delete all lines containing pattern
  • :g!/{pattern}/d - delete all lines not containing pattern
  • d$ or D - xóa đến cuối hàng.
  • x - xóa ký tự tại vị trí con trỏ.

Indent text

  • >> - di chuyển cả hàng sang phải 1 tab.
  • << - di chuyển cả hàng sang trái 1 tab.
  • >% - di chuyển cả block (block được bao bọc bởi (), {}), tính từ hàng chứa con trỏ sang phải 1 tab
  • <% - de-indent a block with () or {} (cursor on brace)
  • >ib - indent inner block with ()
  • >at - indent a block with <> tags
  • 3== - re-indent 3 hàng, tính từ hàng chứa con trỏ.
  • =% - re-indent cả block (block được bao bọc bởi (), {}), tính từ hàng chứa con trỏ.
  • =iB - re-indent inner block with {}
  • gg=G - re-indent entire buffer
  • ]p - paste and adjust indent to current line

Exiting

  • :w - lưu file
  • :w !sudo tee % - lưu file với quyền root
  • :wq or :x or ZZ - lưu và đóng file.
  • :q - đóng file nếu file đó chưa bị thay đổi.
  • :q! or ZQ - đóng file, bỏ qua những thay đổi có trong file.
  • :wqa - lưu và đóng tất cả file đang mở.

Search and replace

  • /pattern - search for pattern
  • ?pattern - search backward for pattern
  • \vpattern - 'very magic' pattern: non-alphanumeric characters are interpreted as special regex symbols (no escaping needed)
  • n - repeat search in same direction
  • N - repeat search in opposite direction
  • :%s/old/new/g - replace all old with new throughout file
  • :%s/old/new/gc - replace all old with new throughout file with confirmations
  • :noh[lsearch] - remove highlighting of search matches

Search in multiple files

  • :vim[grep] /pattern/ {`{file}`} - search for pattern in multiple files
e.g. :vim[grep] /foo/ **/*
  • :cn[ext] - jump to the next match
  • :cp[revious] - jump to the previous match
  • :cope[n] - open a window containing the list of matches
  • :ccl[ose] - close the quickfix window

Tabs

  • :tabnew or :tabnew {page.words.file} - open a file in a new tab
  • Ctrl + wT - move the current split window into its own tab
  • gt or :tabn[ext] - move to the next tab
  • gT or :tabp[revious] - move to the previous tab
  • #gt - move to tab number #
  • :tabm[ove] # - move current tab to the #th position (indexed from 0)
  • :tabc[lose] - close the current tab and all its windows
  • :tabo[nly] - close all tabs except for the current one
  • :tabdo command - run the command on all tabs (e.g. :tabdo q - closes all opened tabs)

Working with multiple files

  • :e[dit] file - edit a file in a new buffer
  • :bn[ext] - go to the next buffer
  • :bp[revious] - go to the previous buffer
  • :bd[elete] - delete a buffer (close a file)
  • :b[uffer]# - go to a buffer by index #
  • :b[uffer] file - go to a buffer by file
  • :ls or :buffers - list all open buffers
  • :sp[lit] file - open a file in a new buffer and split window
  • :vs[plit] file - open a file in a new buffer and vertically split window
  • :vert[ical] ba[ll] - edit all buffers as vertical windows
  • :tab ba[ll] - edit all buffers as tabs
  • Ctrl + ws - split window
  • Ctrl + wv - split window vertically
  • Ctrl + ww - switch windows
  • Ctrl + wq - quit a window
  • Ctrl + wx - exchange current window with next one
  • Ctrl + w= - make all windows equal height & width
  • Ctrl + wh - move cursor to the left window (vertical split)
  • Ctrl + wl - move cursor to the right window (vertical split)
  • Ctrl + wj - move cursor to the window below (horizontal split)
  • Ctrl + wk - move cursor to the window above (horizontal split)
  • Ctrl + wH - make current window full height at far left (leftmost vertical window)
  • Ctrl + wL - make current window full height at far right (rightmost vertical window)
  • Ctrl + wJ - make current window full width at the very bottom (bottommost horizontal window)
  • Ctrl + wK - make current window full width at the very top (topmost horizontal window)

Diff

  • zf - manually define a fold up to motion
  • zd - delete fold under the cursor
  • za - toggle fold under the cursor
  • zo - open fold under the cursor
  • zc - close fold under the cursor
  • zr - reduce (open) all folds by one level
  • zm - fold more (close) all folds by one level
  • zi - toggle folding functionality
  • ]c - jump to start of next change
  • [c - jump to start of previous change
  • do or :diffg[et] - obtain (get) difference (from other buffer)
  • dp or :diffpu[t] - put difference (to other buffer)
  • :diffthis - make current window part of diff
  • :dif[fupdate] - update differences
  • :diffo[ff] - switch off diff mode for current window
Tip The commands for folding (e.g. za) operate on one level. To operate on all levels, use uppercase letters (e.g. zA).
Tip To view the differences of files, one can directly start Vim in diff mode by running vimdiff in a terminal. One can even set this as git difftool.