How to color specific Vim tabs [duplicate] - vim

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.

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

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.

Related

Get "usable" window width in vim script

How do I get the width of 3 (marked with green color in the image) in vim script?
If there is no signs column, and there are no other "special columns", I can get it with
winwidth(0) - (max([len(line('$')), &numberwidth-1]) + 1)
I think, you should be able to get that width using:
:set virtualedit=all
:norm! g$
:echo virtcol('.')
Alternatively, you could check, whether a signcolumn is present (e.g. using redir)
:redir =>a |exe "sil sign place buffer=".bufnr('')|redir end
:let signlist=split(a, '\n')
:let width=winwidth(0) - ((&number||&relativenumber) ? &numberwidth : 0) - &foldcolumn - (len(signlist) > 1 ? 2 : 0)
My ingo-library plugin has a ingo#window#dimensions#NetWindowWidth() function for that.
Answering because I can't comment yet:
Christian's answer gives the wrong result in the case that the actual number of lines in the file exceeds &numberwidth (because &numberwidth is just a minimum, as kshenoy pointed out). The fix is pretty simple, though, just take the max() of &numberwidth and the number of digits in the last line in the buffer (plus one to account for the padding vim adds):
redir =>a | exe "silent sign place buffer=".bufnr('') | redir end
let signlist = split(a, '\n')
let lineno_cols = max([&numberwidth, strlen(line('$')) + 1])
return winwidth(0)
\ - &foldcolumn
\ - ((&number || &relativenumber) ? lineno_cols : 0)
\ - (len(signlist) > 2 ? 2 : 0)
Kale's answer corrected one corner case where the number of lines is exceeding what &numberwidth can display. Here I fix another corner case where the signcolumn option is not set to auto
function! BufWidth()
let width = winwidth(0)
let numberwidth = max([&numberwidth, strlen(line('$'))+1])
let numwidth = (&number || &relativenumber)? numberwidth : 0
let foldwidth = &foldcolumn
if &signcolumn == 'yes'
let signwidth = 2
elseif &signcolumn == 'auto'
let signs = execute(printf('sign place buffer=%d', bufnr('')))
let signs = split(signs, "\n")
let signwidth = len(signs)>2? 2: 0
else
let signwidth = 0
endif
return width - numwidth - foldwidth - signwidth
endfunction
None of the above answers take into account the following points -
Plugins use sign-groups (if available), so simply running exe "silent sign place buffer=".bufnr('') does not show the sign's placed in the plugin's group
Neovim has support for variable signcolumn width
So this is the answer that finally set the ball rolling for me. Of course it is influenced by all of the above answers -
function! BufferWidth()
let width = winwidth(0)
let numberwidth = max([&numberwidth, strlen(line('$')) + 1])
let numwidth = (&number || &relativenumber) ? numberwidth : 0
let foldwidth = &foldcolumn
if &signcolumn == 'yes'
let signwidth = 2
elseif &signcolumn =~ 'yes'
let signwidth = &signcolumn
let signwidth = split(signwidth, ':')[1]
let signwidth *= 2 " each signcolumn is 2-char wide
elseif &signcolumn == 'auto'
let supports_sign_groups = has('nvim-0.4.2') || has('patch-8.1.614')
let signlist = execute(printf('sign place ' . (supports_sign_groups ? 'group=* ' : '') . 'buffer=%d', bufnr('')))
let signlist = split(signlist, "\n")
let signwidth = len(signlist) > 2 ? 2 : 0
elseif &signcolumn =~ 'auto'
let signwidth = 0
if len(sign_getplaced(bufnr(),{'group':'*'})[0].signs)
let signwidth = 0
for l:sign in sign_getplaced(bufnr(),{'group':'*'})[0].signs
let lnum = l:sign.lnum
let signs = len(sign_getplaced(bufnr(),{'group':'*', 'lnum':lnum})[0].signs)
let signwidth = (signs > signwidth ? signs : signwidth)
endfor
endif
let signwidth *= 2 " each signcolumn is 2-char wide
else
let signwidth = 0
endif
return width - numwidth - foldwidth - signwidth
endfunction
Starting from Vim 8.2.3627, getwininfo()'s output has a textoff containing the
number of columns occupied by any 'foldcolumn', 'signcolumn' and line number in front of the text
therefore subtracting that to the width entry, e.g. computing
getwininfo(win_getid()).width - getwininfo(win_getid()).textoff
should give the desired result.
Before textoff was available, it seems to me that the followinig computation does cut it:
let textoff = max([&numberwidth, (&number ? len(line('$')) + 1 : (&relativenumber ? winfo.height + 1 : 0))])
\ + &foldcolumn
\ + (empty(sign_getplaced(bufname(), {'group': '*'})[0].signs) ? 0 : 2)
I made use of both solutions in this plugin of mine for showing soft-wrapped lines.

