Applying different colorschemes to different windows - vim

I want to used different colorschemes for different filetypes and I added the following code in my .vimrc
function SetColorScheme ()
if &filetype != "vo_base"
colorscheme desertEx
endif
endfunction
au WinEnter * call SetColorScheme()
This works fine with one issue.
If I open a .otl file, say todo.otl (vo_base), and then open another file, say example.xml, using :sp the colorscheme desertEx does not get applied to the second window (the one having example.xml).
If I use BufEnter instead of WinEnter than desertEx gets applied to both the windows.
Is there a way to make sure that when I open a window with :sp the above function (a) runs, and (b) runs only for that particular window and not all the windows in the current session.

No there's no way to do that. There can be only one active colorscheme at the same time in vim.

Something that comes to mind is to create a color scheme that directly points to the low-level syntax groups in the Vim syntax files.
Take for instance c.vim for the C programming language. You will find for instance syntax hightlighting groups such as: cStatement, cLabel, cConditional, cType.. etc.
Take python.vim and you will find pythonDefStatement, pythonFunction, pythonConditional, etc..
So, if you want to use different color schemes for C code and python, you would copy the two original color schemes to ~/.vim/colors/mycolorscheme.vim and edit them to point to the low-level syntax groups instead of the generic high level groups such as Comment, Constant, Error, Identifier, etc. that are found in many available color schemes.
Note that you would probably want keep a default stanza of 'highlight' statements on top of these other two to take care of syntax highlighting for files that contain neither C nor python code.
To clarify, you could edit the celebrated 'Hello World' code and issue the following from the Vim command line:
:hi cInclude ctermfg=cyan guifg=cyan
You have not changed color schemes, other files displayed in other windows or tabs are unaffected, and yet the '#include' is now displayed in a different color.
Unless you absolutely need the feature, I would advise against it, because it pretty much breaks Vim's syntax highlighting. Besides, it will require significant work to convert the existing ':hi' statements comprised in the original color schemes because there are usually many low-level syntax highlighting groups.
A somewhat better approach might be to link the language-specific low level groups to high-level groups that are also specific to each language. This would help keep the custom color scheme file reasonably small, but requires additional inventory work.

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.

Highlighting and syntax trouble in vim using rmarkdown

I would like to get Vim to stop highlighting list characters (-,*) and heading characters (#) in rmarkdown. You can find a screenshot here: https://imgur.com/a/0YSB8V8.
This is happening when I set the file type to either pandoc or rmd. This is also happening no matter what terminal or colortheme I use.
I have the plugins: vim-pandoc, vim-pandoc-syntax, and vim-rmarkdown installed.
I would like to know a way to make the two characters just appear normally.
I would also like to know if there is a way to make italicized text in tables and headings appear italicized. As far as modifying the appearance of italicized text, I've tried using: hi Italic ctermfg=red cterm=italic in my vimrc, but that does not seem to affect the text in between asterisks (*) in rmd files. I admit I don't know much about the way that syntax works in Vim. Do I need to modify after/ftplugin/rmd.vim or runtime/syntax/rmd.vim? What is the difference between the two?
Any help would be appreciated!
Your syntax highlighter does not seem to recognize the bullet points, but thinks that they mark the beginning of an italic span. Maybe you have a clash of plugins. You could also try another highlighter (e.g. vim-polyglot, supports italics).
vim-pandoc-syntax uses the conceal feature (:h conceal). You can recolor the highlighting group Conceal to change the appearance of the replacement characters.
You can put changes to existing syntax files in .vim/after/syntax/rmd.vim. Files in syntax are executed when they are needed for the first time, but at most once per session. Files in ftplugin are executed every time the file type is changed.

vim colorscheme poor syntax support

I have tried well over 15 different colorschemes for vim. I have made all of the correct settings for full color support in terminal. This 'problem' persists in both terminal and gvim. Perhaps it is not a bug and simply the design of the colorschemes themselves, but only one colorscheme I have tried actually has decent highlight support. For example:
In this python class all of the colorschemes will only highlight a couple of things.
wombat only highlights comments and the if.
molokai is extremly dissapointing but at least gets the ints
All of the themes I try are similar to these two except 'Crayon' which compares like so:
Most of the vim color-scheme github pages show previews with highlight support compared to crayon. My question is what might be causing this problem? or is this just the design of the themes themselves?
Syntax highlighting is the combination of two things:
syntax definition, provided by syntax scripts typically found in syntax/ or after/syntax/,
highlighting definition, provided by colorschemes typically found in colors/.
The former defines syntax groups, the latter defines how those groups look.
But those almost never come in pairs so there's no guarantee whatsoever that every possible syntax group is properly handled by every possible colorscheme.
If a colorscheme you like doesn't handle some of the syntax groups you expect it to handle, open an issue or (better) patch it.

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.

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