Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to enumerate tabs in vim?

Tags:

vim

tabs

Vim is very productive editor and I enjoy using it everyday, but I've found that moving between tabs takes more time than it should.

When I want to switch to another tab I often repeat gt or gT multiple times. Vim provides a better way to reach required tab - n + gt, where n is tab number. But to use it you should count tab number first. It quickly become boring if you open a dozen of tabs.

I think it would be nice to enumerate tabs. A single number on each tab in front of file name, something like this:

1 Readme | 2 main.c | 3 main.h | 4 process.h

I hope it is possible to configure vim to do this by editing config or using some plugin.

Is there a way to achieve it?

like image 376
lambdas Avatar asked Jul 06 '12 16:07

lambdas


1 Answers

You can use the tabline option for setting the label of the tabs in console mode of vim. See the help at :h setting-tabline which also shows a very basic minimal example, which you can tweak to your need, e.g. for what you want, I would use something like this:

fu! MyTabLabel(n)
let buflist = tabpagebuflist(a:n)
let winnr = tabpagewinnr(a:n)
let string = fnamemodify(bufname(buflist[winnr - 1]), ':t')
return empty(string) ? '[unnamed]' : string
endfu

fu! 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'
    " display tabnumber (for use with <count>gt, etc)
    let s .= ' '. (i+1) . ' ' 

    " the label is made by MyTabLabel()
    let s .= ' %{MyTabLabel(' . (i + 1) . ')} '

    if i+1 < tabpagenr('$')
        let s .= ' |'
    endif
endfor
return s
endfu
set tabline=%!MyTabLine()
like image 122
Christian Brabandt Avatar answered Oct 19 '22 08:10

Christian Brabandt