Is there any way to make VIM fold one line more?

I am using an adaptation of the VIM folding settings published here and here. Now, notice the program below:
When folded on my VIM, it produces the following result:
I don't really like that look, I'd prefer the following:
Which I got by rewriting the code:
But leaving the code like that is horrible. Is it possible to update the VimScript so I get the folding I want, without having to leave the JavaScript code like that?
Here is my exact setting:
setlocal foldmethod=expr
setlocal foldexpr=GetPotionFold(v:lnum)
setlocal foldminlines=0
function! s:NextNonBlankLine(lnum)
let numlines = line('$')
let current = a:lnum + 1
while current <= numlines
if getline(current) =~? '\v\S'
return current
endif
let current += 1
endwhile
return -2
endfunction
function! s:IndentLevel(lnum)
return indent(a:lnum) / &shiftwidth
endfunction
function! GetPotionFold(lnum)
if getline(a:lnum) =~? '\v^\s*$'
return '-1'
endif
let this_indent = <SID>IndentLevel(a:lnum)
let next_indent = <SID>IndentLevel(<SID>NextNonBlankLine(a:lnum))
if next_indent == this_indent
return this_indent
elseif next_indent < this_indent
return this_indent
elseif next_indent > this_indent
return '>' . next_indent
endif
endfunction
function! NeatFoldText()
let line = getline(v:foldstart)
let lines_count = v:foldend - v:foldstart + 1
let lines_count_text = '| ' . printf("%10s", lines_count . ' lines') . ' |'
let foldchar = ' '
let foldtextstart = strpart(line, 0, (winwidth(0)*2)/3)
let foldtextend = lines_count_text . repeat(foldchar, 6)
let foldtextlength = strlen(substitute(foldtextstart . foldtextend, '.', 'x', 'g')) + &foldcolumn
return foldtextstart . repeat(foldchar, winwidth(0)-foldtextlength) . foldtextend
endfunction
set foldtext=NeatFoldText()
hi Folded ctermbg=255 ctermfg=21
hi FoldColumn ctermbg=white ctermfg=darkred
And here is the sample code:
function foo(x){
var y = x*x;
var z = y+y;
return z;
};
function bar(x){
var y = x*x;
var z = y+y;
return z;
};
function foobar(x){
var y = x*x;
var z = y+y;
return z;
};
function barfoo(x){
var y = x*x;
var z = y+y;
return z;
};
set foldmethod=marker
set foldmarker={,}
Should give you what you want.
You are using a fold expression that is (as far as I can see from a glance) designed for indent folding (used for HAML, Python, Haskell and other indent-syntax languages).
For C, you should just be using :set foldmethod=syntax.

VIM: how to show folder name in tab, but only if two files have the same name

