VIM默认是支持同时打开多个文件同时编辑,如果需要做文件切换是必须用 :ls 的指令把VIM缓存中打开的文件都列出来了,然后再根据序号切换到想要的文件。想想这样也太落后老土了。还好VIM有个非常实用的插件叫”Buffer Explorer“,装上这个插件以后可以直接用在Buffer的列表中选择文件做快速的切换,这样比原来要敲2个指令才能换文件编辑的速度提高的可不止一倍。
1 从VIM.org 上下载 “Buffer Explorer”
2 解压缩以后复制到 .vim/docs 和plugin 目录
3 重启VIM后OK,现在可以开始试用了!
4 打开多个文件后使用快捷键 “\be",直接看到全部打开的文件列表。用上下键选择后回车就完成切换了
5 如果不喜欢这种全屏的方式,还有 ”\bv" "\bs" 可以试试!
6 如果打开的文件太多在一个平面显示不下,还可以在buffer中用“s” 快捷键排序
When I first started using Vim the thing that confused me the most was buffers. The editors that I used before simply showed windows or tabs for each open file -- if a file wasn't visible it was closed. Buffers are confusing to beginners because they seem to hang around even when the associated window has been closed.
To understand what buffers are it's important to realise that Vim separates the concept of an open file from how it's displayed. Buffers are a portion of memory that's been loaded with the contents of a file -- butnot the original file. Nothing happens to the original file until the buffer is written.
Since Vim can display buffers in windows and tabs, then buffers can actually be hidden from view. That means every file you've opened will remain accessible until you quit Vim, and that also implies opening a file for editing automatically creates a buffer.
Managing Buffers
I usually type the :ls
ex command to list buffers, but if you have trouble remembering this start with:buffers
:
To make any of these buffers the active buffer, type :buffer number
, wherenumber
is the number of the buffer in the list. The a
next to the third file here indicates that it's the active buffer.
To open all of the buffers in windows, type :ball
:
This actually places each buffer in a new window. They can even be opened in vertical windows using:vertical ball
.
A specific buffer can be opened in a new window by issuing the :sbuffer
command. A shortcut for this is to type a number then pressctrl w^
.
During the average working day I'll open dozens of files -- doesn't that mean Vim will keep opening buffers until there's an unmanageable amount? Yes, but fortunately buffers can be deleted by using:bdelete
. A file name or buffer number can be supplied as the argument. A range of buffers can even be deleted::10,16bdelete
would delete the buffers from 10 up to and including 16.
Buffer Navigation
Other than the basic :ls
and :buffer
commands, there's:bnext
and :bprevious
, which are typically issued with the shortened forms of:bn
and :bp
.
Further Reading
There's a lot more to learn about buffers, but I feel like this should be enough to use Vim effectively without being overwhelmed. The help text for buffers and windows, available in:help buffers
, explains everything you could ever want to know about buffers. TheVim Buffer FAQ is a relatively concise overview of buffers that's a little bit easier to get to grips with.