简体   繁体   中英

How to have different title and tab title in vim

As of now I have put the following in my .vimrc file in order to display the name of the currently open file in the screen title of my Terminal:

autocmd BufEnter * let &titlestring = expand("%:@")
set title

Is it possible to also set a specific tab title? That is, when I have a number of vim tabs open in Terminal on my Mac I would Terminal to display the directory path in the Terminal header when I have selected the tab related to the file and I would like to display only the name of the file in the header of the tab.

Some comments to your goal

Personally I hardly use tabs in vim. What you want to have can be achieved by setting some options. Before I talk about the options, I would say that what you want to do could be inconvenient. Because in vim, tab is a collection of windows, which means, it can have splitted windows, and in a tab page, you can show more than one buffers. That is, in a tab page, it is not always showing one file. As you are switching from split to split in a tab page, both your terminal title and the tab label could be changing.

For example, you have 3 dirs, /one /two and /three and two tab pages in your vim.

Now you have:

tab1:
/one/file1

tab2 (in split-windows):
/one/file1
/two/file1
/three/file1

When you are in tab1 , your terminal title would be /one , tab1-label would be file1 , fine. But when you are in tab2, your terminal title will change as you are switching from split to split. In this example, the tab-label would be same file1 , so you have to check terminal window and the tab label to know which buffer/file you are currently editing. I don't think this is convenient.

Answer to your question

Okay, if you want that to happen, :h setting-tabline and check the example in the help text, it shows how to set the label of tabs. This should achieve your goal for vim in Terminal.

FYI, to get only parent dir name you can use %:p:h . to get only filename, you can use %:p:t .

Plus, some other things you may want to check:

  • There is another autocommand event: TabEnter .
  • :h setting-tabline if you want your tabline to show some complex text or other highlighting
  • if you want to play with filename/dirname further, you can check :h expand() and :h filename-modifiers

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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