Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

vim

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?

like image 829
Piotr Kochański Avatar asked Apr 29 '13 11:04

Piotr Kochański


1 Answers

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.

like image 158
Ingo Karkat Avatar answered Oct 08 '22 20:10

Ingo Karkat