Vim Tips Wiki
Register
We recommend that you log in before editing. This will allow other users to leave you a message about your edit, and will let you track edits via your Watchlist. Creating an account is quick and free.

The edit can be undone. Please check the comparison below to verify that this is what you want to do, and then publish the changes below to finish undoing the edit.

Latest revision Your text
Line 21: Line 21:
 
<pre>
 
<pre>
 
set showtabline=2 " always show tabs in gvim, but not vim
 
set showtabline=2 " always show tabs in gvim, but not vim
  +
 
" set up tab labels with tab number, buffer name, number of windows
 
" set up tab labels with tab number, buffer name, number of windows
 
function! GuiTabLabel()
 
function! GuiTabLabel()
 
let label = ''
 
let label = ''
 
let bufnrlist = tabpagebuflist(v:lnum)
 
let bufnrlist = tabpagebuflist(v:lnum)
  +
 
" Add '+' if one of the buffers in the tab page is modified
 
" Add '+' if one of the buffers in the tab page is modified
 
for bufnr in bufnrlist
 
for bufnr in bufnrlist
Line 32: Line 34:
 
endif
 
endif
 
endfor
 
endfor
  +
 
" Append the tab number
 
" Append the tab number
 
let label .= v:lnum.': '
 
let label .= v:lnum.': '
  +
 
" Append the buffer name
 
" Append the buffer name
 
let name = bufname(bufnrlist[tabpagewinnr(v:lnum) - 1])
 
let name = bufname(bufnrlist[tabpagewinnr(v:lnum) - 1])
Line 48: Line 52:
 
endif
 
endif
 
let label .= name
 
let label .= name
  +
 
" Append the number of windows in the tab page
 
" Append the number of windows in the tab page
 
let wincount = tabpagewinnr(v:lnum, '$')
 
let wincount = tabpagewinnr(v:lnum, '$')
 
return label . ' [' . wincount . ']'
 
return label . ' [' . wincount . ']'
 
endfunction
 
endfunction
  +
 
set guitablabel=%{GuiTabLabel()}
 
set guitablabel=%{GuiTabLabel()}
 
</pre>
 
</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.
 
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>
 
<pre>
 
" set up tab tooltips with every buffer name
 
" set up tab tooltips with every buffer name
Line 61: Line 68:
 
let tip = ''
 
let tip = ''
 
let bufnrlist = tabpagebuflist(v:lnum)
 
let bufnrlist = tabpagebuflist(v:lnum)
  +
 
for bufnr in bufnrlist
 
for bufnr in bufnrlist
 
" separate buffer entries
 
" separate buffer entries
Line 66: Line 74:
 
let tip .= " \n "
 
let tip .= " \n "
 
endif
 
endif
  +
 
" Add name of buffer
 
" Add name of buffer
 
let name=bufname(bufnr)
 
let name=bufname(bufnr)
Line 77: Line 86:
 
endif
 
endif
 
let tip.=name
 
let tip.=name
  +
 
" add modified/modifiable flags
 
" add modified/modifiable flags
 
if getbufvar(bufnr, "&modified")
 
if getbufvar(bufnr, "&modified")
Line 85: Line 95:
 
endif
 
endif
 
endfor
 
endfor
  +
 
return tip
 
return tip
 
endfunction
 
endfunction
  +
 
set guitabtooltip=%{GuiTabToolTip()}
 
set guitabtooltip=%{GuiTabToolTip()}
 
</pre>
 
</pre>
Line 159: Line 171:
 
set tabline=%!MyTabLine()
 
set tabline=%!MyTabLine()
 
map <C-Tab> :tabnext<CR>
 
map <C-Tab> :tabnext<CR>
imap <C-Tab> <C-O>:tabnext<CR>
+
imap <C-Tab> <C-O>:tabnext<CR>
 
map <C-S-Tab> :tabprev<CR>
 
map <C-S-Tab> :tabprev<CR>
 
imap <C-S-Tab> <C-O>:tabprev<CR>
 
