34

If the full path of a file is very long, you can't tell which file is in a given tab. so I'm wondering is there is a way let the tab only display the file name rather than the full path of the file, might be convenient in some case.

Alexis Wilke
  • 19,179
  • 10
  • 84
  • 156
Haiyuan Zhang
  • 40,802
  • 41
  • 107
  • 134

5 Answers5

36

Try

:set guitablabel=%t

For format of possible options see

:help 'statusline'

Habi
  • 3,262
  • 25
  • 23
  • 4
    This is for GUI only – Breno Leitão Jan 17 '17 at 12:39
  • 1
    Use the `tabline` option in terminal. A function is needed to specify "the whole tab pages line at once". See the help page for `setting-tabline`. – Qian Aug 14 '20 at 21:32
  • 1
    Have you read the question? The question was about VIM not Gvim, amazing how majority of answers on stackoverflow about this topic is misunderstood! – bora89 Nov 07 '22 at 07:33
26

I have the following in my vimrc:

set guitablabel=\[%N\]\ %t\ %M 

which outputs: [Number] Filename and + sign if a file is modified ([4] foo.html +). Number is very useful to immediate switch to the chosen tab with command [Number]gt (4gt if I want to jump to the file in the tab 4)

larsen1
  • 261
  • 3
  • 2
  • 4
    Should be noted that %N only shows the tab number in gui vim. If you have `set guioptions-=e`, or are using terminal vim, then %N seems to represent the number of windows (splits) open in the tab and NOT the tab number. See `h setting-tabline` or this https://github.com/mkitt/tabline.vim – overthink Oct 30 '13 at 14:26
6

I use this solution instead of Habi's as this one still keeps the default features of putting a '+' symbol in the tab to indicate the files being modified, as well as a count of the number of windows in the tab. So it basically works the same as the default tab labelling but just uses file names, not full paths.

" Tab headings
function GuiTabLabel()
    let label = ''
    let bufnrlist = tabpagebuflist(v:lnum)

    " Add '+' if one of the buffers in the tab page is modified
    for bufnr in bufnrlist
        if getbufvar(bufnr, "&modified")
            let label = '+'
            break
        endif
    endfor

    " Append the number of windows in the tab page if more than one
    let wincount = tabpagewinnr(v:lnum, '$')
    if wincount > 1
        let label .= wincount
    endif
    if label != ''
        let label .= ' '
    endif

    " Append the buffer name (not full path)
    return label . "%t"
endfunction

set guitablabel=%!GuiTabLabel()
David Terei
  • 1,985
  • 16
  • 18
  • Ideal solution: make a list of path/words/filename in all tab titles, if two titles are same, pick the unique/part/pathname for naming that tab. More complicated to code, but easier for user to disambiguate automatically. – mosh Aug 29 '16 at 05:22
  • Would you like to make a plugin from it, on GitLab? – Vitaly Zdanevich May 12 '23 at 06:31
2

The other solutions only work for GUI VIM and does not work for terminal vim or embedded vim (nvim).

You can use vim-plug and vim-airline. Then, add this to your .vimrc.

call plug#begin(stdpath('data') . '/plugged')
Plug 'vim-airline/vim-airline'
Plug 'vim-airline/vim-airline-themes'
call plug#end()

let g:airline#extensions#tabline#enabled = 1
let g:airline#extensions#tabline#fnamemod = ':t'
Armin Primadi
  • 754
  • 6
  • 10
1

Someone on the link at the bottom posted this on reddit, works flawlessly.

function! Tabline() abort
    let l:line = ''
    let l:current = tabpagenr()

    for l:i in range(1, tabpagenr('$'))
        if l:i == l:current
            let l:line .= '%#TabLineSel#'
        else
            let l:line .= '%#TabLine#'
        endif

        let l:label = fnamemodify(
            \ bufname(tabpagebuflist(l:i)[tabpagewinnr(l:i) - 1]),
            \ ':t'
        \ )

        let l:line .= '%' . i . 'T' " Starts mouse click target region.
        let l:line .= '  ' . l:label . '  '
    endfor

    let l:line .= '%#TabLineFill#'
    let l:line .= '%T' " Ends mouse click target region(s).

    return l:line
endfunction

set tabline=%!Tabline()
Zablon
  • 117
  • 1
  • 4