vim: how to let vim use apostrophe as start of a string - vim

I am working on a sql file with some m4 macro embedded. Since m4 uses apostrophe to mark the start of string literals, it breaks the string highlighting of the file totally. Like
m4_include(`SQLCommon.m4')
I am wondering whether there is any option we can let vim to pair apostrophe with a single quotation mark. I searched on-line but didn't find any answer.
Thanks!

You're going to want to override vim's normal syntax highlighting for sqlString.
I was able to get the behavior I think you're looking for with just this:
:syntax region m4String start=/`/ end=/'/
To achieve nesting, we have to tell vim that a m4 string can contain more of the same:
:syntax region m4String start=/`/ end=/'/ contains=m4String extend
If you'd like to color those strings separately instead of just disrupting the normal highlighting you can separately link to the String highlight group.
:hi link m4String String
I wasn't entirely sure what you were looking for though; if the backtick-apostrophe delimited strings are inside of the normal SQL strings this won't work.

Related

Vim: use different color for words with a pattern

Am a hardware engineer and I use Embedded Ruby Language to simplify writing my hardware verilog/system verilog code. In my *.sv and *.v files, i have a lot of ERB variables starting with "__" (double underscore). E.g. <% __MEM_DEPTH = 64 %>. Is there any way by which I can make vim display the words starting with the double underscore in a different color?
You can extend the built-in syntax highlighting. For example, put the following into ~/.vim/after/syntax/verilog.vim:
syntax match verilogErbVar "\<__\w\+\>"
hi link verilogErbVar Identifier
This assumes that the corresponding text fragments aren't yet matched by the original syntax (in my short test, they weren't). Else, you need to find the syntax groups that contain them and add a containedin=... to the :syntax command.
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.

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.

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'

VIM: How to change the Showbreak Highlight color without using the NonText Color-element

I noted that the 'showbreak' symbol is highlighted with the highlight "NonText" color-element. NonText is also used for the EOL Characters.
I would like to keep the highlight-color for the EOL characters but want to change it for the showbreak symbol is that possible?
Another problem is that my showbreak symbol is not displayed.
I would like to use this symbol "↳" and put it in the linenumbers column (using set cpoptions+=n). I can't find out how to display the symbol and how to put a space after the showbreak symbol (between the text and the symbol).
Can anyone help me?
I don't think you're going to get highlighting to be different than the EOL character, at least I am not aware of a way to do that.
For the second part I can help with. I was able to get "↳ " to show up in my line number column with the following settings:
let &showbreak = '↳ '
set wrap
set cpo=n
Note that there is a space after the ↳. This lines up nice until you have > 9 lines in the file. If you wanted it to line up with the last character of the number column regardless of the number of lines I'm not sure what you're going to have to do.
Edit: I've recently written a proof-of-concept function for someone on IRC that highlights the first character on a line that has been wrapped with a different highlight group. It hasn't been tested much but it seems to work. Not exactly what you're looking for but maybe it's worth a look.
:help hl-NonText makes it pretty clear that you cannot have different colors for the 'showbreak' string and other non-text strings, of which eol is a member (see :help 'listchars'):
NonText
'~' and '#' at the end of the window, characters from 'showbreak' and
other characters that do not really exist in the text (e.g., ">"
displayed when a double-wide character doesn't fit at the end of the
line).
If you're willing to accept this limitation (#elliottcable) hi! link NonText LineNr will match the 'showbreak' string to the line number colors.
If you really wanted to get clever, as a compromise you could create a mapping or command to toggle between ':set list' and ':set nolist' that would also adjust the NonText highlight setting simultaneously.
If you use :set relativenumber (added in vim 7.3), :set showbreak=↳\ \ \ will reliably keep your 'showbreak' neatly lined up since the number width will not change as you navigate through the file. (This in addition to the :set cpo+=n and :set wrap #Randy Morris mentioned in his answer.)
You'll definitely need UTF-8 for the ↳ character, since it does not appear in other encodings. I'd strongly recommend you carefully document your encoding problems, with details about how to reproduce them along with your OS, its version, and the :version output of vim, and post them as separate questions. UTF-8 should be helping you wrangle multiple languages rather than being an impediment.

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