2

I have a couple of tabs open. Is it possible to set custom tab page names for each individual tab page?

For example, I want to name one tab "database", another "backend", and another "frontend". I just want static labels. Does something like rename-window from tmux exist in vim? (Without a plugin.)

Update: I'm curious about vim in the terminal, not gvim.

1

There are two tablines in Vim: a "regular" tabline, and a GUI tabline.

If you're running GVim and guioptions contains e (this is the default) then the GUI tabline is used.

Tweaking the GUI tabline is pretty straightforward:

" use t:TabLabel if set; otherwise use the defaults
let &guitablabel='%{get(t:,"TabLabel","")}'

And then set the labels with:

call settabvar(1, "TabLabel", "Database")
call settabvar(2, "TabLabel", "Backend")
call settabvar(3, "TabLabel", "Frontend")

And so on.

However, for the regular tabline you have to compose the whole string at once, so it gets much more tricky.

The following code is loosely based on the example code from :h setting-tabline

" get some label for a single tab
function! MyTabLabel(tabarg)
    " try t:TabLabel
    let l:result = gettabvar(a:tabarg, "TabLabel", "")
    " or active buffer name
    if empty(l:result)
        let l:result = bufname(winbufnr(win_getid(tabpagewinnr(a:tabarg), a:tabarg)))
    endif
    " or some fixed string
    if empty(l:result)
        let l:result = '[Noname]'
    endif
    " truncate the path, so (hopefully) it will not get too long
    return l:result[strridx(l:result, '/') + 1 : ]
endfunction

" build the whole tabline
function! MyTabLine()
    let l:result = ''
    " all our tabs
    for l:num in range(1, tabpagenr("$"))
        " tab color
        let l:result .= (l:num != tabpagenr()) ? '%#TabLine#' : '%#TabLineSel#'
        " tab text
        let l:result .= '%' . l:num . 'T %{MyTabLabel(' . l:num . ')} '
    endfor
    " space filler
    let l:result .= '%#TabLineFill#%T%='
    " [X] button on the right if there are several tabs
    let l:result .= repeat('%#TabLine#%999X[X]', l:num > 1)
    return l:result
endfunction

" setup our tabline
set tabline=%!MyTabLine()

" set sample labels
call settabvar(1, "TabLabel", "Database")
call settabvar(2, "TabLabel", "Backend")
call settabvar(3, "TabLabel", "Frontend")

And the last but not least, make sure that showtabline is not equal to zero, or your tabline will never be shown.

  • Can't you do it with airline somehow? – eyal karni Sep 16 at 7:30
  • @eyalkarni I don't use airline. But I think the code should work as is if airline does not touch the tabline (and this is the default, I believe). – Matt Sep 16 at 7:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.