Is it possible to change tab header color each filetype in vim?

女生的网名这么多〃 提交于 2019-12-20 06:36:32

问题


If I'm opening .js, .html, .rb and other filetype, is it possible to change tab color each filetype? Tab means vim's tab not like space.


回答1:


Use the format %#String# will color with the string hightlight :
- set tabline=%#String#\ toto

A script to put in your vimrc:

function! TabTest()
  let res = ''

  for i in range(tabpagenr('$'))
    let i += 1
    " Get open buffer
    let i_window = tabpagewinnr(i)
      let l_buffer = tabpagebuflist(i)
    let i_buffer = l_buffer[i_window - 1]

    " Get type
    let s_type = getbufvar(i_buffer, '&filetype')

    " Set color according to filetype
    let s_color = ''
    if i == tabpagenr()
      let res .= '%#TabLine#'
    elseif 'javascript' == s_type
      let res .= '%#String#'
    elseif 'html' == s_type
      let res .= '%#Comment#'
    else 
      let res .= '%#Normal#'
    endif

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

    " Set label text
      let s_buffer = bufname(i_buffer)
    try
      let s_file = split(s_buffer, '/')[-1]
    catch
      let s_file = '[No Name]'
    endtry
      let res .= ' ' . s_file
  endfor

  return res
endfunction

set tabline=%!TabTest()

As you are asking for an color hightlight according to the filetype, you must get the filetype of a buffer:
- let s_type = getbufvar(i_buffer, '&filetype')

For this you must get the buffer number of the activer buffer in the tab:
- let i_window = tabpagewinnr(i)
- let l_buffer = tabpagebuflist(i)
- let i_buffer = l_buffer[i_window - 1]

Then you must wisely distinguish accroding to the filetype with a if. So you definitely want to hide all this in a function:
set tabline=%!TabTest()

More :
h tabline
h statusline




回答2:


You will have to write your own :help 'tabline' (see also :help 'statusline') for that… and all its supporting code.

Having different colors is the easy part:

:set tabline=%#Error#\ foo.js\ %*%#DiffAdd#\ bar.rb\ %*%#Search#\ baz.scss\ %*

It's the code that will determine the correct tabline value on various events that won't be trivial at all.



来源:https://stackoverflow.com/questions/51664336/is-it-possible-to-change-tab-header-color-each-filetype-in-vim

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!