Vim match weird behaviour - vim

I¡ve setup the following match to highlight TODOs in my code:
hi todo guifg=#b294bb
match todo /TODO:?/
But the rule doesn't match the optionsl colon in TODO: (CoffeeScript file):
I've tried some Regex variations, like escaping the colon or wrapping it with parens.
The weird thing is that it works differently in some filetypes, for example:
In my screenshot (CoffeeScript) it doesn't matches the colon.
In LESS, it doesn't match anything.
In vim file, it matches both TODO and the colon.
What am I missing? Is something in my language syntax files overriding this rule?
Notice that I'm using AfterColors plugin to create this colours customisations.

The question mark matches a literal question mark in vim regex (by default). If you set the mode to very magic with \v you will get the behavior you are expecting. (Or use \?)
match todo /\v\CTODO:?/
It is recommended to set the magic mode and case sensitivity explicitly when using regex in scripts since they are affected by user settings.
Read :h magic and :h \C

The "todo" group already exists (and matches /TODO/), try using another name.

Related

Can not find the good syntax for a vim sustitute pattern

I have a bunch of t_ prefixed fonction names in a header file and i would like to remove this prefix from some of them.
I have ended by typing :
:s/\(\s+\)t_\([^(]+\)(/\1\2(/c
But vim complains with Pattern not found
What is wrong in my pattern ?
You forgot to put a backslash before + to give it its quantifier meaning. You can also probably simplify it using \< to match the start of a word instead of capturing spaces.
I suggest breaking the problem down, first the search:
/\v\s+\zst_\ze.*\(
Now the substitution:
:s///\c
We can reuse a search pattern simply typing an empty search on the substitution
OBS: I suppose your functions have () at the end of the line, so .*().
Another thing; \zs and \ze to match only t_, for more see: :h \zs.
If you are using neovim you can see what happens befor hitting enter, just put these lines on your init.vim:
if has("nvim")
set inccommand=nosplit
endif

problem in jumping to system verilog macros `define in VIM using ctags

I checked following things, tags file path is set properly.
the macro tag i am looking for exist in the tags file. task/function/parameters etc works without hiccups. i have problem only with regex.
the expression present in ctags file are
1. --regex-SystemVerilog=/^\s*`define\b\s*(\w+)/`\1/d,define/
2. --regex-systemverilog=/^[ \t]*`define[ \t]*([a-zA-Z_0-9]+)/`\1/d,define/
both the options does not work.
When :tag `altuvm_test_arg works, but the corresponding CTRL-] key doesn't, this often is caused by incompatible 'iskeyword' definitions, so that actually gets passed to :tag is altuvm_test_arg (without the leading backtick).
You can check the scope of keyword matching (when on such a tag) with the * command, which highlights the same keyword under the cursor. If backticks are excluded, try adding the backtick to the range of keyword characters:
:setlocal iskeyword+=`
If this helps (and doesn't break things like syntax highlighting, or annoys you with the changed navigation of commands like w and e), you can make that setting permanent for verilog filetypes put putting it in ~/.vim/after/ftplugin/verilog.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.

In vim, how do I highlight TODO: and FIXME:?

In vim, FIXME and TODO are highlighted, but I can't get FIXME: and TODO: (note the colon after the keyword) to highlight? What should I put in my .vimrc to make this happen?
Well, you've already found the problem, but here's the why.
There are three basic types of syntax matching: keywords, matches, and regions. Keywords are fixed strings, generally used for basic language keywords (int, double, ...) and also, in your case, for the FIXME and TODO. I really do mean fixed strings; they have to be exact and whole words, unlike matches and regions, which use regex. For example, from the C syntax:
syn keyword cTodo contained TODO FIXME XXX
It looks like that in pretty much all built-in syntax definitions, just with different group names (cTodo).
iskeyword tells vim whether a given character can be part of keyword. By default, it does not include colons, so when looking for keywords, vim sees "FIXME:" as "FIXME", and ignores the colon. If you tack on the colon (set iskeyword+=:), you can now define an extra bit of highlighting:
syn keyword myTodo contained TODO: FIXME:
It's up to you how you want to work it into the existing syntax/highlight groups. If it's for just one filetype, you could add it to that syntax's todo group (e.g. cTodo). If you want it everywhere, you can do "myTodo" as I suggested, then link it straight to the Todo highlighting group (hi def link myTodo Todo).
Alternatively, you can leave iskeyword alone (I'd probably recommend this), and simply use a match:
syn match myTodo contained "\<\(TODO\|FIXME\):"
hi def link myTodo Todo
augroup vimrc_todo
au!
au Syntax * syn match MyTodo /\v<(FIXME|NOTE|TODO|OPTIMIZE|XXX):/
\ containedin=.*Comment,vimCommentTitle
augroup END
hi def link MyTodo Todo
The containedin will add it to all groups ending in "Comment", plus
vimCommentTitle, where " TODO: foo would not get highlighted as MyTodo otherwise.
If you make your own environment, make syntax file (not .vimrc)
global syntax file is located vim directory (ex.
/usr/share/vim/vim72/syntax/c.vim)
and if you make ~/.vim/syntax/c.vim, then you can add syntax for your
own. (override)
Just add additional syntax in that file. (the way #Jefromi does)

Vim: need help with a tiny script code to highlight

I need a script code to highlight "[Capítulo" and "]" and everything between them. Thank you.
I want it to work everytime I open , for example, a .txt file. Just like code highlighting.
Here's an easy way to do it:
in vim, make sure syntax highlighting is on with :syn on
run the command :highlight to get a listing of all the highlight group names, and samples of what they look like. The Error group looks like it stands out well in my colorscheme, so I'll use that in my example (but you can use any of the other names, like Todo or Search)
:syntax match Error /\[Capítulo[^\]]*\]/
This pattern will keep you from greedily matching the largest chunk. Even though other people are suggesting you use the regular expression /\[Capítulo.*\]/ - it's probably not what you want, because it will match everything in between if there are two or more such patterns on a line.
For example /\[Capítulo.*\]/ will match this entire line:
[Capítulo foo] these words should not be highlighted [Capítulo bar]
The same example but with /\[Capítulo[^\]]*\]/ will only match stuff inside []:
[Capítulo foo] these words should not be highlighted [Capítulo bar]
With regular expressions, it's a common trick to make a group that matches everything but the character that you want to end your match, instead of using the .* which will match as many characters as it can. In this case, we make the group [^\]]* - which says "match everything except ]."
If this works the way you want it to, add the syntax match line without the ":" to your .vimrc
A regular expression is what you're looking for:
Type / to enter in "search mode" and type:
\[Capítulo.*\]/

Resources