Vim: Enhance Vim tabs (file name only, file status, and the ability to rename tabs)

5/5
" Language: Vim script
" Author: James Cherti
" License: MIT
" Description: Enhance the tab line. The tabs will show the base name of 
"              the file and its status. Tabs can be renamed with 'TabRename'. 
" URL: https://www.jamescherti.com/vim-improve-the-tab-line-basename-of-the-file-and-status/

function! MyTabLabel(tabnr) abort
  let l:bufnr = tabpagebuflist(a:tabnr)[tabpagewinnr(a:tabnr) - 1]

  let l:modified = 0
  if getbufvar(l:bufnr, '&modified')
    let l:modified = 1
  endif

  let l:tablabel = ''
  let l:custom_tablabel = gettabvar(a:tabnr, 'tablabel', '')
  if empty(l:custom_tablabel)
    let l:bufname = bufname(l:bufnr)
    if empty(l:bufname)
      let l:tablabel = empty(&buftype) ? 'No Name' : '<' . &buftype . '>'
    else
      let l:tablabel = fnamemodify(l:bufname, ':t')
    endif
  else
    let l:tablabel .= l:custom_tablabel
  endif

  if l:modified
    let l:tablabel .= '*'
  endif

  return l:tablabel
endfunction

function! MyTabLine() abort
  let l:tabline = ''

  for l:num in range(1, tabpagenr('$'))
    let l:tabline .= (l:num != tabpagenr()) ? '%#TabLine#' : '%#TabLineSel#'
    let l:tabline .= '%' . l:num . 'T %{MyTabLabel(' . l:num . ')} '
  endfor

  let l:tabline .= '%#TabLineFill#%T%='
  let l:tabline .= repeat('%#TabLine#%999X[X]', l:num > 1)

  return l:tabline
endfunction

function! MyGuiTabLine() abort
  return MyTabLabel(tabpagenr())
endfunction

function! TabRename(tablabel) abort
  let t:tablabel = a:tablabel
  execute 'redrawtabline'
endfunction

if exists('+showtabline')
  command! -nargs=1 TabRename call TabRename(<q-args>)
  
  set tabline=%!MyTabLine()
  set guitablabel=%{MyGuiTabLine()}
endifCode language: Vim Script (vim)