imap <C-S-Tab> <C-O>:tabprev<CR>
Line 167: Line 179:
 
This function is not perfect: the name for a netrw directory appears as [No Name], just like for an unnamed buffer. Feel free to enhance it if you can. — [[User:Tonymec|Tonymec]] 22:49, August 14, 2010 (UTC)
 
This function is not perfect: the name for a netrw directory appears as [No Name], just like for an unnamed buffer. Feel free to enhance it if you can. — [[User:Tonymec|Tonymec]] 22:49, August 14, 2010 (UTC)
 
-----
 
-----
Here is my tabline function. Unlike the others, it doesn't rely on +gui. It will display 1: (tab #), [n+] (modified count on tab--only if needed) and special help and quickfix buffer labels. This tabline shows all active buffers. To minimize space, dir paths of regular files are shortened to first characters. I based it on the sample tabline in the vim docs.
+
Here is my tabline function. Unlike the others, it doesn't rely on +gui. It will display 1: (tab #), [n+] (modified count on tab--only if needed) and special help and quickfix buffer labels. This tabline shows all active buffers. To minimize space, dir paths of regular files are shortened to first characters. I based it on the sample tabline in the vim docs.
   
Buyer Beware! This is my first attempt at a vim script. If it destroys things, don't say I didn't warn you! Use at your own risk. Finally, any pointers and improvements are welcome.
+
Buyer Beware! This is my first attempt at a vim script. If it destroys things, don't say I didn't warn you! Use at your own risk. Finally, any pointers and improvements are welcome.
   
 
To use, just copy and paste the code into your .vimrc.
 
To use, just copy and paste the code into your .vimrc.
   
 
<pre>
 
<pre>
set showtabline=2 " 0, 1 or 2; when to use a tab pages line
+
set showtabline=2 " 0, 1 or 2; when to use a tab pages line
set tabline=%!MyTabLine() " custom tab pages line
+
set tabline=%!MyTabLine() " custom tab pages line
 
function MyTabLine()
 
function MyTabLine()
  +
let s = '' " complete tabline goes here
+
let s = '' " complete tabline goes here
" loop through each tab page
 
  +
for t in range(tabpagenr('$'))
 
 
" loop through each tab page
" set highlight for tab number and &modified
 
 
for t in range(tabpagenr('$'))
let s .= '%#TabLineSel#'
 
  +
" set the tab page number (for mouse clicks)
 
 
" set highlight for tab number and &modified
let s .= '%' . (t + 1) . 'T'
 
 
let s .= '%#TabLineSel#'
" set page number string
 
  +
let s .= t + 1 . ':'
 
 
" set the tab page number (for mouse clicks)
" get buffer names and statuses
 
 
let s .= '%' . (t + 1) . 'T'
let n = '' "temp string for buffer names while we loop and check buftype
 
  +
let m = 0 " &modified counter
 
 
" set page number string
let bc = len(tabpagebuflist(t + 1)) "counter to avoid last ' '
 
 
let s .= t + 1 . ':'
" loop through each buffer in a tab
 
  +
for b in tabpagebuflist(t + 1)
 
 
" get buffer names and statuses
" buffer types: quickfix gets a [Q], help gets [H]{base fname}
 
 
let n = '' "temp string for buffer names while we loop and check buftype
" others get 1dir/2dir/3dir/fname shortened to 1/2/3/fname
 
 
let m = 0 " &modified counter
if getbufvar( b, "&buftype" ) == 'help'
 
 
let bc = len(tabpagebuflist(t + 1)) "counter to avoid last ' '
let n .= '[H]' . fnamemodify( bufname(b), ':t:s/.txt$//' )
 
  +
elseif getbufvar( b, "&buftype" ) == 'quickfix'
 
 
" loop through each buffer in a tab
let n .= '[Q]'
 
 
for b in tabpagebuflist(t + 1)
else
 
  +
