Warm tip: This article is reproduced from serverfault.com, please click
vim

VIM: how to show folder name in tab, but only if two files have the same name

发布于 2013-04-29 11:11:27

I would like to have the following feature in VIM (GVIM in particular). I think Sublime Text has something like that:

  1. In the "normal" case the tab name should be just the file's name, but...
  2. If there are two files opened with the same name but in different directories, I would like to see a tab name parent folder name + file name.

Example:

When there are tabs for the following files:

  • c:\my\dir\with\files\justAfile.txt
  • c:\my\dir\with\files\myfile.txt
  • c:\my\dir\with\backup\myfile.txt

Tab names would be then:

justAfile.txt | files\myfile.txt | backup\myfile.txt 

Is this doable with some clever configuration?

Questioner
Piotr Kochański
Viewed
0
Ingo Karkat 2013-04-29 19:35:36

In GVIM, you can customize the tab labels with the 'guitablabel' option.

In terminal Vim; there's no 'guitablabel' equivalent; one has to render the entire 'tabline'. Fortunately, the Vim help has an example which delegates the label rendering to a separate function, so re-using your custom function is pretty easy.

The help pages for the mentioned options link to examples; you probably have to use fnamemodify() to canonicalize all buffers' paths to full absolute paths, find the common base directory, and then strip that off the paths.

On the other hand, if it's okay for you to :cd to the base directory, you'll get that kind of tab label pretty much out-of-the-box.