Customising the colours of vim's tab bar
How would one go about modifying the colour of vim's tab bar?
I have tried using
:hi TabLineFill ctermbg=N
which does change the colour of the tab bar's background to the colour that 'N' signifies, but this is all I have manag开发者_StackOverflow中文版ed to glean from Googling. I had a look at :help cterm-colors
, but I did not gain much more of an understanding from it.
Would I be able to use this highlight facility to change the colour of an active tab's foreground and background, and an inactive tab's foreground and background? If so, how would I go about doing this?
You can do the following, for example:
:hi TabLineFill ctermfg=LightGreen ctermbg=DarkGreen
:hi TabLine ctermfg=Blue ctermbg=Yellow
:hi TabLineSel ctermfg=Red ctermbg=Yellow
This line affects the window counter per tab:
:hi Title ctermfg=LightBlue ctermbg=Magenta
Garish colours chosen to highlight what each of the items changes.
I find that looking at $VIMRUNTIME/colors/*.vim
really helps when playing with highlighting.
Note that these change the colours for Vim running in a console. If you want to change the colours for GVim, you need to use guibg
, guifg
, et cetera. For example:
:hi TabLineFill guifg=LightGreen guibg=DarkGreen ctermfg=LightGreen ctermbg=DarkGreen
Yes, you would.
There are three items in the tab line that can be customized to your liking: TabLine, TabLineSel and TabLineFill.
TabLineSel - is the current (so to say) active tab label.
TabLine - are the labels which are not currently active.
TabLineFill - is the remaining of the tabline where there is no labels (background).
You modify all three of them in the usual way.
The 'tabline' option specifies what the line with tab pages labels looks like. It is only used when there is no GUI tab line.
You can use the 'showtabline' option to specify when you want the line with tab page labels to appear: never, when there is more than one tab page or always.
The highlighting of the tab pages line is set with the groups TabLine TabLineSel and TabLineFill. |hl-TabLine| |hl-TabLineSel| |hl-TabLineFill|
A "+" will be shown for a tab page that has a modified window. The number of windows in a tabpage is also shown. Thus "3+" means three windows and one of them has a modified buffer.
The 'tabline' option allows you to define your preferred way to tab pages labels. This isn't easy, thus an example will be given here.
For basics see the 'statusline' option. The same items can be used in the 'tabline' option. Additionally, the |tabpagebuflist()|, |tabpagenr()| and |tabpagewinnr()| functions are useful.
Since the number of tab labels will vary, you need to use an expression for the whole option. Something like: > :set tabline=%!MyTabLine()
Then define the MyTabLine() function to list all the tab pages labels. A convenient method is to split it in two parts: First go over all the tab pages and define labels for them. Then get the label for each tab page. >
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
Now the MyTabLabel() function is called for each tab page to get its label. >
function MyTabLabel(n)
let buflist = tabpagebuflist(a:n)
let winnr = tabpagewinnr(a:n)
return bufname(buflist[winnr - 1])
endfunction
This is just a simplistic example that results in a tab pages line that resembles the default, but without adding a + for a modified buffer or truncating the names. You will want to reduce the width of labels in a clever way when there is not enough room. Check the 'columns' option for the space available.
You can use autocommand groups to make the changes from @Johnsyweb's excellent answer permanent. Place the following in your ~/.vimrc
:
" Highlight the active tab.
augroup TabColors
autocmd!
autocmd ColorScheme *
\ highlight TabLineFill
\ ctermfg=Black
\ ctermbg=NONE
autocmd ColorScheme *
\ highlight TabLine
\ ctermfg=NONE
\ ctermbg=NONE
autocmd ColorScheme *
\ highlight TabLineSel
\ ctermfg=NONE
\ ctermbg=DarkBlue
autocmd ColorScheme *
\ highlight Title
\ ctermfg=NONE
\ ctermbg=NONE
augroup END
You can experiment with changing the colors to whatever you like.
精彩评论