Vim Tips Wiki
Register
(→‎Comments: suggestion)
(clean up, don't have time to separate out components, and darnit I LIKE this version :-))
Line 11: Line 11:
 
|category2=
 
|category2=
 
}}
 
}}
  +
You can quickly jump to a specific tab page using the {{help|prefix=no|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.
{{dodgy|This tip is still in a draft version, not yet ready for general consumption. You can probably get some good use out of it, but ''caveat emptor''. Please feel free to improve this tip as much as you can!}}
 
   
  +
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.
It can be hard to quickly determine the tab number you want when jumping to a specific tab by giving a count to the <tt>gt</tt> command. This makes it easier by displaying the tab page number at the beginning of the tab page text. Note, only works in gui Vim.
 
   
  +
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.
{{todo}}
 
* Tweak to use tablabel option for terminal Vim
 
* Lose the tooltip portion, adding a "see also" might be in order.
 
* Emphasize the specific portion that deals with adding the tab page number, possibly removing the rest of the script or moving it to a sub page. Would be best to duplicate the default tab page text in the tip proper and provide the alternative as a subpage (maybe a user subpage of [[User:Fritzophrenic]]).
 
   
  +
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.
Drop the following in your [[vimrc]]:
 
   
 
<pre>
 
<pre>
Line 39: Line 36:
   
 
" Append the tab number
 
" Append the tab number
let label .= tabpagenr().': '
+
let label .= v:lnum.': '
   
 
" Append the buffer name
 
" Append the buffer name
Line 61: Line 58:
 
endfunction
 
endfunction
   
 
set guitablabel=%{GuiTabLabel()}
  +
</pre>
  +
  +
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.
  +
  +
<pre>
 
" set up tab tooltips with every buffer name
 
" set up tab tooltips with every buffer name
 
function! GuiTabToolTip()
 
function! GuiTabToolTip()
Line 96: Line 99:
 
endfunction
 
endfunction
   
set guitablabel=%{GuiTabLabel()}
 
 
set guitabtooltip=%{GuiTabToolTip()}
 
set guitabtooltip=%{GuiTabToolTip()}
 
</pre>
 
</pre>
  +
  +
==References==
  +
* {{help|'guitablabel'}}
  +
* {{help|setting-guitablabel}}
  +
* {{help|'guitabtooltip'}}
   
 
==Comments==
 
==Comments==
 
{{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)
 
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 14:57, 6 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 'tablabel' 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 'tablabel', 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

 TO DO 

  • Tweak to use tablabel option for terminal Vim

Looks good. In the first function, I think tabpagenr() should be v:lnum. We should probably just give this a very quick cosmetic tweak and remove the dodgy tag, and leave optimisation for later. 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. --Fritzophrenic 14:57, May 6, 2010 (UTC)