0

It's possible to change the colours used for the whole tab bar, but is it possible to set the colours used for individual tabs?

That way I could colour-code tabs based on the file path, or extension, or other criteria.

1 Answer 1

1

With a lot of creativity and vimscript you can try to come up with something. To start read and try examples from :h setting-tabline.

In short, wouldn't be easy but looks like should be possible, once you also define rules for the tabpages that will have several windows with different buffers with different paths, filetypes and what not.

enter image description here


hi TabColor1 guibg=green guifg=blue
hi TabColor2 guibg=blue guifg=yellow
hi TabColor3 guibg=red guifg=yellow

:set tabline=%!MyTabLine()

function MyTabLine()
  let s = ''
  for i in range(tabpagenr('$'))
    " dummy selection of highlighting
    if i + 1 == tabpagenr()
      let s ..= '%#TabColor1#'
    elseif i == 2
      let s ..= '%#TabColor2#'
    else
      let s ..= '%#TabColor3#'
    endif

    " set the tab page number (for mouse clicks)
    let s ..= '%' .. (i + 1) .. 'T'

    " the label is made by MyTabLabel()
    let s ..= ' %{MyTabLabel(' .. (i + 1) .. ')} '
  endfor

  " after the last tab fill with TabLineFill and reset tab page nr
  let s ..= '%#TabLineFill#%T'

  " right-align the label to close the current tab page
  if tabpagenr('$') > 1
    let s ..= '%=%#TabLine#%999Xclose'
  endif

  return s
endfunction

function MyTabLabel(n)
  let buflist = tabpagebuflist(a:n)
  let winnr = tabpagewinnr(a:n)
  let bname = bufname(buflist[winnr - 1])
  return empty(bname) ? "[No name]" : bname
endfunction
2
  • Checking that I understand right: this is done by injecting colour-selecting format characters into the tab line?
    – Len
    Feb 22 at 23:46
  • 1
    This is done by doing all tabline on your own, all the text and highlighting. The format of tabline is similar or the same as for statusline. And yes you can specify what highlight group should be used for the text that "would follow" -- %#TabColor1#
    – Maxim Kim
    Feb 23 at 9:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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