let n .= pathshorten(bufname(b))
 
 
" buffer types: quickfix gets a [Q], help gets [H]{base fname}
endif
 
 
" others get 1dir/2dir/3dir/fname shortened to 1/2/3/fname
" check and ++ tab's &modified count
 
if getbufvar( b, "&modified" )
+
if getbufvar( b, "&buftype" ) == 'help'
 
let n .= '[H]' . fnamemodify( bufname(b), ':t:s/.txt$//' )
let m += 1
 
 
elseif getbufvar( b, "&buftype" ) == 'quickfix'
endif
 
  +
let n .= '[Q]'
" no final ' ' added...formatting looks better done later
 
 
else
if bc > 1
 
let n .= ' '
+
let n .= pathshorten(bufname(b))
endif
+
endif
  +
let bc -= 1
 
 
" check and ++ tab's &modified count
endfor
 
 
if getbufvar( b, "&modified" )
" add modified label [n+] where n pages in tab are modified
 
if m > 0
+
let m += 1
 
endif
let s .= '[' . m . '+]'
 
  +
endif
 
 
" no final ' ' added...formatting looks better done later
" select the highlighting for the buffer names
 
 
if bc > 1
" my default highlighting only underlines the active tab
 
 
let n .= ' '
" buffer names.
 
 
endif
if t + 1 == tabpagenr()
 
  +
let s .= '%#TabLine#'
 
  +
let bc -= 1
else
 
  +
let s .= '%#TabLineSel#'
 
 
endfor
endif
 
  +
" add buffer names
 
 
" add modified label [n+] where n pages in tab are modified
let s .= n
 
 
if m > 0
" switch to no underlining and add final space to buffer list
 
let s .= '%#TabLineSel#' . ' '
+
let s .= '[' . m . '+]'
  +
endif
endfor
 
  +
" after the last tab fill with TabLineFill and reset tab page nr
 
 
" select the highlighting for the buffer names
let s .= '%#TabLineFill#%T'
 
" right-align the label to close the current tab page
+
" my default highlighting only underlines the active tab
 
" buffer names.
if tabpagenr('$') > 1
 
 
if t + 1 == tabpagenr()
let s .= '%=%#TabLineFill#%999Xclose'
 
 
let s .= '%#TabLine#'
endif
 
 
else
return s
 
 
let s .= '%#TabLineSel#'
  +
endif
  +
 
" add buffer names
 
let s .= n
  +
 
" switch to no underlining and add final space to buffer list
 
let s .= '%#TabLineSel#' . ' '
  +
 
endfor
  +
 
" after the last tab fill with TabLineFill and reset tab page nr
 
let s .= '%#TabLineFill#%T'
  +
 
" right-align the label to close the current tab page
 
if tabpagenr('$') > 1
 
let s .= '%=%#TabLineFill#%999Xclose'
  +
endif
  +
 
return s
  +
 
endfunction
 
endfunction
 
</pre>
 
</pre>
Line 244: Line 277:
 
set tabline=%!MyTabLine()
 
set tabline=%!MyTabLine()
 
function MyTabLine()
 
function MyTabLine()
  +
let s = '' " complete tabline goes here
+
let s = '' " complete tabline goes here
" loop through each tab page
 
  +
for t in range(tabpagenr('$'))
 
 
" loop through each tab page
" select the highlighting for the buffer names
 
if t + 1 == tabpagenr()
+
for t in range(tabpagenr('$'))
  +
let s .= '%#TabLineSel#'
 
 
" select the highlighting for the buffer names
else
 
let s .= '%#TabLine#'
+
if t + 1 == tabpagenr()
  +
let s .= '%#TabLineSel#'
endif
 
" empty space
+
else
let s .= ' '
+
let s .= '%#TabLine#'
" set the tab page number (for mouse clicks)
+
endif
  +
let s .= '%' . (t + 1) . 'T'
 
" set page number string
+
" empty space
let s .= t + 1 . ' '
+
let s .= ' '
  +
" get buffer names and statuses
 
