Highlighting set of specific keywords in gvim - vim

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.

Related

How to use different colorscheme and syntax highlighting in vim?

Suppose I want to use this colorscheme:
https://github.com/NLKNguyen/papercolor-theme
I copied the PaperColor.vim file into .vim/colors and made my .vimrc:
syntax on
colorscheme PaperColor
background=light
Now, I want to use this syntax highlighting for haskell files: https://github.com/raichoo/haskell-vim/tree/master/syntax
There are two syntax highlighting files. Which one am I supposed to use, and where do it put them?
Thanks!
Do I put it in ./vim/syntax and vim auto-loads all files in ./vim/syntax folder?
It seems like to load haskell.vim automatically. But doesn't load cabal.vim. Wondering if it only loads haskell.vim when I open .hs files? I'm trying to make it like that. Can vim load multiple syntax files at once?
TL;DR: Everything's (mostly) fine. There's a difference between colorschemes and syntax scripts.
Most filetypes (like python) in Vim come with a syntax that defines highlight groups (see them via :highlight python<C-d>). These particular groups (e.g. pythonFunction) are then linked to a set of default groups (:help highlight-groups, e.g. Identifier). A colorscheme then provides combinations of foreground / background color and/or formatting like bold and italic (separately for terminals, color terminals, and/or GVIM) for the default groups.
highlight group → default group → color + style
pythonFunction → Identifier → term=underline ctermfg=3 guifg=DarkCyan
So, for a set of beautifully matching colors that please your personal taste, you choose a colorscheme. For you, that would be colorscheme PaperColor. Note that the background needs to be set before choosing the color (and you've missed the :set command):
syntax on
set background=light
colorscheme PaperColor
The syntax scripts know how to parse a certain syntax (for you: both haskell and cabal; what gets activated depends on filetype detection, which usually does the right thing, but you could also manually override it (:setlocal syntax=cabal); I think the former is for Haskell source code while cabal is a package definition). They basically recognize certain syntax elements, and link them to generic highlight groups (like Statement, String, Comment, and so on). Now how these are then colored (e.g. bold green) is determined by your chosen colorscheme.
As you can see, colorschemes and syntax scripts each have a distinct role, and play together. While the former is a global personal choice, the latter is activated based on the detected filetype, which is different for each buffer.

Set spelling exception in vimrc

When editing a text in markdown, I don't want to highlight bibliography entries. This can be achieved with the following command:
:syn match CitNoSpell '\[#[^[:space:]]\+\]' contains=#NoSpell
However, if I enter this command to .vimrc, it is ignored. I assume that is because spell file is loaded after vimrc has been read, and this definition is not kept.
How should I force vim to ignore this pattern? I prefer it to stay in .vimrc, as I synchronize the file across multiple systems, but another solution would also be welcome.
As the ~/.vimrc is loaded first (before any files), the syntax of an opened file is set only later, and syntax scripts :syntax clear any existing syntax stuff, including your definition.
The right place for your customization would be the :help after-directory; i.e. ~/.vim/after/syntax/markdown.vim, as this will be sourced after $VIMRUNTIME/syntax/markdown.vim.
If you insist on configuring this within your ~/.vimrc, you can try the following autocmd, which has to be put somewhere after :syntax on:
autocmd Syntax markdown syn match CitNoSpell ...
PS: For consistency, as you're tweaking the Markdown syntax, your added syntax group should also start with the syntax name, i.e. markdownCitNoSpell.

How can I apply custom syntax highlighting in VIM to all file types?

The most preferable way to do this would be in some like my .vimrc file or another location in my vimfiles that's easily persisted and not attached to an extra plugin.
The help files for VIM (along with almost all solutions found on the Internet abroad) relate directly to adding syntax highlighting for a specific file type.
However, how would one add highlights that apply to all files?
An example would be highlighting extra keywords as part of the Todo highlighting group - such as "NOTE", "INTERNAL", etc.
I've attempted to use vimfiles\after\syntax\..., but again, it seems to be predicated on the right file type getting used for the .vim file created in that directory.
So, something like vimfiles\after\syntax\cpp.vim with the following works to achieve this in C++:
syntax keyword cTodo contained NOTE INTERNAL IMPORTANT
for C++ files specifically, and this works how I would expect it to.
But how can this be generalized to all file types when a file is loaded into a buffer?
You can hook into the syntax script loading via the Syntax event. Be sure to define this after :syntax on in your .vimrc:
:autocmd Syntax * syntax keyword allTodo NOTE
If you also want to handle buffers that don't have any syntax (/ filetype) set, add the BufNewFile,BufReadPost events.
Alternative
Depending on the syntax, you may need to specify contained[in], or :syn cluster add=... to augment the existing syntax. That unfortunately cannot be generalized. An alternative approach would be using :match or :call matchadd(...), which goes on top of (and is totally independent of) syntax highlighting. Since this is window-local, the autocmd would be:
:autocmd VimEnter,WinEnter * match Todo /\<NOTE\>/

Changing color of vim syntastic error window

I have installed syntastic on VIM to help me show errors in PHP code however with the current colorscheme/setting I have to following colors:
As you can see it's very hard to read, I wonder if there is a way to change the color if this error window specifically.
If this is only for the current selected item in the quickfix window, that's the Search highlight group on top of the normal quickfix highlighting. You then have to change either one; the Search group will affect search results in other windows, too.
If this is other / special Syntastic highlighting, you best look through all groups in the :hi output to find the bad one.
Overriding
Unless you want to completely switch your colorscheme, you can adapt individual highlight groups via :highlight commands after the :colorscheme command in your ~/.vimrc. Either :hi link to another predefined group, or provide your own ctermfg=... guifg=... etc. color definitions, as described by :help highlight-args.
Syntastic doesn't change (nor cares about) highlighting of the error window. It's a plain quickfix window, with filetype qf. Looking at syntax/qf.vim, the default highlighting is this:
syn match qfFileName "^[^|]*" nextgroup=qfSeparator
syn match qfSeparator "|" nextgroup=qfLineNr contained
syn match qfLineNr "[^|]*" contained contains=qfError
syn match qfError "error" contained
hi def link qfFileName Directory
hi def link qfLineNr LineNr
hi def link qfError Error
Thus, if you see the quickfix window in different colours than the main text, it's because your colour scheme has specifically intended it to look that way. You can override highlighting for qfFileName, qfSeparator, qfLineNr, and qfError to make it more readable, but the better solution IMO would be to use a less broken colour scheme.
Edit: Vim 8.0.641 and later has QuickFixLine.

Set colors for custom syntax keywords in Vim?

I'm using a syntax file in vim that defines a number of filetype-specific syntax keywords. To color files of this type I've also created a colorscheme file for the share directory that attempts to highlight these syntax keywords, however they don't take effect when I open files of that extension.
My color file does however highlight normal groups such as Normal, Special, Comment, etc. As well, when I attempt to move these highlight commands to my .vimrc file, they still have no effect. However, after the file is loaded in vim, entering the highlight commands manually works as intended.
Is there something special I need to do in order to use syntax keywords defined in the syntax files? Do I maybe need to specify the load ordering of my syntax files & color files in my .vimrc?
EDIT: using :scriptnames, I am able to see that my custom colorscheme file loads long before the syntax file, which in fact loads dead last. However, my .vimrc file specifies the colorscheme CustomPersonal as the last line, far after syntax on.
Vim has a built-in mechanism for overriding syntax groups, so if you do everything right (even in your ~/.vimrc), this should work. The solution suggested by #MatthewStrawbridge might work, but feels wrong, because colorschemes are global in Vim, and therefore shouldn't be specified in an ftplugin.
Here's how the overriding works:
Most filetypes (like python) in Vim come with a syntax that defines highlight groups (see them via :highlight). These particular groups (e.g. pythonFunction) are then linked to a set of default groups (:help highlight-groups, e.g. Identifier). A colorscheme then provides combinations of foreground / background color and/or formatting like bold and italic (separately for terminals, color terminals, and/or GVIM) for the default groups.
highlight group → default group → color + style
pythonFunction → Identifier → term=underline ctermfg=3 guifg=DarkCyan
So, for a set of beautifully matching colors that please your personal taste, you choose a colorscheme. In order to tweak some particular associations, you can change the linking of highlight group to default group, e.g.:
:hi link pythonFunction Special
This can already be done in ~/.vimrc. As long as the syntax script correctly uses :hi def link, the original link established by you will be kept; the def[ault] parameter ensures that the link will only be created if no link exists yet.
Your .vimrc will be run when you first start Vim; the syntax file won't be applied until the file loads, and it sounds like it's overriding your settings.
Instead you could put your custom color scheme in ~/.vim/after/syntax/<filetype>.vim instead of CustomPersonal.vim (or at least call it from there).
Another alternative would be to add an autocmd to your .vimrc to source CustomPersonal.vim in response to a suitable event.

Resources