I would like to have the following feature in VIM (GVIM in particular). I think Sublime Text has something like that:
In the "normal" case the tab name should be just the file's name, but...
If there are two files opened with the same name but in different directories, I would like to see a tab name parent folder name + file name.
Example:
When there are tabs for the following files:
c:\my\dir\with\files\justAfile.txt
c:\my\dir\with\files\myfile.txt
c:\my\dir\with\backup\myfile.txt
Tab names would be then:
justAfile.txt | files\myfile.txt | backup\myfile.txt
Is this doable with some clever configuration?
In GVIM, you can customize the tab labels with the 'guitablabel' option.
In terminal Vim; there's no 'guitablabel' equivalent; one has to render the entire 'tabline'. Fortunately, the Vim help has an example which delegates the label rendering to a separate function, so re-using your custom function is pretty easy.
The help pages for the mentioned options link to examples; you probably have to use fnamemodify() to canonicalize all buffers' paths to full absolute paths, find the common base directory, and then strip that off the paths.
On the other hand, if it's okay for you to :cd to the base directory, you'll get that kind of tab label pretty much out-of-the-box.
Assuming the following files:
z.txt
a/b/c/d.txt
a/b/f/d.txt
My current setup will make the tabline look like so (I reversed-engineered the behavior from Sublime Text 2):
z.txt | d.txt - c | d.txt - f
My code has a lot of extras like treating Nerdtree/FZF tabs specially, and naming tabs according to the left-most buffer when there are splits. You can remove these extras yourself if you don't want them, or change anything you don't like. I also assumed Unix only, and terminal VIM only (GVIM would need minor tweaking I guess).
I am providing the code below without guarantee, as a starting point for you to customize according to your needs.
set tabline=%!GetTabLine()
function! GetTabLine()
let tabs = BuildTabs()
let line = ''
for i in range(len(tabs))
let line .= (i+1 == tabpagenr()) ? '%#TabLineSel#' : '%#TabLine#'
let line .= '%' . (i + 1) . 'T'
let line .= ' ' . tabs[i].uniq_name . ' '
endfor
let line .= '%#TabLineFill#%T'
return line
endfunction
function! BuildTabs()
let tabs = []
for i in range(tabpagenr('$'))
let tabnum = i + 1
let buflist = tabpagebuflist(tabnum)
let file_path = ''
let tab_name = bufname(buflist[0])
if tab_name =~ 'NERD_tree' && len(buflist) > 1
let tab_name = bufname(buflist[1])
end
let is_custom_name = 0
if tab_name == ''
let tab_name = '[No Name]'
let is_custom_name = 1
elseif tab_name =~ 'fzf'
let tab_name = 'FZF'
let is_custom_name = 1
else
let file_path = fnamemodify(tab_name, ':p')
let tab_name = fnamemodify(tab_name, ':p:t')
end
let tab = {
\ 'name': tab_name,
\ 'uniq_name': tab_name,
\ 'file_path': file_path,
\ 'is_custom_name': is_custom_name
\ }
call add(tabs, tab)
endfor
call CalculateTabUniqueNames(tabs)
return tabs
endfunction
function! CalculateTabUniqueNames(tabs)
for tab in a:tabs
if tab.is_custom_name | continue | endif
let tab_common_path = ''
for other_tab in a:tabs
if tab.name != other_tab.name || tab.file_path == other_tab.file_path
\ || other_tab.is_custom_name
continue
endif
let common_path = GetCommonPath(tab.file_path, other_tab.file_path)
if tab_common_path == '' || len(common_path) < len(tab_common_path)
let tab_common_path = common_path
endif
endfor
if tab_common_path == '' | continue | endif
let common_path_has_immediate_child = 0
for other_tab in a:tabs
if tab.name == other_tab.name && !other_tab.is_custom_name
\ && tab_common_path == fnamemodify(other_tab.file_path, ':h')
let common_path_has_immediate_child = 1
break
endif
endfor
if common_path_has_immediate_child
let tab_common_path = fnamemodify(common_path, ':h')
endif
let path = tab.file_path[len(tab_common_path)+1:-1]
let path = fnamemodify(path, ':~:.:h')
let dirs = split(path, '/', 1)
if len(dirs) >= 5
let path = dirs[0] . '/.../' . dirs[-1]
endif
let tab.uniq_name = tab.name . ' - ' . path
endfor
endfunction
function! GetCommonPath(path1, path2)
let dirs1 = split(a:path1, '/', 1)
let dirs2 = split(a:path2, '/', 1)
let i_different = 0
for i in range(len(dirs1))
if get(dirs1, i) != get(dirs2, i)
let i_different = i
break
endif
endfor
return join(dirs1[0:i_different-1], '/')
endfunction
As Ingo suggests you can use guitablabel. On my installation its only configured to show the file name (:echo &guitablabel reports %M%t). To set this to show the relative path do :set guitablabel=%M%f. Like Ingo says, use :cd DIRECTORY to set the home directory, and :pwd to see where its currently set.
See :help statusline for (many) more formatting options.
Here's my solution that makes the tabname the directory---which is usually a good proxy for the project that tab is meant to represent. This solution can be modified to show the filename if there is only one buffer (modification shown below).
This solution draws a tiny bit from Jerome's. I'm not doing anything as complex as they are, so mine is 5x shorter.
Also, this solution places the tab number alongside the name, making it easy to bounce around, meaning the tabs will look like this: 1:log 2:doc 3:vimfiles and 2gt will move to the second tab.
set tabline=%!TabLine()
function! TabLine()
let line = ''
for i in range(tabpagenr('$'))
let line .= (i+1 == tabpagenr()) ? '%#TabLineSel#' : '%#TabLine#'
let line .= '%' . (i + 1) . 'T'
let line .= TabLabel(i + 1) . ' '
endfor
let line .= '%#TabLineFill#%T'
return line
endfunction
function! TabLabel(n)
" Return list of buffer numbers for each window pane open in tab.
let panelist = tabpagebuflist(a:n)
" See :help setting-tabline then search MyTabLabel if you want to
" use use the active window. I use the topmost pane, which let's
" me rename the tab just by putting a window from a different
" directory in the first position.
let filepath = bufname(panelist[0])
let dirname = fnamemodify(filepath, ':p:h:t')
return a:n . ':' . dirname
endfunction
The modification to show the filename if only one buffer is visible:
function! TabLabel(n)
" Return list of buffer numbers for each window pane open in tab.
let panelist = tabpagebuflist(a:n)
" See :help setting-tabline then search MyTabLabel if you want to
" use use the active window. I use the topmost pane, which let's
" me rename the tab just by putting a window from a different
" directory in the first position.
let filepath = bufname(panelist[0])
let dirname = fnamemodify(filepath, ':p:h:t')
let filename = fnamemodify(filepath, ':t')
let tabname = len(panelist) > 1 ? dirname : filename
return a:n . ':' . tabname
endfunction

