Vim Tips Wiki
Register
Advertisement
Tip 1640 Printable Monobook Previous Next

created November 30, 2009 · complexity basic · author Fritzophrenic · version 7.0


You can quickly jump to a specific tab page using the gt command with a count, but it can be difficult to quickly determine the tab number you want. This task is made much easier if you display the tab's number at the beginning of its tab page label.

The following script will set up a tab label containing a "modified" indicator, the tab number, the name of the currently active buffer in the tab, and the number of windows in the tab. Drop it into your vimrc or its own file in your plugin directory.

Getting the tab number is easy, it's just the "let label.=v:lnum" portion of the script, if you don't want to use the entire thing. We could have also used the tabpagenr() function, which may be required if using 'tabline' instead. Note that we CANNOT simply use %N as stated in :help setting-guitablabel, because we are returning text from a function that is called using %{...} syntax.

Note, this script only works in GUI Vim, because it uses the (marginally easier) 'guitablabel' option instead of 'tabline', which would work in terminal Vim.

set showtabline=2 " always show tabs in gvim, but not vim

" set up tab labels with tab number, buffer name, number of windows
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 tab number
  let label .= v:lnum.': '

  " Append the buffer name
  let name = bufname(bufnrlist[tabpagewinnr(v:lnum) - 1])
  if name == ''
    " give a name to no-name documents
    if &buftype=='quickfix'
      let name = '[Quickfix List]'
    else
      let name = '[No Name]'
    endif
  else
    " get only the file name
    let name = fnamemodify(name,":t")
  endif
  let label .= name

  " Append the number of windows in the tab page
  let wincount = tabpagewinnr(v:lnum, '$')
  return label . '  [' . wincount . ']'
endfunction

set guitablabel=%{GuiTabLabel()}

Since a tab can contain multiple windows, it can be nice to see at a glance which buffers are loaded in each window in a non-current tab. There is not enough space in a tab label for this, but we can add a tooltip so that we can see what files are loaded by hovering the mouse over the tab label in question. Again, this will only work in GUI Vim.

" set up tab tooltips with every buffer name
function! GuiTabToolTip()
  let tip = ''
  let bufnrlist = tabpagebuflist(v:lnum)

  for bufnr in bufnrlist
    " separate buffer entries
    if tip!=''
      let tip .= ' | '
    endif

    " Add name of buffer
    let name=bufname(bufnr)
    if name == ''
      " give a name to no name documents
      if getbufvar(bufnr,'&buftype')=='quickfix'
        let name = '[Quickfix List]'
      else
        let name = '[No Name]'
      endif
    endif
    let tip.=name

    " add modified/modifiable flags
    if getbufvar(bufnr, "&modified")
      let tip .= ' [+]'
    endif
    if getbufvar(bufnr, "&modifiable")==0
      let tip .= ' [-]'
    endif
  endfor

  return tip
endfunction

set guitabtooltip=%{GuiTabToolTip()}

References

Comments

The tip could be tweaked to use the 'tabline' option for terminal Vim.


Should probably start with a simple version, something like following.

Set tab label to show tab number, filename, if modified ('+' is shown if the current window in the tab has been modified):

:set guitablabel=%N/\ %t\ %M

Then mention how the script shows '+' if any window in the tab has been modified. JohnBeckett 07:27, May 19, 2010 (UTC)


Yes, I like this. It was my original intent with this tip to include such a simple introductory example.

In fact, it may be better to tweak this easy example to remove as much script as possible. I believe the script gains very little other than what you mention. The only other thing I see is that it shows the number of windows in the tab, but I'm not sure whether that's possible without the script or not.

I believe we can readily use something like %N/\ %t\ %{ModifiedWindow()} for the tab line...not sure though.

--Fritzophrenic 17:24, May 19, 2010 (UTC)

Advertisement