I am using MacVim with the Cobalt theme. I found it very nice, however, coming from Sublime Text, I feel there aren't enough different colours which makes my javscript code hard to read.
For example, I'd like the function name to be coloured to make them stand out a bit more:
myClass.prototype.myFunction = function myFunction() {
// here, I'd like "myClass" to have a different color from the text
// same for "prototype" and "myFunction"
}
Another example is the use of methods:
myArray.pop();
// I'd like to change the color of ".pop()" for more visibility
How can I add these types of patterns?
A syntax script parses the programming language into different groups (which can be listed via :syntax list). A colorscheme then prescribes how to color and format each individual group.
So, if there are distinct groups, but your colorscheme just assigns the same color to it, that can be easily changed by putting
:hi link <syntaxGroup> <highlightGroup>
commands into your ~/.vimrc.
The detail of parsing depends on the language and syntax script. Extending an existing syntax (to parse out more details) is possible, but complex. For JavaScript, there exist some alternatives (like this) to the built-in syntax script; you might want to give those a try.
PS: :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.
I use the plugin vim-javascript-syntax.
Related
I'm new to vim syntax highlight customizations. Trying to create a text based call path description file:
// Entry point
Class1#mainMethod
Class1#privateMethod2
Class2#method3
Class3#method4
Class4#method5
In plantUML the equivalent would be:
ExternalActor -> Class1 : mainMethod
Class1 -> Class1 : privateMethod2()
Class1 -> Class2 : method3()
Class2 -> Class3 : method4()
Class1 -> Class4 : method5()
I already have some decent syntax file in place that makes it look like:
current syntax file looks like:
syn keyword celTodo contained TODO FIXME XXX NOTE
syn match celComment "//.*$" oneline contains=celTodo
hi celComment ctermfg=yellow
hi celTodo ctermfg=green
syn match methodCall /\(#\)\#<=\w*/ contained oneline
hi methodCall ctermfg=blue
syn match className /\w*\(#\)\#=/ contained oneline
hi className ctermfg=red
syn region line start='\(\.\)\#<=\w' end='.\($\)\#=' oneline fold transparent contains=celComment,className,methodCall
Problem
I think a different background color for each occurrence of the same class would help understand the sequence better.
Is there any way to achieve this? So that Class1 will have a different background color than Class2, 3, and 4. But each class always the same, consistent color.
Syntax highlighting associates a keyword, pattern match, or region with a syntax group. A corresponding highlight group then (directly or indirectly through linked groups) determines the color and formatting of the text.
In order to have different (background or otherwise) colors for each class name, you'd have to define different syntax groups, and assign different highlight groups, too. Your syntax file would not only have a fixed set of :syntax match commands, but also a loop that extracts matches from the current buffer and builds the corresponding :syntax match and :highlight command as the syntax loads (using :execute).
Then you have the problem of updating, if the user adds or changes class names. Normally, a syntax is static, so once it loads, it's done. In your case, you'd have to define :autocmds that periodically re-scan the buffer, and add new class names (and maybe even recycle unused highlight group names, so you don't run out of colors). The CursorHold event would be a good candidate for it, but there will be a delay until the colors show up. The available colors is another problem if you want to make this syntax available to other users. The number of colors can vary wildly, and coming up with background colors that work well with various colorschemes is difficult.
Summary
It is possible, but it would be unusual for a syntax, and have side effects like delays in updating or poor performance. (I've seen this used for highlighting function names from the tags file, though.) Some users definitely would want to turn this off.
Alternative
For small files with few (or very distinct) class names, this additional highlighting probably isn't necessary. For large files with many classes, having everything light up would make it appear like a Christmas tree, and all the colors could be more distracting than helpful. I'd rather leave it to the user to do such highlighting of some classes of interest, on demand. My Mark plugin provides the generic functionality for this, in a way that does not interfere with syntax highlighting, and it ships with color palettes that look like text marker highlightings. I use this often to have better orientation in log files or legacy code bases. (The plugin page has links to alternative plugins; there are a few.)
Your syntax
Group names typically have a common prefix that's identical to the name of your syntax. If that is cel, use celMethodCall instead of methodCall, and so on.
I would put the :hi commands all at the bottom; most syntax plugins do it like this.
Especially if you intend to later share the syntax, favor :hi linking to existing syntax groups (:help highlight-groups) over defining your own colors. Even for your personal use, defining your colors in your ~/.vimrc has the benefit of having a single place to adapt and reuse it, instead of hunting around various syntax scripts.
By using :hi def, users can customize the syntax, e.g. in their ~/.vimrc. :help 44.12 has more information on writing syntax plugins.
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.
Spell check in vim is nice enough to not highlight things inside of a math environment when writing LaTeX. This is convenient, but I make some very common typos like "theat" instead of "theta" that would be nice to catch automatically. Is there a way to get the words in the explicit list of 'bad words' to be highlighted regardless of their context?
Note that I still don't want to check whether everything is good within a math environment, just that it is not explicitly bad.
To enable spell checking for a syntax group, you'd have to add contains=#Spell, e.g. for the texStatement group, that would be:
:syn match texStatement "\\\a\+" contains=#Spell
But with that, you'd still have to add all "good" statements to your spell file. If you just want to highlight certain "bad" words, you can define a contained match:
:syn match texBadStatement "theat" containedin=texStatement
And then link to the error or bad spell highlighting:
:hi link texBadStatement SpellBad
Put those commands into ~/.vim/after/syntax/tex.vim to make them persistent.
I'm not a Latex specialist. If the texStatement group is wrong, 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.
I'm adding i18n to an existing project (web application). This involves replacing every bit of static text with calls to an i18n library. It would be convenient to be able to search for this text rather than rely on syntax highlighting to identify it visually.
In vim, is it possible to search within a file for occurrences of a certain highlighting type?
Something like:
/[%type=Boolean]
Sub 'Boolean' with 'Comment', 'htmlTag', or any group defined in your syntax highlighting file.
This plugin will do it for you
Take a look at the answer I gave here:
Vim search in C/C++ code lines
I think my :SearchInside command will do what you want.
I have highlighted all Symbols by using tags file & highlight option.
But I could not able to highlight my local variables.
I have an idea, that is, VIM already supports autocompletion of keywords for a current file, it does autocompletion of my local variable, so, if I get a list of keywords for my current file then I will highlight those keywords by using "highlight" vim command.
But problems is, I don't know, how to get a list of keywords for a current file.
You can highlight recognised names using the tags file as long as the tags file is generated with the --c-kinds=+l to ensure that it includes local variables. However, there is currently no realistic way to identify the scope of those variables (ctags does not provide much information), therefore Vim will not distinguish between variables in one function and another:
void main(void)
{
int MyVariable; // Highlighted
}
int MyFunction(void)
{
int MyFunctionVariable; // Highlighted
MyVariable = 1; // Syntax error, but still highlighted
}
It could be done by parsing the C file in a little more detail and creating syntax regions for each function, but it is far from easy (and it would be incompatible with plugins like rainbow.vim as Vim doesn't support overlapping regions).
On a related note, you may also be interested in my tag highlighting plugin available here. It will highlight local variables (if b:TypesFileIncludeLocals is set to 1 in the buffer open when running :UpdateTypesFile), but it doesn't deal with the scope of local variables. It does, however offer a lot more highlighting colour variations than the highlighting suggested in :help tag-highlight. Note that your colour scheme will have to have highlights defined for lots of extra groups (e.g. GlobalVariable, LocalVariable, DefinedName etc) to take full advantage of it.