📜 ⬆️ ⬇️

Assigning Tab Headers to Vim

Vim provides a fairly convenient way to group editable files - tabs (tabs). And everything would be fine, but personally I was strongly hampered by one drawback - the tab names do not contain their numbers, and therefore when switching to the right tab with the command: tabn [tab_num], each time you have to look at all tabs and calculate the ordinal number of this tab. With frequent switching it really starts to bother ...

The other day, while reading the Vim documentation, I was surprised to find that it provides the ability to assign its own functions that will generate names for each tab. There was also a simple example of how this can be done. Having run over eyes according to the manual about writing scripts for Vim, I modified the given example, having received the following result (for text and GUI display mode):


Here is the code you need to insert in your ~ / .vimrc to get the same result:

"We set our own functions for naming tabs headers ->
function MyTabLine ()
let tabline = ""
')
"We form a tabline for each tab ->
for i in range ( tabpagenr ( '$' ))
"Highlight the title of the currently selected tab.
if i + 1 == tabpagenr ()
let tabline . = '% # TabLineSel #'
else
let tabline . = '% # TabLine #'
endif

"Set the tab number
let tabline . = '%' . ( i + 1 ) . 'T'

"Get the tab name
let tabline . = '% {MyTabLabel (' . ( i + 1 ) . ')} |'
endfor
"We form a tabline for each tab <-

"Filling the extra space
let tabline . = '% # TabLineFill #% T'

"Right Aligned Tab Close Button
if tabpagenr ( '$' ) > 1
let tabline . = '% =% # TabLine #% 999XX'
endif

return tabline
endfunction

function MyTabLabel ( n )
let label = ''
let buflist = tabpagebuflist ( a: n )

"Filename and tab number ->
let label = substitute ( bufname ( buflist [tabpagewinnr ( a: n ) - 1 ] ) , '. * /' , '' , '' )

if label == ''
let label = '[No Name]'
endif

let label . = '(' . a: n . ')'
"Filename and tab number <-

"Determine if there is at least one in the tab
"modified buffer.
"->
for i in range ( len ( buflist ))
if getbufvar ( buflist [i], "& modified" )
let label = '[+]' . label
break
endif
endfor
"<-

return label
endfunction

function MyGuiTabLabel ()
return '% {MyTabLabel (' . tabpagenr () . ')}'
endfunction

set tabline =%! MyTabLine ()
set guitablabel =%! MyGuiTabLabel ()
"We set our own functions to assign names to tab headers <-

By the way, quite an interesting language is used in Vim for writing scripts - very similar to Python and just as simple and easy to use - to quickly figure it out if necessary will not be any difficulty.

Source: Crossposting from my blog - konishchevdmitry.blogspot.com .

Source: https://habr.com/ru/post/29373/


All Articles