在Vim中,使用选项配置在屏幕顶部(使用选项卡时)形成选项卡行的文本tabline
.
我想对默认标签页行进行一些小调整,例如用选项卡的索引替换选项卡中的窗口数.不幸的是,它的默认版本(在tabline
未设置时是活动的)是复杂且没有文档的.对我来说没有什么可以调整的.
是否有一个Vim脚本提供了我可以根据我的需求调整的默认实现?
我使用自定义函数重置标签号和视口号,从 这里开始(参见Tonymec的评论).您可以使用它来更改显示选项卡的方式.
这就是我的.vimrc
.它只是一个稍微修改过的版本,它改变了标签#和视口#的显示方式.
"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
Run Code Online (Sandbox Code Playgroud)
以下是我的功能中定义的颜色
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
Run Code Online (Sandbox Code Playgroud)
尤达的解决方案是正确的。为了具体回答这个问题, 没有默认值tabline
。如果未设置,Vim 会自行构造显示行。实现位于Vim 7.3 源代码中的src/screen.c
at 。draw_tabline()
我希望在这里找到一个通过同一引擎运行的隐藏默认值,但遗憾的是它是纯 C 实现。让我想知道为什么他们不直接构造一个 tabline 值并使用引擎来解析它,而是在 CPU 周期计数的那一天写回 Vim,这当然要快一些。
这不是您要的答案,但我会与您分享我自己的表格。
这是第一个选项卡在其中打开三个窗口的地方,其中两个在一个编辑过的文件上打开。
(对 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() " acclamation to avoid conflict
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
Run Code Online (Sandbox Code Playgroud)