Turn off highlighting a certain pattern in vim - vim

In vim, I am editing a file of filetype "markdown", but which contains latex math expressions such as $x_i$. Vim's syntax highlighting for markdown thinks the pattern *_* (letter-underscore-letter) is an error, and highlights the underscore in such patterns bright red. I would like to turn this off by adding a line to my .vimrc:
autocmd BufEnter *.Rmd "Dear vim, please don't highlight the pattern *_*"
What is the appropriate command to do that? Is it possible at all to do that in .vimrc, without editing a syntax file?
Note: I want to keep the markdown syntax highlighting in general, only turn off that particular feature.

If you want to remove _ from the markdown error pattern, you can redefine it. In my case I want to turn off error notifications of underscores in a word as I put a lot of URLs in my documents.
There's a line that defines the error pattern inside syntax/markdown.vim file
" Original error pattern
syn match markdownError "\w\#<=_\w\#="
Remove the _ from the pattern and add that to ~/.vim/after/syntax/markdown.vim.
" New error pattern without the underscore
syn match markdownError "\w\#<=\w\#="

You have to modify the Markdown syntax for that. One way would be to remove the parsing of the error:
:syn clear markdownError
But that would cause the other syntax rules to start an italic section on that _ char. Better just clear the error highlighting with:
:hi link markdownError Normal
To maintain the general error highlighting, but only define exceptions for the special $x_i$ string, define an overriding syntax group; luckily, this is easy because no existing syntax is there:
:syn match markdownIgnore "\$x_i\$"
(Adapt the regular expression to match all possible math expressions.)
This needs to be put into ~/.vim/after/syntax/markdown.vim to be executed after the original syntax script.

Related

Highlighting set of specific keywords in gvim

I have tried highlighting specific words by adding them in .vimrc, colorscheme file and also in syntax.vim(I changed one at a time, not altogether).
syn match mygroupwords '\c\<\(-word1\|-word2\)'
hi def link mygroupwords colo_words12
hi colo_words12 guifg=red1 gui=bold guibg=white
But somehow it seems to be getting overwritten by default syntax highlighting
i need to highlight keywords irrespective of color-scheme or file-type which have following words-
Ex; -word1 , -word2
Any suggestions?
explanation of your failed attempts
A colorscheme just provides color definitions for predefined highlight groups; that's the wrong place for actual syntax matches! ~/.vimrc is the first configuration that is read; if a filetype is detected and a corresponding syntax script is loaded, that will override your syntax definition.
syntax extensions
If your desired highlightings are extensions to an existing syntax, you can place :syntax match commands in a syntax script in the after directory. For example, to extend Python syntax, put it in ~/.vim/after/syntax/python.vim. That may still fail if the original syntax obscures the match; sometimes, this can be solved via containedin=...
independent marks
If your highlightings are independent of syntax and filetype, there's a different built-in mechanism: :match (and :2match, and additional variants via :call matchadd(...)):
:match mygroupwords /\c\<\(-word1\|-word2\)/
This goes on top of (and is independent of) syntax highlighting. However, it is local to the current window. So, if you put this into your .vimrc, it will only affect the first window (but any files viewed in there). To apply this globally to window splits and tab pages, you have to use :autocmds. It's not trivial to get this totally right. If you need such a full solution, have a look at my Mark plugin; this supports multiple colors, allows presetting with :[N]Mark /{pattern}/ (similar to :match), and highlights in all windows.

Vim syntax highlighting unwanted words

I have created a syntax file for some AnB-files for vim and while it does load the syntax highlighting and highlight everything I want it to highlight, it seems to highlight some unwanted stuff as well.
The images show both my highlight file and the AnB file and the problem is:
The word "Elgamel", the characters "g" and "h" (any single lower-case letters) and the words "M1" and "M2" (any single upper-case letter followed by an integer) are highlighted with an unwanted magenta color. Anyone knows what to do here? I tried searching for the problem on both google and stackoverflow, but I couldn't find any similar questions (I might not search using the correct terms, though :D)
Syntax file: http://i.imgur.com/bYoAQcu.png
AnB file: http://i.imgur.com/FOtccXJ.png
This looks like error highlighting from the spell checker.
:set nospell
should then turn it off. You can determine which syntax groups get spell checked via :syntax spell, see :help :syn-spell. Usually, you use enable spelling in comments, etc. by added contains=#Spell.
Here's a tip for syntax script development: When you need to find out which syntax group causes the highlighting. :syn list shows all active groups, but it's easier when you install the SyntaxAttr.vim - Show syntax highlighting attributes of character under cursor plugin.
You can use the plugin HiKinkTrace to determine the highlighting group for the offending text, that should help you to narrow it down.

