简体   繁体   English

控制Vim中的选项卡名称

[英]Controlling Tab Names in Vim

In MacVim, the names of tab appears to be set to the name of the buffer most recently opened in that tab. 在MacVim中,选项卡的名称似乎设置为该选项卡中最近打开的缓冲区的名称。 This makes for confusion when using splits. 这在使用拆分时会造成混淆。 How can I fix the tabnames to the top-left window in a tab? 如何在选项卡中将标签名固定到左上角的窗口?

The following function returns the name of the buffer loaded in the top-left window: 以下函数返回左上角窗口中加载的缓冲区的名称:

function! GuiTabLabel()
    return bufname(winbufnr(1))
endfunction

Since you've mentioned MacVim, you can use guitablabel , setting it to the function that we've defined: 既然你已经提到了MacVim,你可以使用guitablabel ,将它设置为我们定义的函数:

set guitablabel=%{GuiTabLabel()}

A problem with the function above is that - depending on the working directory - it will set the tab name to the full path of the file in the top-left buffer, which can get pretty long. 上述函数的一个问题是 - 根据工作目录 - 它会将选项卡名称设置为左上角缓冲区中文件的完整路径,这可能会很长。

Change the function to use only the filename, discarding the path: 更改功能仅使用文件名,丢弃路径:

function! GuiTabLabel()
    return fnamemodify(bufname(winbufnr(1)), ":t")
endfunction

To persist this, define the function in your .vimrc, and after the function: 要坚持这一点,请在.vimrc中定义函数,并在函数之后:

set guitablabel=%!GuiTabLabel()

But the simple set isn't working for me using MacVim: it looks like MacVim startup is overwriting my changes. 但是这个简单的设置对我来说并不适合使用MacVim:看起来MacVim启动会覆盖我的更改。 Setting it in .gvimrc does work in MacVim. 在.gvimrc 设置它在MacVim中可以正常工作。 Between :scriptnames and :verbose set guitablabel it looks like just setting it in .vimrc should work, as it does in eg windows gvim. 介于:verbose set guitablabel之间:scriptnames:verbose set guitablabel它看起来只是设置它.vimrc应该可以工作,就像在例如windows gvim中一样。

See :help setting-guitablabel for more information and a more complicated function example; 请参阅:help setting-guitablabel以获取更多信息和更复杂的功能示例; see :help fname-modifiers for more path-modifier options. 请参阅:help fname-modifiers以获取更多路径修饰符选项。

You can use something like 你可以使用类似的东西

au BufEnter * set guitablabel=%{fnamemodify(bufname(winbufnr(1)), ":t")}

See :help statusline and :help guitablabel for more information. 请参阅:help statusline :help guitablabel:help guitablabel以获取更多信息。 This basically sets the tab label to the name of the top-left window. 这基本上将选项卡标签设置为左上角窗口的名称。 There rest of this is in :help ... BufEnter , fnamemodify( , bufname( , winbufnr( , etc. 剩下的就是:help ... BufEnterfnamemodify(bufname(winbufnr(等)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM