I have a the following in my vimrc to highlight any lines that go past 80 columns:
highlight ColorColumn ctermfg=red ctermbg=bg
call matchadd('ColorColumn', '\%81v.\+', 100)
It works great in most cases. However, I've noticed that if i open a file in a new tab, it doesn't work at all. I'm able to fix this by :source $MYVIMRC. But the issue is, when I source my vimrc I lose my indentLines plugin. I've done a little testing, and I have found that the indentLines goes away anytime the vimrc is sourced in an open instance of vim. Still, I am unable to determine why the 2 lines shown above are not being called when I open a file in a new tab. Any ideas?
my vimrc
The matchadd() only affects the current window. In order to have it on all windows you can add the following to your .vimrc:
if exists("*matchadd")
augroup colorColumn
au!
au BufEnter * call matchadd('ColorColumn', '\%81v.\+', 100)
augroup END
endif
Edit: As pointed by Ingo on the comments, the BufEnter will trigger many times when it is not necessary. The lines below correct this issue:
if exists("*matchadd")
augroup colorColumn
au!
au VimEnter,WinEnter * call matchadd('ColorColumn', '\%81v.\+', 100)
augroup END
endif
Related
The vim plugin vdebug opens several splits when it starts the debugger. One of those is DebuggerWatch at the top right. I read somewhere I can set an autocmd to listen for that buffer opening and take action. In this case I want to run :resize 100 to maximize the height. I know how to set an autocmd by filetype or by file glob, but not by a buffer name that is not a file. How do I write an autocmd for this?
I was able to resize on entering the buffer like this:
augroup vdebugwatchpanellarger
autocmd!
autocmd BufEnter DebuggerWatch resize 999
autocmd BufEnter DebuggerStack resize 999
augroup END
This is helpful because at least if I navigate to one of these buffers it will resize them. But I haven't been able to get it resizing on loading the buffer yet. I tried BufRead, BufNew, BufAdd instead of BufEnter. No effect.
The plugin sets the :help 'syntax' option of the "DebuggerWatch" window to debugger_watch so using the :help Syntax event should work:
augroup vdebugwatchpanellarger
autocmd!
autocmd Syntax debugger_watch resize 999
augroup END
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.
So I found this solution on StackOverflow here: Vim 80 column layout concerns
If I type a long line in my file, I would like the characters that exceed a limit of 80 characters to be highlighted. A lot of people seem to think that this solution works fine, but I have it in my vimrc file and it behaves as though nothing has changed at all. My long lines do not get highlighted.
highlight OverLength ctermbg=red ctermfg=white guibg=#592929
match OverLength /\%81v.\+/
For reference, here is my entire .vimrc, which isn't that long:
" You'll need to add the following to your ~/.vimrc so that pathogen will be loaded
" properly. Filetype detection must be off when you run the commands so its best to
" You'll need to add the following to your ~/.vimrc so that pathogen will be loaded
" execute them first:
"filetype off
call pathogen#runtime_append_all_bundles()
call pathogen#helptags()
"filetype on
syntax on
let mapleader = ","
let g:CommandTMaxHeight=25
imap ii <Esc>
map <S-Enter> O<Esc>
map <CR> o<Esc>
set guioptions-=T
set guioptions-=r
set hlsearch
highlight OverLength ctermbg=red ctermfg=white guibg=#592929
match OverLength /\%79v.\+/
set nocompatible
set ruler
set number
set shellcmdflag=-ic
set list
set expandtab
set tabstop=4
set softtabstop=4
nmap <C-k> ddkP
nmap <C-j> ddp
vmap <C-k> xkP`[V`]
vmap <C-j> xp`[V`]
au! BufWritePost vimrc source %
colorscheme vividchalk
From: https://stackoverflow.com/a/10993757/1701170
augroup vimrc_autocmds
autocmd BufEnter * highlight OverLength ctermbg=darkgrey guibg=#111111
autocmd BufEnter * match OverLength /\%75v.*/
augroup END
This works for me whereas the solution I had before does not. I'm not exactly sure why, but another commenter on the page offers a hint when he remarks, regarding the solution without opening and closing augroup lines, "This only works for the first file you open in any given buffer".
Now if someone could explain why the additional opening and closing lines solve that first-file-in-any-given-buffer problem, and why that problem exists in the first place, then I would feel enlightened.
The following seems to work for me:
highlight OverLength ctermbg=darkred ctermfg=white guibg=#FFD9D9
match OverLength /\%79v.*/
it's bascially the same thing that you have with just \+ changed to * and a different color. For some reason though changing your line didn't work for me. Perhaps there's something wrong with one of the characters?
Anyway - copied from this post: https://stackoverflow.com/a/395326/680238
I think it's the one I used when working on my vimrc, but as a side note I might add I've dropped the idea of highlighting those overlength lines and settled for having vertical line on 81st column. You may want to try this and see which one you like better:
" Highlight first oversize line
set colorcolumn=81
In my opinion it's much cleaner to use textwidth and colorcolumn. Look them up in the help for specifics on their function.
set textwidth=80
set colorcolumn=+1
This only highlights the point at which 80 columns is exceeded so may not meet your goals but in my opinion makes things easier to read if you're often loading up files from sources that do not obey your restrictions.
The highlighting is handled by the ColorColumn highlight group.
You can also just set colorcolumn to 81 and don't need to set textwidth but I use textwidth for a few other things also so have it set anyway, and this allows me to change textwidth and get the coloring at the max width regardless.
I know this is a super old post, but I recently found my OverLength highlighting stopped working (Vim 8.1). my highlight code was originally:
augroup vimrc_autocmds
autocmd BufEnter * highlight OverLength ctermbg=darkred ctermfg=white guibg=#FFD9D9
autocmd filetype BufEnter * match OverLength /\%>79v.\+/
augroup END
I removed filetype from the 3rd line and it started working again. I don't know which version changed this behaviour, but having filetype in there definitely worked for me previously.
This is pretty much the answer from Jonomono, just clarifying in case anyone else runs into a similar issue :)
I have set cursorline in my vimrc. Is there any way to make vim highlight only the current line in the active buffer instead of all buffers?
The problem with the BufEnter / BufLeave hooks used in Zsolt's answer is that they don't fire when moving to an adjacent window that displays the same buffer. I have used the following successfully:
augroup CursorLine
au!
au VimEnter,WinEnter,BufWinEnter * setlocal cursorline
au WinLeave * setlocal nocursorline
augroup END
You might try:
au BufEnter * setlocal cursorline
au BufLeave * setlocal nocursorline
To Zsolt's answer I would add:
au WinLeave * setLocal nocursorline
This improves the behavior when moving between two windows on the same buffer.
The weakness of all the short :autocmd solutions here is that you cannot make exceptions like disabling the cursorline for a particular window or making it permanent for (another) window. Any change of 'cursorline' will be overwritten by the next move to another window.
My CursorLineCurrentWindow plugin handles these exceptions through a more elaborate logic.