Change omnicomplete on the fly using supertab

I use the supertab plugin in vim.
These are my default settings (in _vimrc)
let g:SuperTabDefaultCompletionType = '<c-x><c-k>' -->(dictionary)
let g:SuperTabRetainCompletionDuration = "completion"
let g:SuperTabLongestEnhanced = 1
let g:SuperTabLongestHighlight = 1
I created this script to select omnicomplete with supertab on the fly:
function! SuperTabFunction()
if !exists("WhatSuperTab")
let WhatSuperTab = "SuperTab function?"
endif
if !exists("MenuSuperTab_choices")
let MenuSuperTab_choices = "&Current page\n&Spellchecker\nSentence\nCode"
endif
let n = confirm(WhatSuperTab, MenuSuperTab_choices, "Question")
if n == 1
let g:SuperTabDefaultCompletionType = '<c-x><c-m>'
so $VIM/vimfiles/plugin/supertab.vim
elseif n == 2
let g:SuperTabDefaultCompletionType = '<c-x><c-k>'
so $VIM/vimfiles/plugin/supertab.vim
elseif n == 3
let g:SuperTabDefaultCompletionType = '<c-x><c-l>'
so $VIM/vimfiles/plugin/supertab.vim
elseif n == 4
let g:SuperTabDefaultCompletionType = '<c-x><c-o>'
so $VIM/vimfiles/plugin/supertab.vim
else
return ''
endif
endfun
nmap <silent> <C-S-tab> :call SuperTabFunction()<CR>
imap <silent> <C-S-tab> <esc>:call SuperTabFunction()<CR>a
When I invoke above function and choose p.e. "Sentence"
I can use super tab to complete sentences
When I invoke above function again and choose p.e. "Spellchecker"
The correct value is assigned to g:SuperTabDefaultCompletionType
but it still replaces sentences.
What did I wrong in this function?
Have you tried calling the provided function instead of altering the global variable directly?
" SuperTabSetDefaultCompletionType(type) {{{
" Globally available function that users can use to set the default
" completion type for the current buffer, like in an ftplugin.
function! SuperTabSetDefaultCompletionType(type)

is there a way on vim to show all column numbers in the current buffer line?

It would be very nice to have an option that would show all the column numbers of the current line or maybe of all the buffer, so I could know where exactly to navigate. Is there such an option or do i have to program it myself (nooo XD)?
:h 'statusline'
It is as easy as defining exactly what you what to see printed. e.g.
" RulerStr() comes from http://www.vanhemert.co.uk/vim/vimacros/ruler2.vim
function! RulerStr()
let columns = &columns
let inc = 0
let str = ""
while (inc < columns)
let inc10 = inc / 10 + 1
let buffer = "."
if (inc10 > 9)
let buffer = ""
endif
let str .= "....+..." . buffer . inc10
let inc += 10
endwhile
let str = strpart(str, 0, columns)
return str
endfunction
let s:saved_stl = {}
function! s:ToggleRuler()
let buf = bufnr('%')
if has_key(s:saved_stl, buf)
let &l:stl = s:saved_stl[buf]
unlet s:saved_stl[buf]
else
let s:saved_stl[buf] = &l:stl
setlocal stl=%{RulerStr()}
endif
endfunction
nnoremap <silent> µ :call <sid>ToggleRuler()<cr>
You can use "set ruler". It will show the line number and column position at the bottom.

Resources