Color specific term - vim

More specifically I would like the term "False" in python to be always colored red as opposed to the term "True" which is already green(as its "False", since both are part of the pythonBuiltin category I believe). So is there a way to determine a color only to a specific word? I want to keep my current colorscheme and only add this tweak.

Woops think I got it.
I added to my .vimrc:
highlight redOnes ctermfg=red
match redOnes /False/
:h match was helpful

Related

Color code for formal parameters

I am trying to modify my current color scheme for vim.
I would like to match formal parameters and give them an orange color, like this:
var f = function (orange, orange, orange) {...}
However, looking at javascript.vim in the syntax folder there does not seem to be an identifier for formal parameters.
I wonder if anyone have some experience with vim color schemes and are willing to guide me in a direction?
This alternate JavaScript support plugin defines a jsFuncArgs group that you can highlight in orange with this command:
:hi jsFuncArgs ctermfg=208 guifg=#ff8700
If your colorscheme doesn't define any color for that group you can simply add the line above to it, without the colon. If you don't use a third party colorscheme, you can add that line at the bottom of your ~/.vimrc.

Making a Vim theme that disables highlighting except for some special keywords

Inspired by several posts, like Your syntax highlighter is wrong, Coding in color and A case against syntax highlighting and some others, I decided making a Vim theme that applied some of these concepts would be a good idea.
The thing is I'm not exactly sure how.
From what I can tell, in order to make a Vim theme you need to basically link a color with a syntax identifier or name. And repeat this hundreds or dozens of time in order to have in your lap a theme.
Like for example linking the color #ff0000 (red) and the syntax identifier, or key, Error. As an example. Not sure if that's actually the syntax key.
This would work fine, except that, every syntax that I don't consider important I have to define as just a default foreground value.
And let's say I wanted to add a new syntax keyword, I'd have to do it with ftsyntax and stuff (I believe) and that would be filetype specific etc.
So the first question is:
What would be the best way to give everything a default foreground color and only pick the exceptions to have some colors?
And the second, perhaps more important question is:
How do I syntax highlight a specific piece of text without having to add a syntax rule? For example have a regex that finds any = and highlights them green, without having to add a syntax rule specific for that.
Any help is appreciated. Of course if the approach I'm taking to this is not ideal or sucks I am open to suggestions to alternatives. Thank you. :)
See the example syntax file below:
syn keyword myKeywords We Are Important Keywords
syn match myEquals '='
hi link myKeywords Special
hi link myEquals Operator
This will put We, Are, Important and Keywords into the myKeywords syntax group and = into the myEquals syntax group.
Then we specify how we want to highlight them, by linking it to the Special and Operator highlight groups.
See: :help group-name for a list of the highlight groups and what the colors look like with your color-scheme.
In my color-scheme, Special is Red and Operator is green.
By default, everything else is set to the default foreground color.
I saved this to ~/.vim/syntax/greduan.vim and tested with :set syntax=greduan
Your question touches two domains:
syntax definition
syntax highlighting
Syntax definition, as in Caek's answer, is simple for the first 10 minutes but grows very quickly into a major PITA because it is a core aspect of Vim's architecture with ramifications far beyond syntax highlighting.
Syntax highlighting has its pitfalls but it is a lot simpler than syntax definition.
I think that you can tackle the problem described in those blog posts with syntax highlighting first and, if needed, graduate to syntax definition.
Grab a simple colorscheme like Busybee.
Link all the highlight groups you don't need to Normal while leaving the ones you want to keep:
hi link Foo Normal
What would be the best way to give everything a default foreground
color and only pick the exceptions to have some colors?
What is best depends ... for me the best, because quickest way was clearing unwanted highlighting in ~/.vimrc:
sy on
hi c Constant|hi c Error|hi c PreProc|hi c Special|hi c Statement|hi c Type
hi c Identifier
How do I syntax highlight a specific piece of text without having to
add a syntax rule?
If by syntax rule you mean syntax item, I'd say you cannot have syntax highlighting without defining syntax items.

Multiple matches for syntax highlighting in VIM

I'm writing a syntax file to match a log format (basically column based; think syslog for a similar example), and I'm trying to set up a type of inheritance for columns.
I have two main goals with this.
First, I want to say that column 3 is the "component" field (let's say it's marked by a header; it could also be at a fixed position) and set the background to, say, Grey. I then want to say that component "foo" gets a foreground color of Red, and component "bar" gets a foreground color of Green, but they should inherit the background color of the "component" column. In this case, the field should really have two syntax matches; this also makes it easy to conceal the entire column (a la Toggling the concealed attribute for a syntax highlight in VIM)
Second, there's a field for levels; I want to set the background of the entire line for a critical level message to Red, but the foreground should be continue to be set via the normal highlighting (component, source, etc; I left off most of the other requirements).
From what I can see in the vim documentation, this doesn't seem possible. Am I missing something? Alternatively, can anyone suggest a good workaround?
Thanks
You can't (yet). For each character, Vim only uses one particular highlight group, determined by the last, "most inner" non-transparent syntax group match.
To work around this, you need to define a combined highlight group and corresponding :syntax commands. Some syntaxes (e.g. $VIMRUNTIME/syntax/html.vim, and various Wiki formats) use that for combining bold, italic and underline, but it gets tedious and repetitive after that.
There's a patch proposed that would add a combine modifier for :syntax commands; it's in Bram's Todo list.

