I have this in my .vimrc:
highlight ExtraWhitespace ctermbg=red guibg=red
match ExtraWhitespace /^\t*\zs \+/
However this config only works for the first tab which is opened at startup.
If I do tabnew another_file the leading whitespaces are not hilited in red.
How do I make vim to set these hiliting rules also upon creation of a new tab? I have tried autocmd on TabEnter event or BufNew event, but seems that my autocommands are ignored.
I have tried autocmd on TabEnter event
The tabs do not have matches or highlights, the windows do. So your event is, obviously, called WinNew and not TabNew.
Another problem is that your highlight will be lost after ColorScheme change. So, unless you're also going to catch autocmd ColorScheme and to harmonize your custom group with a new color scheme, whatever it is, you're advised to use one of the standard groups, for instance, ErrorMsg.
Related
I am trying to change the spellcheck highlight group. So in the end of my .vimrc
I add the following code
highlight clear SpellBad
highlight SpellBad cterm=underline
when I open a new file, it is still showing the old syntax highlighting. But if I run the same commands inside vim manually after opened the file, it will work as expected.
Any idea what is going wrong here? Thanks!
Tweaks to a colorscheme have to happen after the colorscheme has been set. Usually, if you have the :colorscheme in your ~/.vimrc, and put the :highlight commands after it, that should work.
Your case seems to be different (which could be caused by a plugin manager affecting the loading order, or you might even have a dynamically changing colorscheme). To handle such eventualities, you can instead hook into the ColorScheme event:
autocmd ColorScheme * highlight clear SpellBad
autocmd ColorScheme * highlight SpellBad cterm=underline
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).
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).
I have a syntax rule that highlights trailing whitespace:
highlight Badspace ctermfg=red ctermbg=red
match Badspace /\s\+$/
This is in my .vimrc. It works fine, but the problem is I use splits a lot, and it seems that the match is only run on the first file you open, as well it should because the .vimrc should only run once.
Anyway, how can I get the above syntax to match any file that is opened? Is there a "general" syntax file? Is there any other way to run match each time a file opens rather than just once? I'd like to know both because I could end up using either one in the future.
The :match command applies the highlighting to a window, so you can use the WinEnter event to define an :autocmd.
:autocmd WinEnter * match Badspace /\s\+$/
Note that there are already a number of plugins for this purpose, most based on this VimTip: http://vim.wikia.com/wiki/Highlight_unwanted_spaces
They handle all that for you, and turn off the highlighting in insert mode; some can also automatically delete the whitespace. In fact, I have written a set of plugins for that, too: ShowTrailingWhitespace plugin.
You could accomplish this by using an autocmd:
highlight Badspace ctermfg=red ctermbg=red
autocmd BufEnter * match Badspace /\s\+$/
However, there's another way to accomplish your specific goal of marking trailing whitespace. Vim has a built-in feature for highlighting "special" whitespace, which includes tabs (to differentiate from spaces), trailing whitespace, and non-breaking spaces (character 160, which looks like a normal space but isn't).
See :help list and :help listchars. Here's what I use:
set list listchars=tab:>·,trail:·,nbsp:·,extends:>
listchars has the benefit of working with any file type, and marking up multiple whitespace types that are of interest. It is also a lot faster (match will be noticeably slow on giant files) and built-in already.
(Note that those are funky non-ASCII dot characters, which should work fine for you if you cut-and-paste into a UTF8-capable Vim. If they don't work for you, you can use any characters you like there, such as periods or underscores).
Here's what it looks like for me:
The correct approach to this problem is actually to use :syntax to define a custom syn-match.
Try putting this in your vimrc:
augroup BadWhitespace
au!
au Syntax * syn match customBadWhitespace /\s\+$/ containedin=ALL | hi link customBadWhitespace Error
augroup END
Edit: It should also be noted that there is built-in support for highlighting trailing whitespace with the 'list' option; see :help 'listchars' and :h hl-SpecialKey (SpecialKey is the highlight group used to highlight trailing whitespace characters when 'list' is on).
This is accomplished using autocmd. The events you're looking for are BufWinEnter and VimEnter. From the Vim manual:
BufWinEnter
After a buffer is displayed in a window. This
can be when the buffer is loaded (after
processing the modelines) or when a hidden
buffer is displayed in a window (and is no
longer hidden).
Does not happen for |:split| without
arguments, since you keep editing the same
buffer, or ":split" with a file that's already
open in a window, because it re-uses an
existing buffer. But it does happen for a
":split" with the name of the current buffer,
since it reloads that buffer.
VimEnter
After doing all the startup stuff, including
loading .vimrc files, executing the "-c cmd"
arguments, creating all windows and loading
the buffers in them.
Try putting this in your vimrc:
augroup BadWhitespace
au!
au VimEnter,BufWinEnter * match Badspace /\s\+$/
augroup END
Do :help autocmd for more info.
This is completely wrong because :match is window-local, not buffer-local. Ingo Karkat has the right idea. Unfortunately, there is no good way to avoid triggering the autocmd every time you enter the window.
More to the point, though, this is a job for a custom syntax, not match.
I am using mvim. I have following lines in my vimrc .
"highlight text that goes over 80 columns
highlight OverLength ctermbg=red ctermfg=white guibg=#592929
match OverLength /\%81v.*/
Sometimes in my project I see the extra characters highlighted and sometimes I don't . I mean once vim is displaying extra characters then it will continue to display extra characters. However if I get out of vim and start vim instance it might not highlight extra characters. To fix that I type
match OverLength /\%81v.*/
I am puzzled by why it is happening. Is it possible that some plugin is messing with these settings?
It's possible that a plugin is undoing it by setting its own match. It's also possible that you're creating a new window. match is per-window (not per-buffer, and not global) so it only applies to the window that was active when the match command was executed.
You can try creating an autocmd to set up the match on new windows:
au! WinEnter match OverLength /\%81v.*/
Note that this is not executed on the first window.
Well I had the same problem when using new Tabs in MacVim 7.3 (Snapshot 56). When opening a new window the highlighting worked, but when I opened a new Tab it did not work anymore. So I added the following autocommand to my .vimrc file:
autocmd BufWinEnter,BufRead * match OverLength /\%81v.\+/
Now it works for me, in any new window or tab and buffer.
I hope this will help!