Vim: need help with a tiny script code to highlight - vim

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.*\]/

Related

Highlight keyword only on specific lines

Sometimes I just want to highlight the keyword on specific lines. Since the keyword is really common, it can appear everywhere in the file.
For instance, I'm searching for 1s on lines starting with check in the following file:
...
[block-1]
test 31 for instruction block1_test
stim 011000011100101
check xxxxx1xxxx1xxxx
...
Using /1 will highlight every 1, and makes it a bit annoying to find the one I want.
Though using /^check\>.*1 narrowers the search result, it matchs from the very beginning to the last 1 on that line.
I'd like to make all the 1s on check lines clearer to see, to find. Can I achieve this?
You can use a look-behind operator \#<= in your pattern to match 1 but only in lines that start with check.
This search pattern does what you requested:
/\(^check\>.*\)\#<=1
Using "very magic" option :h magic:
/\v(^check .*)#<=1

highlight sub-match in vim

I'm trying to figure out how to highlight a specific portion of a match in vim.
Given the following example rule (taken from the coffeescript syntax file source):
syn match coffeeExtendedOp /\%(\S\s*\)\#<=[+\-*/%&|\^=!<>?.]\+\|[-=]>\|--\|++\|:/ display
This regular expression matches various coffeescript operators. The operators are highlighted (in my vimrc) like this:
hi Operator guifg=#ff0000
For example, since coffeeExtendedOp is linked to coffeeOperator which is linked to Operator, in the above source file. This all works, but I'm wondering how to specifically highlight the ++ operator matched in the above syn match with a different color, say blue, within my vimrc (that is, without altering the original source file above). I'm simply wondering if this is possible.
EDIT: I think the rules are placed under a cluster, so perhaps that's why it's not affecting anything. Is there a way to access the rule within the cluster?
EDIT: Question was clarified.
Solution:
syn match plusplus /++/ contained containedin=coffeeExtendedOp display
hi plusplus guifg=#0000ff
The problem now is that this only works when I run them as commands in vim, but not when I put it in my vimrc file. Any ideas? Could it be that the stuff is hidden behind the cluster? But then why is it visible in vim through a command? I tried including the syntax file but it didn't seem to have any effect.
Looking at the coffee.vim you linked to it seems like the dot belongs to the coffeeDotAccess syntax item. So you can highlight it just by doing this:
:hi coffeeDotAccess ctermfg=blue
I'm going to guess a bit at what you need. (I don't speak Coffeescript and your sample regex is way too complicated for me to start reading at the moment).
Transparent syntax items
You could have a look at transparent syntax rules: (http://vimdoc.sourceforge.net/htmldoc/usr_44.html)
In a C language file you would like to highlight the () text after a "while"
differently from the () text after a "for". In both of these there can be
nested () items, which should be highlighted in the same way. You must make
sure the () highlighting stops at the matching ). This is one way to do this:
:syntax region cWhile matchgroup=cWhile start=/while\s*(/ end=/)/
\ contains=cCondNest
:syntax region cFor matchgroup=cFor start=/for\s*(/ end=/)/
\ contains=cCondNest
:syntax region cCondNest start=/(/ end=/)/ contained transparent
Partial matches in regex
If you really just meant highlighting submatches, have a look at the the
\zs start match
\ze end match
In short,
:match Error /foo\zsbar\zered/
would highlight only 'bar' in 'foobarred'

Delete anything other than pattern