VIM custom syntax school of rock

I try to compact some chord files and then need to highlight some stuff so it is more visual. This syntax is where chord is followed by a comma then a number showing how many beats, and all surrounded in curly brackets. Here is the example from a classic rock song you might have heard:
{Dm,4}Don't cry, don't {C/D,4}raise your eye
It's {Bb/D,2}on-ly {Am,2} teen-age {Dm,4hold}waste-land
I simply want to make the chord names one color, and the time notation another color... In a nutshell that's everything between { and next , as one color then up to the next } as a second color. Maybe the brackets should be another color too. I suck at regular expressions but this forum can point me in right direction to eventually master them I hope.
Here is a very simple version that will just highlight the whole {...} blocks. Put the following in ${HOME}/.vim/syntax/rock.vim:
if exists("b:current_syntax")
finish
endif
syntax match rockNotation "{[^}]*}"
highlight link rockNotation String
And the following in ${HOME}/.vim/ftdetect/rock.vim:
autocmd BufRead,BufNewFile *.rock set filetype=rock
Now whenever you open a .rock file, the chords should be highlighted. (If detection doesn't work, try to set :set filetype=rock manually, see if it changes something.) Note that if you edited these files from Vim, you may need to restart it for the effects to apply.
Here is a longer version, where chords and time indications are highlighted in different colors:
if exists("b:current_syntax")
finish
endif
syntax clear
syntax case match
setlocal iskeyword+=#
setlocal iskeyword+=-
setlocal iskeyword+=+
syntax match rockChord /\<[A-G]\(b\|#\)\=\(m\|-\|dim\|+\|aug\|7\|m7\|Maj7\|m7b5\)\=\(\/[A-G]\(b\|#\)\=\)\=\>/ contained
syntax match rockDuration /[1-9][0-9]*\(hold\|mute\)\=/ contained
syntax region rockAnnotRegion start=/{/ end=/}/ contains=rockChord,rockDuration
highlight link rockChord Type
highlight link rockDuration Constant
highlight link rockAnnotRegion Delimiter
As you can see, my regexp for chords got a little wild. It supports things like A#m7b5/Db, which of course makes little sense (and sounds horrible), but you get the idea.

Vim: C++ symbols' color

VIM: Is it possible to change the color of these symbols:
~!%^&*()-+=[]{},.<>?:/;
like Visual Studio does?
The C/C++ syntaxes are defined in syntax/c.vim and syntax/cpp.vim. If you're using Linux, the main syntax directory is in /usr/share/vimXX/, where XX is the version (e.g. mine are in vim72). I don't know about installation directories on other OSes, but I'm sure you can find it. I'd suggest making a copy of these and placing them in your user vim directory (for example, in Linux, $HOME/.vim/syntax/c.vim and so on). You can then add whatever you like.
The C++ syntax sources the C syntax, so any symbols you want highlighted in both should go in c.vim, and anything for C++ only should be in cpp.vim.
To get syntax highlighting for specific symbols, you'll need to use a syntax match statement, something like:
syn match cUserSpecialCharacter display "[~!%^&*()-+=[\]{},.<>?:;]"
syn match cUserSpecialCharacter display "/[^*/]"me=e-1
syn match cUserSpecialCharacter display "/$"
I called it cUserSpecialCharacter since cCharacter and cSpecialCharacter are already used. The second and third matches are a bit of a kludge to highlight '/' without it matching comment prefixes, which would then override the comment highlighting and break everything. The "display" option tells Vim that it doesn't need to look for this match if it's not going to be displayed - see :help syn-display for an explanation if you like!
Once you've defined a syntax match, you can link it to a highlight group, for example:
hi def link cUserSpecialCharacter cCharacter
This will put it in with the already defined cCharacter group, so it'll get whatever highlighting that gets - in this case, Character. You can see a nice list of highlight groups at the bottom of c.vim for examples. If you really want, you can also hardcode a highlight by doing something like:
hi cUserSpecialCharacter term=reverse ctermfg=15 ctermbg=1 guifg=#ffffff guibg=#800000
(Arbitrary example - my current highlighting for the Error group.) See :help hi for more information on this, or simply :hi to see the list of defined highlighting - plenty of examples. I'd recommend against doing this, though, since it won't change with color schemes.
Yes, you need to edit the C color theme in vimfiles/colors/c.vim I don't know all the theme options one can use, but I'm sure they are documented on http://vim.org/

Resources