vi / vim -如何将选项卡标签/标题长度设置为固定的字符数

gkn4icbw  于 2022-12-13  发布在  其他
关注(0)|答案(6)|浏览(211)

当我在vi/vim(7.2)中打开新的标签页时,如果打开的文件在不同的路径下,标签页标题会显示完整的路径,并占据屏幕真实的,因此其他标签页是不可见的。这意味着我不能用鼠标点击我想要的标签页,而是必须求助于:键盘命令在选项卡之间移动(& K)。
有没有什么方法可以限制标签标题的最大“大小/长度”,所以我只能看到一个文件的最后12个字符在一个遥远的相对路径?

0g0grzrc

0g0grzrc1#

你可以很好地为gvim做这件事,设置为“guitablabel”。
gvimrc中的一段摘录,它修改了默认设置,只显示文件名的12个字符,但保留了修改后的缓冲区的“+”。工具提示没有修改,所以你可以从中得到完整的路径,或者在命令模式下按Ctrl-G。

if version >= 700
    "set showtabline to show when more than one tab
    set showtabline=1
    "set tab labels to show at most 12 characters
    set guitablabel=%-0.12t%M
endif

" don't show the toolbar in the GUI (only the menu)
set guioptions-=T

" don't show tear-off menus
set guioptions-=t
e4yzc0pl

e4yzc0pl2#

在回答我自己的问题时:
在阅读了上面的Chad Birch并在谷歌上搜索setting-tabline后,我发现了TabLineSet插件,它可以做到这一点,还有一些脚本解释here

eni9jsuy

eni9jsuy3#

:help setting-tabline

Seems to have the relevant information, but I'm not familiar enough with vim scripting to be able to help you get the exact effect you want. Hopefully someone else can pick up from this point.
Also see:

:help statusline

For some info about printing various information, that should be useful.

jutyujz0

jutyujz04#

这是一个根据标签总长度动态调整标签长度的例子。(这是针对控制台版本中的vim)。把它放在你的.vimrc文件的末尾

if exists( '+showtabline' )
    function! MyTabLine()
        let s = ''
        let wn = ''
        let t = tabpagenr()
        let i = 1

        let cnt = 0
        let totalLen = 0
        while i <= tabpagenr( '$' )
          let winnr = tabpagewinnr( i )
          let buflist = tabpagebuflist( i )
          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
        "<Number><Space><file><Space>
          let totalLen = totalLen + 3 + len(file)
          let i = i + 1
        endwhile

        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 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

            "echo totalLen . "/" . &columns . ", file=" . file
            if totalLen > &columns && len(file) > 16
                let file = strpart( file, 0, 6 )  . "~" . strpart( file, len(file)-10 )
            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()
    highlight link TabNum Special
endif
b1payxdu

b1payxdu5#

我发现下面的blog post是最简洁的。
该链接提供了以下函数,该函数应放置在.gvimrc文件中。

function! GuiTabLabel()
    " add the tab number
    let label = '['.tabpagenr()

    " modified since the last save?
    let buflist = tabpagebuflist(v:lnum)
    for bufnr in buflist
        if getbufvar(bufnr, '&modified')
            let label .= '*'
            break
        endif
    endfor

    " count number of open windows in the tab
    let wincount = tabpagewinnr(v:lnum, '$')
    if wincount > 1
        let label .= ', '.wincount
    endif
    let label .= '] '

    " add the file name without path information
    let n = bufname(buflist[tabpagewinnr(v:lnum) - 1])
    let label .= fnamemodify(n, ':t')

    return label
endfunction

set guitablabel=%{GuiTabLabel()}
szqfcxe2

szqfcxe26#

I've written a tabline plugin, mintabline.vim , that lets you configure the maximum length of tab names.
It's as easy as putting this in your vimrc:

Plug 'sangdol/mintabline.vim'
let g:mintabline_tab_max_chars = 15

相关问题