I want to ask if it is possible to define highlighting local to a buffer/window, as well as with any other vim option. So far I've this small group in my .vimrc, to "highlight" the active window. Therefore only the active window gets the cursorline.
augroup CursorLine
autocmd!
autocmd WinEnter * set cursorline
autocmd WinLeave * set nocursorline
augroup END
Now I want to do something similar with the background. A pure black for inactive windows and a really dark grey for the active one. My approach is this:
augroup BackgroundSwitch
autocmd!
autocmd WinEnter * highlight Normal ctermbg=235 ctermfg=15 guibg=#ff0000 guifg=#FFFFFF cterm=NONE gui=NONE
autocmd WinLeave * highlight Normal ctermbg=16 ctermfg=15 guibg=#ff0000 guifg=#FFFFFF cterm=NONE gui=NONE
augroup END
Unfortunately this always make all window backgrounds like the active one. I guess WinLeave is thrown before WinEnter, so it is only rly shortly visible (not for me). An setting a highlight instead of an options, affects always all windows in this Vim instance.
So is there the possibility, that this highlights are only set locally, so I get the wished behaviour?
Thanks!
Changing the highlight is always global and can't be changed only for
a scope.
The Diminactive provide a hack for this. But this plugin lacks in the following points:
The "background" is only changed in lines, which contains text (new line char included).
Background highlights like seach results in inactive windows are invisble (the whole result) and lead to ugly gaps within the text.
Makes the redraw theoretically slower, which sum up with all other plugins u might have run in background to process your buffers (not recognized on my computer).
Related
I am using dracula theme for vim and am not able to get the number pane, that is, the side panel which contains the line numbers, to be displayed in a sort of translucent manner. The preview image shows that it's possible.
How the terminal should look like
(source: draculatheme.com)
How it actually looks
To fix this issue, I think I need to configure some attributes accordingly, but being a beginner, I don't know which ones, therefore any help and guidance would be appreciated.
As a reference, these are my dotvim files.
The background of the line numbers column is set in the colorscheme to NONE for color terminals and #282a36 for GUIs:
hi LineNr ctermfg=60 ctermbg=NONE cterm=NONE guifg=#6272a4 guibg=#282a36 gui=NONE
From there you have three options:
Enable the 'termguicolors' option so that Vim uses the gui* attributes instead of the cterm* attributes.
This is how the screenshot was taken but it will only work in select terminal emulators.
See :help 'termguicolors'.
Edit the colorscheme directly:
hi LineNr ctermfg=60 ctermbg=242 cterm=NONE guifg=#6272a4 guibg=#282a36 gui=NONE
I've chosen 242 arbitrarily but you can choose whatever color you want in this chart
Override your colorscheme in your vimrc:
function! MyHighlights() abort
hi LineNr ctermfg=60 ctermbg=242 cterm=NONE guifg=#6272a4 guibg=#282a36 gui=NONE
endfunction
augroup MyColors
autocmd!
autocmd ColorScheme * call MyHighlights()
augroup END
colorscheme dracula
I would like my quickfix-window to have some highlighting for the current line of the cursor.
After some research I found that I could configure the general appearance of the current line using set cursorline and highlight CursorLine term=bold cterm=bold guibg=Grey40.
Now, I only want that when I'm in the quickfix-window though. So I started to wrap these 2 lines inside a function and called that function with a autocommand:
au QuickFixCmdPre * call EnableSearchHighlighting()
So far, so good. Since I still have the highlighting activated after I searched at least once, I needed to disable the effect again. And here is where I am stuck...
I wrote another function to just set nocursorline and call this one on the QuickFixCmdPost-Event. But for some reason this broke everything. Now I won't get the highlighting anymore, not even in the quickfix-window.
It feels like the Post-Event overrides the Pre-Event.
I'm not sure what else to try here.
Maybe anyone can help me out or even has another approach to the highlighting in the first place?
Here is the full code as it is in my .vimrc right now:
function EnableSearchHighlighting()
set cursorline
highlight CursorLine term=bold cterm=bold guibg=Grey40
endfunction
function DisableSearchHighlighting()
set nocursorline
endfunction
au QuickFixCmdPre * call EnableSearchHighlighting()
au QuickFixCmdPost * call DisableSearchHighlighting()
Thanks for reading. :)
Your approach has many problems, but the main one (and the reason it doesn't work) is that both QuickFixCmdPre and QuickFixCmdPost are run for each quickfix command before you get to switch to the error window.
Add this to a file ftplugin/qf.vim:
setlocal cursorline
Then add the highlight definition to your vimrc, outside any function or autocmd:
highlight CursorLine term=bold cterm=bold guibg=Grey40
From these two questions:
Vim 80 column layout concerns
Vim syntax coloring: How do I highlight long lines only?
I've extracted the following config for my .vimrc:
augroup vimrc_autocmds
autocmd BufEnter * highlight OverLength ctermbg=darkred ctermfg=whitee guibg=#FFD9D9
autocmd BufEnter * match OverLength /\%>80v.\+/
augroup END
This works fine for highlighting lines longer that 80 characters in vim,
but when I open another tab of the same file using:
:tab split
the highlighting doesn't work in the new tab, only in the original one. How can I achieve the same effect for the new tab?
Here is a cleaned up version of your snippet:
highlight OverLength ctermbg=darkred ctermfg=white guibg=#FFD9D9
augroup vimrc_autocmds
autocmd!
autocmd BufEnter,WinEnter * call matchadd('OverLength', '\%>80v.\+', -1)
augroup END
The autocommands in that group are properly cleared when/if you reload your vimrc.
The BufEnter event is only triggered once, you need to listen to another event, WinEnter, which is triggered when a window gets the focus.
matchadd() is more flexible than :match and allows you to set the priority of the highlighting (useful if you rely on hlsearch).
Imagine I'm coding, and I have different split panes open. What settings should I pass into vimrc to change the background color as I switch from one buffer/pane to another?
I have tried:
autocmd BufEnter * highlight Normal ctermbg=black
autocmd BufLeave * highlight Normal ctermbg=white
I would like to add that I am sure that I've got 256 colors enabled
Actually, there is a way to get this effect. See the answer by #blueyed to this related question: vim - dim inactive split panes. He provides the script below and when placed in my .vimrc it does dim the background of inactive windows. In effect, it makes their background the same colour specified for the colorcolumn (the vertical line indicating your desired text width).
" Dim inactive windows using 'colorcolumn' setting
" This tends to slow down redrawing, but is very useful.
" Based on https://groups.google.com/d/msg/vim_use/IJU-Vk-QLJE/xz4hjPjCRBUJ
" XXX: this will only work with lines containing text (i.e. not '~')
" from
if exists('+colorcolumn')
function! s:DimInactiveWindows()
for i in range(1, tabpagewinnr(tabpagenr(), '$'))
let l:range = ""
if i != winnr()
if &wrap
" HACK: when wrapping lines is enabled, we use the maximum number
" of columns getting highlighted. This might get calculated by
" looking for the longest visible line and using a multiple of
" winwidth().
let l:width=256 " max
else
let l:width=winwidth(i)
endif
let l:range = join(range(1, l:width), ',')
endif
call setwinvar(i, '&colorcolumn', l:range)
endfor
endfunction
augroup DimInactiveWindows
au!
au WinEnter * call s:DimInactiveWindows()
au WinEnter * set cursorline
au WinLeave * set nocursorline
augroup END
endif
You can't. The :highlight groups are global; i.e. when you have multiple window :splits, all window backgrounds will be colored by the same Normal highlight group.
The only differentiation between active and non-active windows is the (blinking) cursor and the differently highlighted status line (i.e. StatusLine vs. StatusLineNC). (You can add other differences, e.g. by only turning on 'cursorline' in the current buffer (see my CursorLineCurrentWindow plugin.))
One of the design goals of Vim is to work equally well in a primitive, low-color console as in the GUI GVIM. When you have only 16 colors available, a distinction by background color is likely to clash with the syntax highlighting. I guess that is the reason why Vim hasn't and will not have this functionality.
Basically what Kent said above will work -- surprisingly well at that. The only limitation is that you can only dim the foreground color. Copy this into vimrc, and evoke :call ToggleDimInactiveWin().
let opt_DimInactiveWin=0
hi Inactive ctermfg=235
fun! ToggleDimInactiveWin()
if g:opt_DimInactiveWin
autocmd! DimWindows
windo syntax clear Inactive
else
windo syntax region Inactive start='^' end='$'
syntax clear Inactive
augroup DimWindows
autocmd BufEnter * syntax clear Inactive
autocmd BufLeave * syntax region Inactive start='^' end='$'
augroup end
en
let g:opt_DimInactiveWin=!g:opt_DimInactiveWin
endfun
A few things I had to look up in writing this:
(1) windo conveniently executes a command across all windows
(2) augroup defines autocommand groups, which can be cleared with autocmd! group
For Neovim users, there is the winhighlight option. The help file provides the following example:
Example: show a different color for non-current windows:
set winhighlight=Normal:MyNormal,NormalNC:MyNormalNC
Personally, I use my statusline to let me know this. I use the WinEnter and WinLeave autocmds to switch to an inactive status line (grayed out) when leaving and an active statusline (bright colors) when entering. The split panes you mention are windows in vim. This also works because :help statusline tells us that its setting is global or local to a window, so you can use :setlocal statusline=... or let &l:statusline=... to only apply to the current window.
Your method won't work because a) BufEnter and BufLeave aren't necessarily the events that you want and b) highlight groups are global, so changing Normal's definition changes it for every window.
I've just started using Vim and I find myself being confused on which mode I'm currently in. I was wondering if it's possible to change the line number color (which I have displayed by default) in the different modes (command, visual, and insert) so that I can easily identify it.
Vim has an option called showmode that will put a message on the last line if you are not in normal mode. See :help 'showmode' for more info.
If you really want to change the background of the number column while in insert mode, you could do something like the following:
autocmd InsertEnter * highlight LineNr ctermbg=red guibg=red
autocmd InsertLeave * highlight LineNr ctermbg=black guibg=black
Note that this method will break if you are bad and use C-c to exit insert mode instead of <Esc> or <C-[.
Relevant :help tags: autocmd, autocmd-events, highlight, highlight-groups