Vim Tips Wiki
Register
(clean up, don't have time to separate out components, and darnit I LIKE this version :-))
(remove dealt-with comments and make the "todo" just a comment; "tabline" not "tablabel")
Line 15: Line 15:
 
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.
 
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 {{help|prefix=no|'tablabel'}} instead. Note that we CANNOT simply use <tt>%N</tt> as stated in {{help|setting-guitablabel}}, because we are returning text from a function that is called using <tt>%{...}</tt> syntax.
+
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 {{help|prefix=no|'tabline'}} instead. Note that we CANNOT simply use <tt>%N</tt> as stated in {{help|setting-guitablabel}}, because we are returning text from a function that is called using <tt>%{...}</tt> syntax.
   
Note, this script only works in GUI Vim, because it uses the (marginally easier) {{help|prefix=no|'guitablabel'}} option instead of {{help|prefix=no|'tablabel'}}, which would work in terminal Vim.
+
Note, this script only works in GUI Vim, because it uses the (marginally easier) {{help|prefix=no|'guitablabel'}} option instead of {{help|prefix=no|'tabline'}}, which would work in terminal Vim.
   
 
<pre>
 
<pre>
Line 108: Line 108:
   
 
==Comments==
 
==Comments==
 
The tip could be tweaked to use the 'tabline' option for terminal Vim.
{{todo}}
 
* Tweak to use tablabel option for terminal Vim
 
----
 
Looks good. In the first function, I think <tt>tabpagenr()</tt> should be <tt>v:lnum</tt>. We should probably just give this a very quick cosmetic tweak and remove the dodgy tag, and leave optimisation for later. [[User:JohnBeckett|JohnBeckett]] 10:40, May 6, 2010 (UTC)
 
:Thanks, that does make more sense. I made a few quick changes, not sure if more are in order. --[[User:Fritzophrenic|Fritzophrenic]] 14:57, May 6, 2010 (UTC)
 

Revision as of 04:14, 15 May 2010

Proposed tip Please edit this page to improve it, or add your comments below (do not use the discussion page).

Please use new tips to discuss whether this page should be a permanent tip, or whether it should be merged to an existing tip.
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.