2011-05-08 72 views
10

在Vim中,使用tabline选项配置在屏幕顶部形成一行制表符的文本(使用制表符时)。Vim默认的'tabline'函数的实现是什么?

我想对默认的标签页行做一些小的调整,比如用选项卡的索引替换标签中的窗口数量。不幸的是,这个默认版本(在tabline未设置时处于活动状态)很复杂且没有记录。我没有什么可以调整的。

是否有一块Vim脚本提供默认实现,我可以根据需要进行调整?

回答

7

我使用自定义功能重置标签号码和视口号码,从here(请参阅Tonymec的评论)。您可以使用它来更改显示标签的方式。

这就是我在我的.vimrc。它只是一个稍微修改过的版本,它改变了tab#和viewport#的显示方式。

"Rename tabs to show tab# and # of viewports 
if exists("+showtabline") 
    function! MyTabLine() 
     let s = '' 
     let wn = '' 
     let t = tabpagenr() 
     let i = 1 
     while i <= tabpagenr('$') 
      let buflist = tabpagebuflist(i) 
      let winnr = tabpagewinnr(i) 
      let s .= '%' . i . 'T' 
      let s .= (i == t ? '%1*' : '%2*') 
      let s .= ' ' 
      let wn = tabpagewinnr(i,'$') 

      let s .= (i== t ? '%#TabNumSel#' : '%#TabNum#') 
      let s .= i 
      if tabpagewinnr(i,'$') > 1 
       let s .= '.' 
       let s .= (i== t ? '%#TabWinNumSel#' : '%#TabWinNum#') 
       let s .= (tabpagewinnr(i,'$') > 1 ? wn : '') 
      end 

      let s .= ' %*' 
      let s .= (i == t ? '%#TabLineSel#' : '%#TabLine#') 
      let bufnr = buflist[winnr - 1] 
      let file = bufname(bufnr) 
      let buftype = getbufvar(bufnr, 'buftype') 
      if buftype == 'nofile' 
       if file =~ '\/.' 
        let file = substitute(file, '.*\/\ze.', '', '') 
       endif 
      else 
       let file = fnamemodify(file, ':p:t') 
      endif 
      if file == '' 
       let file = '[No Name]' 
      endif 
      let s .= file 
      let s .= (i == t ? '%m' : '') 
      let i = i + 1 
     endwhile 
     let s .= '%T%#TabLineFill#%=' 
     return s 
    endfunction 
    set stal=2 
    set tabline=%!MyTabLine() 
endif 

在这里,在我的函数中定义的

set tabpagemax=15 
hi TabLineSel term=bold cterm=bold ctermfg=16 ctermbg=229 
hi TabWinNumSel term=bold cterm=bold ctermfg=90 ctermbg=229 
hi TabNumSel term=bold cterm=bold ctermfg=16 ctermbg=229 

hi TabLine term=underline ctermfg=16 ctermbg=145 
hi TabWinNum term=bold cterm=bold ctermfg=90 ctermbg=145 
hi TabNum term=bold cterm=bold ctermfg=16 ctermbg=145 
+0

@Mr。向导:谢谢! – abcd 2011-05-08 15:14:09

+0

我弄不清楚的唯一问题:如果存在脏缓冲区,如何添加“+”?我甚至不知道在搜索帮助中找到函数...... :) – Peeja 2011-05-13 19:21:25

+0

@Peeja:我已经修改了上面的函数(参见'endwhile'上面的第二行),它会显示一个'[+]'在未保存的缓冲区中的文件名旁边_if它是当前的buffer_。所以,当你切换到一个缓冲区并且它变脏时,它会显示'[+]'符号。 – abcd 2011-05-13 19:49:21

2

Yoda的解决方案是正确的颜色。要具体回答这个问题,tabline没有默认值。如果没有设置,Vim将自行构建显示的行。在Vim 7.3源代码中,实现位于src/screen.c,地址为draw_tabline()。我希望在这里找到一个隐藏的默认值,它是通过同一个引擎运行的,但是这是一个纯粹的C实现。让我想知道为什么他们不只是构造一个tabline值并使用引擎来解析它,而是在CPU周期计算的那一天写回了Vim,而且这肯定会稍微快一点。

2

这不是你要求的答案,但我会与你分享我自己的标准。

the wikia page的帮助下完成了,这里是我的版本。

这是第一个选项卡中有三个窗口打开的位置,其中两个在一个编辑文件上打开。

enter image description here

(约8个空格的突片对不起)

set showtabline=1 " 1 to show tabline only when more than one tab is present 
set tabline=%!MyTabLine() " custom tab pages line 
function MyTabLine() 
     let s = '' " complete tabline goes here 
     " loop through each tab page 
     for t in range(tabpagenr('$')) 
       " set highlight 
       if t + 1 == tabpagenr() 
         let s .= '%#TabLineSel#' 
       else 
         let s .= '%#TabLine#' 
       endif 
       " set the tab page number (for mouse clicks) 
       let s .= '%' . (t + 1) . 'T' 
       let s .= ' ' 
       " set page number string 
       let s .= t + 1 . ' ' 
       " get buffer names and statuses 
       let n = ''  "temp string for buffer names while we loop and check buftype 
       let m = 0  " &modified counter 
       let bc = len(tabpagebuflist(t + 1))  "counter to avoid last ' ' 
       " loop through each buffer in a tab 
       for b in tabpagebuflist(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 
         if getbufvar(b, "&buftype") == 'help' 
           let n .= '[H]' . fnamemodify(bufname(b), ':t:s/.txt$//') 
         elseif getbufvar(b, "&buftype") == 'quickfix' 
           let n .= '[Q]' 
         else 
           let n .= pathshorten(bufname(b)) 
         endif 
         " check and ++ tab's &modified count 
         if getbufvar(b, "&modified") 
           let m += 1 
         endif 
         " no final ' ' added...formatting looks better done later 
         if bc > 1 
           let n .= ' ' 
         endif 
         let bc -= 1 
       endfor 
       " add modified label [n+] where n pages in tab are modified 
       if m > 0 
         let s .= '[' . m . '+]' 
       endif 
       " select the highlighting for the buffer names 
       " my default highlighting only underlines the active tab 
       " buffer names. 
       if t + 1 == tabpagenr() 
         let s .= '%#TabLineSel#' 
       else 
         let s .= '%#TabLine#' 
       endif 
       " add buffer names 
       if n == '' 
         let s.= '[New]' 
       else 
         let s .= n 
       endif 
       " switch to no underlining and add final space to buffer list 
       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 .= '%=%#TabLineFill#%999Xclose' 
     endif 
     return s 
endfunction 
+0

注意:(可能也适用于tabline的大多数vimscript impl)当您填充时无助于挤压/缩短标签酒吧。这在大多数情况下将我限制在5个选项卡上(这对我来说并不理想),所以我做了一个控件来切换这个选项和默认选项卡实现。 – 2013-08-02 14:56:03

相关问题