Vim:在标签行中显示标签的索引

Vim:在标签行中显示标签的索引

假设我打开了file1.txtfile2.txtfile3a.txtfile3b.txt使得标签行(顶部的东西)如下所示:

file1.txt  file2.txt  2 file3a.txt

(请注意file3b.txt.是如何缺失的,因为它在与 的同一选项卡中拆分显示file3a.txt

为了在选项卡之间更快地移动(使用<Number>gt),我希望每个选项卡都显示其索引以及文件名。如下所示:

1:<file1.txt>  2:<file2.txt>  3:<2 file3a.txt>

格式(特别是尖括号)是可选的;我只希望索引出现在那里(1:2:等等)。

谷歌上没有:h tab-page-commands任何线索。

答案1

您需要查看:

:help 'tabline'
:help setting-tabline

如果你的 'guioptions' 设置中有“e”:

:help 'guitablabel'

答案2

把这个放到你的 vimrc 中

" Rename tabs to show tab number.
" (Based on http://stackoverflow.com/questions/5927952/whats-implementation-of-vims-default-tabline-function)
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 .= '%#TabNum#'
            let s .= i
            " 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 i = i + 1
        endwhile
        let s .= '%T%#TabLineFill#%='
        let s .= (tabpagenr('$') > 1 ? '%999XX' : 'X')
        return s
    endfunction
    set stal=2
    set tabline=%!MyTabLine()
    set showtabline=1
    highlight link TabNum Special
endif

答案3

维基百科页面您可能会发现至少两个(我测试过的)为您提供选项卡索引,其中一个生成每个缓冲区内有编辑的窗口数量。

这是我对生成编辑缓冲区计数的修改结果,我所做的更改是使计数的突出显示值与选项卡的其余部分保持一致:

在此处输入图片描述

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"

答案4

对于基于 GUI 的 Vim(Linux 上的 Gvim、Mac 上的 MacVim 等),将其放入.gvimrc

set guitablabel=%N:%M%t " Show tab numbers

有关实际使用显示数字的一些提示:

  • Ngt将切换到选项卡N。例如,3gt转到选项卡 3。
  • :tabm2移动当前标签页以显示标签 2.
    • 要将此选项卡移动到第一个位置,请使用:tabm0
    • 要将此选项卡移动到最后一个位置,只需使用:tabm

相关内容