How can I cancel out a match pattern for a particular file extension?

I currently have the following line in my vimrc to highlight lines longer than 80 characters wide.
match ErrorMsg '\%>80v.\+
I would like this rule to be disabled when I am editing a SQL file, so I tried adding this line based on what I read in the help.
autocmd BufNew,BufRead *.sql :match ErrorMsg none
However, this throws the following error whenever I load a sql file.
Error detected while processing BufRead Auto commands for "*.sql":
E488: Trailing characters: :match ErrorMsg none
Press ENTER or type command to continue
How can I make it work without throwing the error?
Get rid of the highlight group. (colon removed since it doesn't affect the outcome)
autocmd BufNew,BufRead *.sql match none
You are only allowed 3 matches and you need to use either match, 2match or 3match. So you only need to clear the specific one that you used
Notice how in :h :match {group} is not listed in the syntax for match none
:mat[ch]
:mat[ch] none
Clear a previously defined match pattern.
Note: In recent Vim 7.3+ versions, you can alternatively use:
:setlocal colorcolumn=81
in your otherwise identical :autocmd.
Also note that both this and your :match are window-local, so if you switch buffers, the highlighting will persist. To completely fix this (mere nuisance) would require more elaborate autocmds.

VIM syntax coloring lua function name?

I'm using VIM via terminal (SSH) and want to modify my syntax coloring settings in VIM to make the function_name Yellow for Lua programming. Unfortunately, I can't figure out how to do that.
For example, in the code below - I want my VIM to syntax color myFunc yellow
local function myFunc(arg1)
...
end
I've been able to figure out how to make function yellow, by using this code below:
hi luaFunction ctermfg=Yellow
But this code does not color the word myFunc yellow (and frankly, I'd rather not syntax color function at all)
Question: Any ideas how I can syntax color the function_name in Lua?
Another (more complex) regex to match the function name.
:hi luaCustomFunction ctermfg=yellow
:syn match luaCustomFunction "\(\<function\>\)\#<=\s\+\S\+\s*(\#="
This uses a look-behind to match the word function. Then after 1 or more spaces it will highlight any word that has a parenthesis as the first non whitespace character after it.
This only highlights the function name. It does not highlight the parenthesis.
I think if you put these commands in .vim/after/syntax/lua.vim it should work.
The problem with putting them in your vimrc is that sometime after the vimrc is sourced the syntax highlighting file is sourced and generally the first line in there is syn clear. (Which wipes the custom syntax highlighting you just set)
This very naive implementation works with your sample:
:hi luaCustomFunction ctermfg=yellow
:syn match luaCustomFunction "\s\+\zs\S\+\ze("
It is obviously very limited but at least you get a starting point. Read :h syntax for further help.
How about:
hi def link luaFunction Function
hi Function ctermfg=Yellow

How to create a syntax rule that can be contained anywhere?

The following snippet from my .vimrc highlights superfluous whitespace at line ends in a shade of gray:
autocmd Syntax * syntax match MySpace /\s\+$/
autocmd ColorScheme * highlight MySpace ctermbg=238
But this does not work when this whitespace is already matched by a syntax group. For example, trailing whitespace in various types of comments is not marked.
The manual talks about the contains=ALL option for syntax groups, but there seems to be no analogous containedin=ALL. Can I emulate it in any way? The only method I could come up would be to list all relevant syntax groups in the containedin= option of MySpace, and that's clearly tedious and not at all elegant.
Don't know how to do this with Syntax, but you can use the listchars options to highlight trailing spaces.
From my .vimrc:
" List chars
set listchars="" " Reset the listchars
set listchars+=tab:\|\ " show tabs as "|"
set listchars+=nbsp:· " show non-breaking spaces as "·"
set listchars+=trail:· " show trailing spaces as "·"
set listchars+=precedes:«
set listchars+=extends:»
You should use the :match command (or matchadd()), as described in the Vim Tips Wiki article about this particular topic.
If you like a ready-to-use solution for this, you can also try out my ShowTrailingWhitespace plugin, or one of the alternatives listed on the plugin page.

Resources