Vim improperly highlights "very magic" and "very nomagic" regular expressions - vim

I'm editing a .vim syntax file in vim itself, and I'm using "very magic" and "very nomagic" regexes, simply because I think they make more sense. Unfortunately, vim does not in any way highlight these correctly. For example, this regex, using very nomagic:
/\V[/
should match an open bracket. Unfortunately, vim thinks this is an unterminated collection, and so it floods the file with purple highlighting until it finds the next ].
Is there any way to get vim to properly highlight these regular expression modes?

You'd have to override some of the default syntax/vim.vim syntax definitions (in ~/.vim/after/syntax/vim.vim), because the syntax doesn't consider this case, and assumes the default 'magic' regular expression syntax.
I guess the author of this syntax, Chip Campbell, didn't want to further complicate the (already quite large) syntax, and avoid the increased testing effort. It's certainly a good idea to ask him about including support for the \V / \M atoms, or whether he at least would accept a patch to include such an enhancement.

Related

Why does this regular expression not work in Vim syntax highlighting?

I am attempting to create my own Python syntax highlighting file for Vim.
I'm trying to highlight the class inheritance object and the regex I've created
works in various regex testers, but doesn;t work in Vim. I've read that Vim's regex is close to Perl style
so that is what I've been using.
I'm trying to highlight the word 'Subscribers' in the following text:
class Divisions(Subscribers):
The regex I've composed is:
(?!:class\s\w+)(?<=\()\w+(?=\):)
I'll be honest here, I stumbled into this while I was struggling to make a negative lookbehind work with quantifiers,
which I now understand isn't possible. I was experimenting with the non-capturing group (?:class\s\w+) and accidentally
inserted the exclamation mark which 'magically' solved the problem. At least in the multiple regex testers I was using.
Just for clarity, then follows a look behind (?<=() to caputre but not include the '('
and then a look ahead after the word (?=):) to capture but not include the closing '):'
I've added it to my Vim syntax file as:
syn match pythonClassInherit "(?!:class\s\w+)(?<=\()\w+(?=\):)"
Is this a valid regular expression in Vim? If not, can anybody offer a working solution for Vim?
EDIT: I realized that I've had overcomplicated the issue; you just need a right Vim regex. Try
syntax match pythonClassInherit "\%(class\s\+\h\w*\s*(\s*\)\#<=\h\w*\%(\s*):\)\#="
highlight link pythonClassInherit pythonImport
Then you'll see the result:
You may replace pythonImport by another predefined highlight group.
For highlighting, I found https://learnvimscriptthehardway.stevelosh.com/ (highlighting starts from Chap. 45) helpful. As for regex, romainl's suggestion is awesome.
My first attempt was to use regex \zs. This, unlike \#<=, requires that the syntax group cover the part that comes before \zs, which cannot be satisfied because some other groups have already occupied that part. This led me a complicated solution, by overriding the default highlight groups to include a new group (for Subscribers). But it turns out we really don't have to.
If interested, see Vim syntax file not matching with \zs.
Online regular expression playgrounds don't support Vim's syntax so using them is pointless, here.
See vimregex.com for an overview, :help usr_27 for a gentle tutorial, :help pattern for the definitive reference, and :help perl-patterns for the differences between the Vim syntax and the Perl syntax.

Setting Vi abbreviation that includes special character

How to set the abbreviation for a specific word that contains special character like ":,;'." etc., for instance, I want to set an abbreviation for std::map to std::map
:ab std::map std::map<string,int>
is not working
The abbreviation trigger text has restrictions: it has to be of either full-id, end-id, or non-id type (basically, which characters are keywords; see :help abbreviations for details).
For the C / C++ filetypes, the colon : normally is not part of 'iskeyword', that's causing the E474 error. As it's not recommended to mess with that setting (it affects navigation, syntax highlighting, etc.), you have to drop / replace it from the abbreviation:
:ab stdmap std::map<string,int>
The solution I proposed in Using backslashes in vim abbreviations should be adaptable to your case. You will have to adapt the test getline('.')[col('.')-2]=='\' to something like getline('.')[:col('.')-2] =~ '.*std::$' (untested)
But, honestly, you should consider using a snippet engine instead, there are plenty.

syntax highlighting for Assembler

