Exploit the tabline!

1 Point

Lifepillar Lifepillar

2 months ago

The tabline can display arbitrary information using the same syntax used to customize the status line. Use that space for displaying additional information! For example, I like to show the current working directory in the right side of the tabline. This is achieved with the snippet below.

The MyTabLabel() is used to draw a single label (tab number followed by the current buffer's name—feel free to tweak it as you prefer).

The MyTabLine() function is just a syntactic variation of the function found in :h setting-tabline, with an added variable for customization (g:my_tabline). See :h 'statusline' for the syntax.

Note: the snippet requires Vim compiled with +lambda.

" Set the content of the right side of the tabline:
let g:my_tabline = '⌘ %<%{getcwd()}'

" Do not need to change below this line
fun! MyTabLabel(nr)
  return ' ' . a:nr . ' ' . (get(extend(t:, {
        \ 'tablabel': fnamemodify(bufname(tabpagebuflist(a:nr)[tabpagewinnr(a:nr) - 1]), ':t')
        \ }), 'tablabel') == '' ? '[No Name]' : get(t:, 'tablabel'))
endf

fun! MyTabLine()
  return join(map(range(1, tabpagenr('$')),
        \     { _,i -> '%#TabLine' . (i == tabpagenr() ? 'Sel#' : '#') .'%'.i.'T %{MyTabLabel('.i.')}' }
        \ ))
        \ . '%#TabLineFill#%T%=' . g:my_tabline . ' ' . (tabpagenr('$') > 1 ? '%999X✕ ' : '')
endf

set tabline=%!MyTabLine()
set showtabline=2 " Always show the tab bar