I currently have an autocmd for BufAdd that will add marks (signs) to certain lines of the loaded file. I have defined custom highlights for each sign, along the lines of:
highlight Custom1 ctermfg=black ctermbg=red
highlight Custom2 ctermfg=black ctermbg=green
sign define custom_sign1 text=>> texthl=Custom1
sign define custom_sign2 text=<< texthl=Custom2
Signs are create with:
sign place ID line=LINE name=custom_sign1 file=FILENAME
where ID is a unique id for the sign in the file, LINE is the line number for the sign, and FILENAME is the name of the file.
The signs all get created correctly, but it kills the syntax highlighting. I then have to do syntax on again, but then the custom colors for the signs don't show, at which point I have to reload the vimscript that defines those colors.
How do I make this not break syntax highlighting?
Also, this is done from an autocmd that uses the BufAdd and VimEnter events.
Edit: wrote the wrong BufXXX event at the end of the post - should be BufAdd
Update:
To duplicate my problem, source the .vim file below, and then open a new buffer that is normally syntax-highlighted. The first line will have a red >> sign, but the file will not be syntax highlighted. Note that it must be a new buffer (not already in the buffer list):
highlight Custom1 ctermfg=black ctermbg=red
highlight Custom2 ctermfg=black ctermbg=green
sign define custom_sign1 text=>> texthl=Custom1
sign define custom_sign2 text=<< texthl=Custom2
function! SignLine()
execute "sign place 1 line=1 name=custom_sign1 file=" . expand("<afile>")
endfunction
autocmd!
autocmd BufAdd * call SignLine()
augroup END
I created a simple file called test.c. After opening vim with the above .vim script as the initial file, sourcing it (with :so %), and then opening test.c, this is what I see:
After turning the syntax back on with :syntax on, it now looks like this (custom sign colors are now broken):
To get the custom sign colors working again, I have to re-source the vimscript AGAIN (:so test.vim), after which both the custom signs and the syntax highlighting work:
I shouldn't have to do this. Why is this happening?
When opening your script in Vim, the syntax highlighting already gives a big clue:
That last line isn't highlighted correctly because there's no :augroup command that corresponds to augroup END! You need to define it like that (or combine the two inner lines to autocmd! BufAdd ...):
augroup SignLine
autocmd!
autocmd BufAdd * call SignLine()
augroup END
The initial :autocmd! removed all defined autocmds, also the ones responsible for loading the syntax. That explains the broken behavior.
Related
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.
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 molokai in vim to code in python/html/css/javascript. When I edit python files (or javascript) parenthesis are not colored. This is not true for simple scripts (like molokai.vim itself) where parenthesis are colored gray.
I edited molokai.vim and added
hi parens guifg=#999999
and then I edited .vimrc and added:
syn match parens /[(){}]/
but parenthesis and brackets remain white.
What am I doing wrong?
Never use :syn to highlight all filetypes, there is matchadd() for this. Using :syn can easily break highlighting, matchadd() is an overlay.
Syntax highlighting is being overridden when Syntax event fires. More, it has effect only on the current buffer. So just syn in vimrc will never work, you have to use autocommands
autocmd! Syntax python :syntax match Parens /[(){}]/
(for python it is safe as parenthesis and figure brackets are not matched by any other syntax element).
In javascript parenthesis (()) are already matched by javaScriptParens highlighting group. Thus you have to use
hi def link javaScriptParens Parens
(in colorscheme). Braces are matched by javaScriptBraces and require similar command.
To determine what highlighting is used for specific symbol I place cursor on this symbol and launch
echo 'Normal '.join(map(synstack(line('.'), col('.')), 'synIDattr(v:val, "name")'))
, the last displayed word is normally what you need. If only Normal is displayed then symbol is not highlighted and you have to go 2., otherwise you have to go 3.
For universal solution disregarding currently used highlighting you may use matchadd() as I already said. But it is local to window so if you are working with multiple windows/tabs you can’t go without autocmd:
autocmd! WinEnter * :if !exists('w:parens_match_id') | let w:parens_match_id=matchadd('Parens', '[(){}]') | endif
All autocommands are to be surrounded with
augroup HighlightParens
autocmd! …
augroup END
I would like to highlight the word TODO in vim, no matter what file is edited (code or normal text). It currently works for many different languages (for example, TODO is highlighted in C/Java comments by default), but I use vim for non-code text files and I would like to see the TODOs highlighted for them too.
What is the easiest way to do that ?
Highlighting TODO for every filetype and without possibly breaking other syntax rules can be done only by using :match/matchadd():
augroup HiglightTODO
autocmd!
autocmd WinEnter,VimEnter * :silent! call matchadd('Todo', 'TODO', -1)
augroup END
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