I need to add support for assembler language I'm working with (it is not x86, 68K, or 8051 which are well supported by vim). I looked at the existing syntax files, and here are my questions
1) When does it really make sense to use syn keyword and syn match? My understanding is that the latter supports regex and gives more flexibility. On the other way, looking at /usr/share/vim/vim70/syntax/asmh8300.vim - they define opcodes in both keyword and match, what benefit does it really give?
2) Instructions in my Asm have a common format:
INSTR OP1, OP2 ..; i.e. space delimits the instruction name from operands.
I think for this I'm ok with only defining all Asm commands in 'keyword' since space symbol is by default in 'iskeyword'. Am I right ?
3) The Asm also supports C-style structures, enums and comments. Can I just borrow its syntax definition from c.vim or it won't work and requires some tweaking?
If you have a limited set of identifiers, and if they all consist solely of keyword characters, then :syn keyword is the best choice. You're right in that :syn match provides a superset of functionality. Essentially,
:syn keyword myGroup foobar
is equivalent to
:syn match myGroup "\<foobar\>"
Beware of old versions
The syntax/asmh8300.vim syntax you've referenced is from 2002, it may not be the best example of how to write a syntax file. (For example, it omits the \<...\> around its matches, what looks like a bug to me. And it still has compatibility stuff for Vim 5 / 6 that's not needed any more.)
Also, do you actually use Vim 7.0?! Vim 7.0 is from 2007 and very outdated. It should be possible to install the latest version 7.3; if you can't find a proper package for your distribution (for Windows, check the binaries from the Cream project, it's also not very difficult to compile (e.g. from the Mercurial sources) on Linux.
Borrow other syntax elements
If other syntaxes are embedded in your syntax, and clearly delimited (e.g. like JavaScript inside HTML), you can :syn include it. But if there are just similar constructs, it's best to copy-and-paste them into your syntax (and adapt at least the group names). You need to be careful to catch all contained syntax groups, too; together with syntax clusters, the hierarchy can be quite complex!
More tips
When writing a syntax, you often 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.

Vim smart tabs inside an if statement

I'm using Vim's SmartTabs plugin to alingn C code with tabs up to the indentation level, then spaces for alignment after that. It works great for things like
void fn(int a,
________int b) {
--->...
Tabs are --->, spaces are _. But it doesn't seem to work so well for cases like
--->if(some_variable >
--->--->some_other_variable) {
--->...
In the case above, Vim inserts tabs on the second line inside the parentheses. Is there a way I can modify what Vim sees as a continuation line to include cases like this, so I get:
--->if(some_variable >
--->___some_other_variable) {
--->...
If there's an indentation style that would both allow flexible indentation width according to one's preferences, and consistent alignment, your suggested scheme would be it. Unfortunately, this style requires some basic understanding of the underlying syntax (e.g. whether some_other_variable is part of the line-broken conditional (→ Spaces) or a function call within the conditional (→ Tab)), and this makes implementing it difficult.
I'm not aware of any existing Vim plugin. The 'copyindent' and 'preserveindent' options help a bit, but essentially you have to explicitly insert the non-indent with Space yourself (and probably :set list to verify).
I don't know about that other Editor, but the situation is similar for most other inferior code editors. Without good automatic support, this otherwise elegant style will have a hard time gaining acceptance. I would love to see such a plugin for Vim.

Vim syntax highlighting: overlapping regex

I try to add some syntax highlighting for javascript to vim, but I keep running into one problem: when characters are already highlighted, they seem to be completely ignored by all other regular expressions.
For example, I tried to add syntax highlighting for the argument list of a function. While creating the right rexex I disabled the syntax highlighting for the function keyword, such that it was easier to see what my regex did. I ended up with the following (working) regex:
syn match javaScriptArguments "[(=\:\s,]function.\{-}(\zs.\{-}\ze)"
However, as soon as I enabled the highlighting for the function keyword again, this line doesn't work anymore. It seems that vim simply excludes everything which is already highlighted, and thus it won't find any matches for the regex above, even though it won't result in characters being highlighted twice.
How can I solve/work around this problem?
Syntax definitions must be contained for them to match inside other syntax items. Find all the gruesome details at :help syn-contains.
In your case, you're relying on a look-before of the "function" keyword via \zs. In my experience, that's bound to cause problems, but may turn out to be unneccessary once you use contained. In general, it is difficult to extend an existing syntax definition without modifying the original script (which I suppose is what you're intending to do). Have a look at :help :syn-containedin and :help :syn-nextgroup.

Resources