Let's say this is my text:
this is my text this
is my text this is my text
my text is this
I would like to highlight all text except pattern and delete the highlighted text.
p.e. text: this must be the result.
text
texttext
text
I've found the code how to select all text except pattern:
\%(\%(.{-}\)\#!text\zs\)*
however I don't know how to delete all highlighted text.
This doesn't work:
:%s/\%(\%(.{-}\)\#!bell\zs\)*//
Can anyone help me?
Try this:
:%s/\(^\|\(text\)\#<=\).\{-}\($\|text\)\#=//g
Explanation:
\(^\|\(text\)\#<=\) # means start of line, or some point preceded by “text”
.\{-} # as few characters as possible
\($\|text\)\#= # without globbing characters, checking that we reached either end of line or occurrence of “text”.
Another way to do it:
Create a function that count matches of a pattern in a string (see :help match() to help you design that)
Use: :%s/.*/\=repeat('text', matchcount('text', submatch(0)))
Forgive me, because I'm not a vim expert, but wouldn't prepending the search with v find the inverse so that you could do something like this?
:v/pattern/d
I've implemented Benoit's clever regular expression as a custom :DeleteExcept command in my PatternsOnText plugin. It offers other related commands like :SubstituteExcept or :SubstituteInSearch, too.
OP's example would be
:%DeleteExcept /text/
Comparing that with #Benoit's explicit command (:%s/\(^\|\(text\)\#<=\).\{-}\($\|text\)\#=//g), it's a lot simpler.

vim custom syntax highlighting

I'm wanting to create custom syntax highlighting in vim for a task-list.
Task items begin with a hyphen. Two types of task items are relevant: (a) items without an '#done' tag. (b) items with an #done tag. (a) and (b) need to be highlighted differently.
I'm using taskpaper, which works fine, but the issue is, I'm trying to make this to work for task items that span multiple lines. For example:
- Regular item (works)
- Completed item #done (works)
- Multi-line item. This item continues on to
the line below. (doesn't work)
- Multi-line completed item. This item continues
on to the line below. (doesn't work). #done
The highlighting file at taskpaper works for the first two, but not for the second two. As a workaround hack, I tried this for the last case above:
syn region multLineDoneItem start="{" end="}" fold
HiLink multLineDoneItem NonText
But now, I'm forced to mark multi-line done items with braces like so:
- {Multi-line completed item. This item continues
on to the line below. (workaround works).}
I've already searched stackexchange and elsewhere. I would appreciate any help! :)
You could try using the \ze regex atom in the end part of your syntax region. This would allow you to match everything up to but not including the next task. I haven't looked at how you do matching but something like this might work.
syn region muiltLineItem start="^-" end="\(\s*\n)\+\ze^-" fold
syn region multiLineDoneItem start="^-" end="#done\s*\n\(\s*\n\)*\ze^-" fold
HiLink multiLineItem Normal
HiLink multiLineDoneItem NonText
I haven't tested this at all but I think it, or something like it, should work. If you wish to take indentation into account the \z regex atom will allow you to keep matching lines with the same indent.
UPDATE:
Try this:
syn match multilineItem "^-\_.\{-}\ze\(\n-\|\%$\)" fold
syn match multilineDoneItem "^-\(\%(\_^-\)\#!\_.\)\{-}#done\s*\n\ze" fold
command -nargs=+ HiLink highlight default link <args>
HiLink multilineItem Normal
HiLink multilineDoneItem NonText
delcommand HiLink
Oh, also this should work for all four cases and not just the multi-line items.

Possible to highlight matching quotes in vim?

With the syntax highlighting in vim, I get the handy feature where the matching paren or bracket will be highlighted when I put the cursor over it. Is it possible to do the same thing for quotes?
While not eloquent, one workaround is to select everything inside of matching quotes. You can do this by using the command:
vi"
This will select everything in-between the quotes. However, you won't get proper results with nested quotes as it will match the first found ".
The problem with quotes is that they are symmetrical. It would be very hard to determine which quotes belong with each other.
For instance: "Which \"quotes\" go with each other in this statement?"
This has been discussed on the vim mailing lists a few times, as well as in the bug trackers of a few of the auto-delimiter type plugins. In every case that I've seen, it's been decided that this is better left as is.
The solution is here: Stackoverflow in matchquote except it has the unfortunate limitation that only the current line is considered.
matchit seems to comes close by allowing defining of multi-line matches of words such as if/endif but still no multi-line possibility that I can figure out to get matching for " and '.
VIM already highlights quoted text in a different color, so you can easily identify strings. Do you really need it to match quotes when the whole string is already highlighted?
From :h matchparen
The characters to be matched come from the 'matchpairs' option. You
can change the value to highlight different matches. Note that not
everything is possible. For example, you can't highlight single or
double quotes, because the start and end are equal.

Resources