在Tab中设置文件名

时间:2011-08-30 00:11:35

标签: vim

当我使用Vim打开多个文件时,有人能告诉我如何在选项卡中显示文件名吗? 在选项卡上有一个名称可以更容易地更改为不同的文件。

1 个答案:

答案 0 :(得分:6)

我认为你的问题是“你如何仅显示标签上的文件名”。如果这是问题,我的答案是:

在gui vim中,你会使用:

:set guitablabel=%t

然而,如果在vim中,它会变得更复杂一些。您必须使用:tabline覆盖整行。我修改了:help setting-tabline中提供的示例,以添加您想要的行为。您需要将以下代码添加到vimrc:

set tabline=%!MyTabLine()

function MyTabLine()
  let s = ''
  for i in range(tabpagenr('$'))
    " select the highlighting
    if i + 1 == tabpagenr()
      let s .= '%#TabLineSel#'
    else
      let s .= '%#TabLine#'
    endif

    " set the tab page number (for mouse clicks)
    let s .= '%' . (i + 1) . 'T' 

    " the label is made by MyTabLabel()
    let s .= ' %{MyTabLabel(' . (i + 1) . ')} '
  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#%999Xclose'
  endif

  return s
endfunction

function MyTabLabel(n)
  let buflist = tabpagebuflist(a:n)
  let winnr = tabpagewinnr(a:n)
  let label =  bufname(buflist[winnr - 1]) 
  return fnamemodify(label, ":t") 
endfunction

我希望这有帮助!