我已经看到了在Vim中使用选项卡(使用:table,:tabnew等)的能力,以取代我目前在隐藏缓冲区的同一个窗口中打开多个文件的做法。

我想每个不同的文件,我已经打开,总是在自己的选项卡。然而,有一些事情阻碍了这一点。我如何解决这些问题:

When commands like gf and ^] jump to a location in another file, the file opens in a new buffer in the current tab. Is there a way to have all of these sorts of commands open the file in a new tab, or switch to the existing tab with the file if it is already open? When switching buffers I can use :b <part of filename><tab> and it will complete the names of files in existing buffers. <part of filename> can even be the middle of a filename instead of the beginning. Is there an equivalent for switching tabs?


当前回答

You can map commands that normally manipulate buffers to manipulate tabs, as I've done with gf in my .vimrc: map gf :tabe <cfile><CR> I'm sure you can do the same with [^ I don't think vim supports this for tabs (yet). I use gt and gT to move to the next and previous tabs, respectively. You can also use Ngt, where N is the tab number. One peeve I have is that, by default, the tab number is not displayed in the tab line. To fix this, I put a couple functions at the end of my .vimrc file (I didn't paste here because it's long and didn't format correctly).

其他回答

停,停,停。

这不是Vim标签的设计用途。事实上,它们的名字是错的。更好的名字应该是“viewport”或“layout”,因为这就是标签的含义——它是所有现有缓冲区窗口的不同布局。

试图将Vim转换为1个tab == 1缓冲区是徒劳的。Vim不知道也不关心,它不会在所有命令上都尊重它——特别是任何使用快速修复缓冲区的命令(:make、:grep和:helpgrep是首先想到的缓冲区)都会很高兴地忽略制表符,而且您无法阻止这种情况。

而不是:

:设置隐藏 如果您还没有这个集合,那么就这样做。它使vim像地球上所有其他多文件编辑器一样工作。你可以编辑在某个窗口中不可见的缓冲区。 使用:bn,:bp,: b#,:b name和ctrl-6来切换缓冲区。我自己喜欢ctrl-6(单独切换到以前使用的缓冲区,或者#ctrl-6切换到缓冲区编号#)。 使用:ls列出缓冲区,或者像MiniBufExpl或BufExplorer这样的插件。

看着 :帮助选项卡 看起来vim并不想以您的方式工作……

缓冲区是跨选项卡共享的,因此似乎不可能将给定的缓冲区锁定为只出现在某个选项卡上。

不过,这是个好主意。

通过使用支持选项卡的终端(如multi-gnome-terminal),然后在每个终端选项卡中运行vim实例,您可能会得到想要的效果。不过,这并不完美……

与这里的其他一些答案相反,我说您可以随心所欲地使用选项卡。Vim被设计为多功能和可定制的,而不是强迫您根据预定义的参数工作。我们都知道我们程序员喜欢把自己的“道德规范”强加给其他人,所以这个成就当然是一个主要特征。

<C-w>gf是相当于缓冲区gf命令的制表符。<C-PageUp>和<C-PageDown>将在制表符之间切换。(在Byobu中,这两个命令从来不适合我,但它们在Byobu/tmux之外工作。<C-w>T将当前窗口移动到一个新的标签页。

如果您希望vim尽可能使用现有的选项卡,而不是创建重复的选项卡,请在.vimrc文件中添加:set switchbuf=usetab。您可以将newtab添加到列表(:set switchbuf=usetab,newtab),以强制在单独的选项卡中打开显示编译错误的QuickFix命令。我更喜欢拆分,它会在拆分窗口中打开编译错误。

如果您启用了鼠标支持:set mouse=a,您可以通过单击选项卡与它们进行交互。默认情况下还有一个+按钮,它将创建一个新选项卡。

对于有关选项卡的文档,在正常模式下键入:help选项卡页。(在你这样做之后,你可以练习移动一个窗口到一个选项卡使用<C-w> t)有一个很长的命令列表。一些窗口命令与选项卡有关,因此您可能也想通过:help窗口查看相关文档。

补充:2013-12-19

要在vim中打开多个文件,每个文件都在单独的选项卡中,请使用vim -p file1 file2 ....如果您像我一样总是忘记添加-p,您可以在最后添加它,因为vim遵循正常的命令行选项解析规则。或者,您也可以添加一个bash别名,将vim映射到vim -p。

有点晚了,但很惊讶我没有在这个列表中看到以下内容:

:tab sball -这将为每个打开的缓冲区打开一个新标签。

:help switchbuf -控制缓冲区切换行为,尝试:set switchbuf=usetab,newtab。这应该意味着如果缓冲区打开,则切换到现有选项卡,如果没有打开则创建一个新选项卡。

You can map commands that normally manipulate buffers to manipulate tabs, as I've done with gf in my .vimrc: map gf :tabe <cfile><CR> I'm sure you can do the same with [^ I don't think vim supports this for tabs (yet). I use gt and gT to move to the next and previous tabs, respectively. You can also use Ngt, where N is the tab number. One peeve I have is that, by default, the tab number is not displayed in the tab line. To fix this, I put a couple functions at the end of my .vimrc file (I didn't paste here because it's long and didn't format correctly).