let n = '' "temp string for buffer names while we loop and check buftype
+
" set the tab page number (for mouse clicks)
let m = 0 " &modified counter
+
let s .= '%' . (t + 1) . 'T'
  +
let bc = len(tabpagebuflist(t + 1)) "counter to avoid last ' '
 
  +
" set page number string
" loop through each buffer in a tab
 
for b in tabpagebuflist(t + 1)
+
let s .= t + 1 . ' '
  +
" buffer types: quickfix gets a [Q], help gets [H]{base fname}
 
" others get 1dir/2dir/3dir/fname shortened to 1/2/3/fname
+
" get buffer names and statuses
  +
let n = '' "temp string for buffer names while we loop and check buftype
if getbufvar( b, "&buftype" ) == 'help'
 
let n .= '[H]' . fnamemodify( bufname(b), ':t:s/.txt$//' )
+
let m = 0 " &modified counter
 
let bc = len(tabpagebuflist(t + 1)) "counter to avoid last ' '
elseif getbufvar( b, "&buftype" ) == 'quickfix'
 
  +
let n .= '[Q]'
 
 
" loop through each buffer in a tab
else
 
let n .= pathshorten(bufname(b))
+
for b in tabpagebuflist(t + 1)
  +
"let n .= bufname(b)
 
 
" buffer types: quickfix gets a [Q], help gets [H]{base fname}
endif
 
  +
" others get 1dir/2dir/3dir/fname shortened to 1/2/3/fname
" check and ++ tab's &modified count
 
if getbufvar( b, "&modified" )
+
if getbufvar( b, "&buftype" ) == 'help'
  +
let n .= '[H]' . fnamemodify( bufname(b), ':t:s/.txt$//' )
let m += 1
 
 
elseif getbufvar( b, "&buftype" ) == 'quickfix'
endif
 
  +
let n .= '[Q]'
" no final ' ' added...formatting looks better done later
 
if bc > 1
+
else
let n .= ' '
+
let n .= pathshorten(bufname(b))
 
"let n .= bufname(b)
endif
 
let bc -= 1
+
endif
  +
endfor
 
" add modified label [n+] where n pages in tab are modified
+
" check and ++ tab's &modified count
 
if getbufvar( b, "&modified" )
if m > 0
 
"let s .= '[' . m . '+]'
+
let m += 1
let s.= '+ '
+
endif
  +
endif
 
 
" no final ' ' added...formatting looks better done later
" add buffer names
 
if n == ''
+
if bc > 1
let s .= '[No Name]'
+
let n .= ' '
 
endif
else
 
  +
let s .= n
 
 
let bc -= 1
endif
 
  +
" switch to no underlining and add final space to buffer list
 
"let s .= '%#TabLineSel#' . ' '
+
endfor
  +
let s .= ' '
 
  +
" add modified label [n+] where n pages in tab are modified
endfor
 
 
if m > 0
" after the last tab fill with TabLineFill and reset tab page nr
 
let s .= '%#TabLineFill#%T'
+
"let s .= '[' . m . '+]'
 
let s.= '+ '
" right-align the label to close the current tab page
 
 
endif
if tabpagenr('$') > 1
 
  +
let s .= '%=%#TabLine#%999XX'
 
 
" add buffer names
endif
 
 
if n == ''
return s
 
 
let s .= '[No Name]'
 
else
 
let s .= n
 
endif
  +
 
" switch to no underlining and add final space to buffer list
  +
"let s .= '%#TabLineSel#' . ' '
 
let s .= ' '
  +
 
endfor
  +
 
" after the last tab fill with TabLineFill and reset tab page nr
 
let s .= '%#TabLineFill#%T'
  +
  +
" right-align the label to close the current tab page
 
if tabpagenr('$') > 1
 
let s .= '%=%#TabLine#%999XX'
 
endif
  +
 
return s
  +
 
endfunction
 
endfunction
 
</pre>
 
</pre>
Please note that all contributions to the Vim Tips Wiki are considered to be released under the CC-BY-SA
Cancel Editing help (opens in new window)