Skip to content

06.Manage Multiple Files

Creating New Files

CommandKey BindingEffect
:new<C-w>nCreate a new empty buffer within a horizontal split window
:enewCreate a new empty buffer within the current window
:vnewCreate a new empty buffer within a vertical split window
:tabnewCreate a new empty buffer within a new tab

Tip 37. Track Open Files with the Buffer List

We can load multiple files during an editing session. Vim lets us manage them using the buffer list.

Understand the Distinction Between Files and Buffers

Most Vim commands operate on buffers, but a few operate on files, including the :write, :update and :saveas commands.

  • See :h :read

Meet the Buffer List

  • :ls, :files, :buffers
  • See :h :b

The % symbol indicates which of the buffers is visible in the current window, while the # symbol represents the alternate file. We can quickly toggle between the current and alternate files by pressing <C-^>.

::: Tip Note

When you have multiple buffers open, both <C-6> and <C-^> toggle the current/previous files.

:::

Use the Buffer List

CommandKey BindingEffect
:bn[ext]Move to the next buffer in the list
:bp[revious]Move to the previous buffer in the list
:bf[irst]Takes us to the first buffer in the list
:bl[ast]Takes us to the last buffer in the list
:b{N}
:{N}b
:b[all]
:b#<C-6>Move to the alternative buffer
:bmTakes us to the next modified buffer

Deleting Buffers

  • :bdelete N1 N2 N3
  • :[N,M] bd[elete], :[N,M]bd[elete]
  • :[N,M] bw[ipeout], :[N,M]bw[ipeout]

Tip 38. Group Buffers into a Collection with the Argument List

The argument list is easily managed and can be useful for grouping together a collection of files for easy navigation. We can run an Ex command on each item in the argument list using the :argdo command.

Tip 39. Manage Hidden Files

When a buffer has been modified, Vim gives it special treatment to ensure that we don’t accidentally quit the editor without saving our changes. Find out how to hide a modified buffer and how to handle hidden buffers when quitting Vim.

Tip 40. Divide Your Workspace into Split Windows

Vim allows us to view multiple buffers side by side by dividing our workspace into split windows.

In Vim’s terminology, a window is a viewport onto a buffer (:h window).

Creating Split Windows

CommandEffect
<C-w>sSplit the current window horizontally, reusing the current buffer in the new window
<C-w>vSplit the current window vertically, reusing the current buffer in the new window
:sp[lit] {file}Split the current window horizontally, loading {file} into the new window
:vsp[lit] {file}Split the current window vertically, loading {file} into the new window

Changing the Focus Between Windows

Closing Windows

Ex CommandNormal CommandEffect
:clo[se]<C-w>cClose the active window
:on[ly]<C-w>oKeep only the active window, closing all others

Resizing and Rearranging Windows

Tip 41. Organize Your Window Layouts with Tab Pages

Vim’s tabbed interface is different from that of many other text editors. We can use tab pages to organize split windows into a collection of workspaces.

In Vim, a tab page is a container that can hold a collection of windows (:h tabpage).

  • Vim workspace
    • tab 1
      • window a
      • window b
    • tab 2
      • window x
      • window y

How to Use Tabs

Suppose that we’re at work on a project, with our workspace divided into a few split windows. Out of the blue, something urgent comes up and we have to switch contexts. Rather than opening new files in our current tab page, which would mess up our carefully assembled workspace, we can create a new tab page and do the work there. When we’re ready to resume our previous work, we just have to switch back to the original tab page where all of our windows will have been preserved as we left them.

The :lcd {path} command lets us set the working directory locally for the current window. If we create a new tab page and then use the :lcd command to switch to another directory, we can then comfortably scope each tab page to a different project. Note that :lcd applies locally to the current window, not to the current tab page. If we have a tab page containing two or more split windows, we could set the local working directory for all of them by running :windo lcd {path}.

  • :h :lcd
  • :h :windo

Opening and Closing Tabs

CommandEffect
:tabe[dit] {filename}Open {filename} in a new tab
<C-w>TMove the current window into its own tab
:closeIf current tab with a single window, close the window and the tab pag with it
:tabc[lose]Close the current tab page and all of its windows
:tabo[nly]keep the active tab page, closing all others
  • see :h CTRL-W_T
  • see :h :tabnew
  • see :h :close

Switching Between Tabs

Tabs are numbered starting from 1. We can switch between tabs with the {N}gt command, which can be remembered as goto tab {N}. When this command is prefixed with a number, Vim jumps to the specified tab, but if the number is omitted, Vim advances to the next tab. The gT command does the same but in reverse.

Ex CommandNormal CommandEffect
:tabn[ext]{N}{N}gtSwitch to tab page number {N}
:tabn[ext]gtSwitch to the next tab page
:tabp[revious]gTSwitch to the previous tab page

Rearranging Tabs

We can use the :tabmove [N] Ex command to rearrange tab pages. When [N] is 0, the current tab page is moved to the beginning, and if we omit [N], the current tab page is moved to the end. If your terminal supports the mouse or if you’re using GVim, reordering tab pages by drag and drop is also possible.

多标签编辑

命令功能
vim -p files打开多个文件,每个文件占用一个标签页
:tabe,:tabnew如果加文件名,就在新的标签中打开这个文件,否则打开一个空缓冲区
<C-w> gf在新的标签页里打开光标下路径指定的文件
:tabn切换到下一个标签。Ctrl + PgDn,也可以
:tabp切换到上一个标签。Ctrl + PgUp,也可以
[n]gt切换到下一个标签。如果前面加了 N,就切换到第 N 个标签。第一个标签的序号就是 1
:tab split将当前缓冲区的内容在新页签中打开
:tabc[lose]关闭当前的标签页
:tabo[nly]关闭其它的标签页
:tabs列出所有的标签页和它们包含的窗口
:tabm[ove][N]移动标签页,移动到第 N 个标签页之后。如 tabm 0 当前标签页就会变成第一个标签页