>

Editing multiple files

1) Open multiple files at the same time

  • vi a.txt b.txt c.txt
  • :n, :next edit next file
  • :2n edit next two files
  • :N, :previous edit previous file
  • :wnext save current file then edit next file
  • :wprevious save current file, then edit previous file
  • :args list args (args are a sequence of filenames)
  • :n filenames, :args filenames specifies file list
  • vi -o filenames Edit multiple files in horizontally splitted windows
  • vi -O filenames Edit multiple files in vertically splitted windows

2) Editing multiple files in Tabs

  • vim -p files Open multiple files in tabs
  • :tabe, tabnew Open new file in tab
  • ^w gf Open file with filename under the current cursor in new tab
  • :tabn Switch to the next tab (can also achieved by Ctrl + PageDown)
  • :tabp Switch to the previous tab (can also be achieved by Ctrl + PageUp)
  • [n] gt Switch to the next nth tab, tab number is 1-based
  • :tab split Open current buffer content in new tab
  • :tabc[lose] Close current tab
  • :tabo[nly] Close all other tabs
  • :tabs List all open tabs
  • :tabm[ove] [N] Move tab, if tabm 0, current tab will become the first tab

Vim Buffer

  • :buffers, :ls, :files list buffers
  • ctrl+^ Switch between the most recent buffers
  • :bn Switch to the next buffer
  • :bp Switch to the previous buffer
  • :bl Switch to the last buffer
  • :b[n]/[n]b Switch to the next nth buffer
  • :nbw(ipeout) Delete the nth buffer completely
  • :nbd(elete) Delete the nth buffer, but still it in unlisted
  • :ba[ll] Open all buffers in current tab, split window for each buffer

Spliting Windows

1) Editing multiple files in splitted windows

  • vim -o file1 file2: open file in horizontally splitted windows
  • vim -O file1 file2: open file in vertically splitted windows

2) Split current window horizontally

  • :sp, :split Split current into two windows, up and down (same as CTRL-w s, or CTRL-w CTRL-s) Note CTRL-S might freeze the terminal, press CTRL-Q to continue
  • :split filename Open new file in a newly splitted window, locate right of the current window.
  • :nsplit(:nsp) Split a window in above with 3-line height
  • :[N]new Split a new window in above, and edit new file in that window. ( same as CTRL-w n or CTRL-w CTRL-n)
  • ctrl+w f Split a new window in above, and open file under the cursor in that window.
  • C-w C-^ Edit the file in a newly horizontally splitted window

2) Split current window horizontally

  • :vsp, :vsplit Vertically split current window into two. ( same as CTRL-w v, or CTRL CTRL-V)
  • :[N]vne[w] Vertically split a new window
  • :vertical

Close windows

  • :qall close all windows
  • :wall close all modified windows
  • :only close all other windows
  • :close close current window (same as CTRL-w c)

Resize window

  • ctrl+w + Increase current file by one line, use n for n lines
  • ctrl+w - Decrease current file by one line, use n for n lines
  • ctrl+w _ Maximize current window height
  • :resize n Set height of current file
  • ctrl+w = Resize all windows to the same height
  • n ctrl+w _ Set height of current file to n lines
  • ctrl+w < Decrease current window by one column, use n for n lines
  • ctrl+w > Increase current window by one column, use n for n lines
  • ctrl+w | Maximize current window width

Switch cursor between windows

  • ctrl+w ctrl+w: Switch to the next window ( same as Ctrl+w w)
  • ctrl+w p: Switch to the previous window
  • ctrl+w h(l,j,k): Switch to the left, right, below, or above window
  • ctrl+w t(b): Switch to the top/bottom window
  • ctrl+w H(L,K,J): Move current to left, right, below, or above
  • ctrl+w r:Rotate windows clockwise
  • ctrl+w T: Move current window onto a new tab