Dynamic pattern for vim highlight - vim

In the vim help, there is a suggestion to use highlight groups for highlighting text greater than the textwidth:
Another example, which highlights all characters in virtual column
72 and more:
:highlight rightMargin term=bold ctermfg=blue guifg=blue
:match rightMargin /.\%>72v/
I would like this to always reflect the value of texwidth setup. Something like:
match rightMargin /%\=&textwidthv.*/
But this doesn't give me what is expected. Can you help me to parameterize OverLength with the actual value of textwitdh.
NB: I plan to put this in a filetype autocommand block , inside which, there would be a set to textwidth option and redefinition of the rightMargin highlight group.
I realize that this parameterization will not save me any lines of code, but I just want to know if this is possible at all in vim.

One way to do it:
call matchadd('rightMargin', '\%'. &tw .'v')
You should probably put this in a ftplugin (see :help ftplugin) rather than an autocmd.

Related

In vim, how to highlight a line while keeping syntax coloring?

In vim, I want to:
highlight a single line (e.g. :hi CursorLine ctermbg=black)
AND
maintain syntax highlighting
AND
not set up any custom color themes or similar
(note: adding a few lines to .vimrc is fine)
I've tried setting via :hi CursorLine ctermbg=black, but this results in changing the cursor highlight color but not maintaining syntax coloring.
not highlighted, and has syntax coloring:
highlighted, but loses syntax coloring:
in above example, I would want the string word to stay purple, if word stay yellow, etc., even though line is highlighted.
I also tried toggling :syntax off :syntax on, and not surprisingly this had no effect.
This question (Syntax highlighting in vim) seems similar to what I'm asking, but it's not because 1) I don't want to change the background, 2) I don't want to change theme, 3) it seems like OP here was having trouble with existing syntax color scheme and just wanted to be able to see things.
This question (Custom syntax coloring vim) seems similar to what I'm asking, but it's not because 1) I don't want to change existing syntax coloring, I want to keep it, 2) I don't want to add arbitrary syntax highlighting, I just want CursorLine to be highlighted while also maintaining syntax coloring.
I got my desired behavior by running :hi CursorLine ctermbg=black term=none cterm=none.
And while out of scope of my original question, running :set cursorline is also needed for the line highlighting to be displayed.
This seemed to work for me ...
:hi CursorLine cterm=NONE guifg=NONE

Highlighting the 70th character of a current line in Vim

I like to keep a strict 70 character margin whenever possible. To help with this, I want to configure vim so that the 70th character of the current line is highlighted. I understand that
set cursorline
can be used to highlight the current line. I, however, would like just the
very end of the line (the 70th character) to be highlighted. How would I go about accomplishing this?
Edit: cursorcolumn isn't what I'm looking for. I just want a single character (the 70th one on the current line).
Edit 2: perhaps a picture will help.
You can use colorcolumn to set a "right margin" bar.
This did not exist before Vim 7.3, so it is wisest to only enable it if the feature is available.
if exists('&colorcolumn')
set colorcolumn=70
endif
I prefer that this only be shown in insert mode, so I use this:
if exists('&colorcolumn')
autocmd InsertEnter * set colorcolumn=80
autocmd InsertLeave * set colorcolumn=""
endif
That will set the option when you switch to insert mode, and turn it off when you leave insert mode.
:call matchadd('Todo', '\%70c')
and if you don't want to count one tab as a single character, but you want to take into account all the spaces it takes:
:call matchadd('Todo', '\%70v')
You can use any other highlight group (for example to change color) listed by :hi instead of Todo.
:autocmd CursorMoved * exe 'match IncSearch /\%70v\%' . line(".") . 'l./'
The highlight color will be determined by your color scheme.
You can change IncSearch to any of the highlight groups, which can be found by typing:
:hi
If you are using VIM 7.3 you can set the color of a certain column with:
set colorcolumn=70

How to add extra syntax highlight rule to existing Vim syntax highlighting script?

I have a syntax highlighting script that is pretty good except I'd like to enhance it without modifying the original file. In particular, it defines an Identifier highlight group name:
:hi Identifier
Identifier xxx term=underline ctermfg=208 guifg=#FD971F
I'd like to have all words that start with a capital letter ([A-Z]) be highlighted by this. What do I have to add to my .vimrc to get this effect?
For more permanent scenarios I prefer to use the syntax commands instead of match, as suggested by #ryuichiro.
Adding something like the following to your vimrc will achieve what you ask for:
:au FileType * syntax match Identifier /\<[A-Z].*\>/
For more information check the following help page:
:help syntax.txt
matchadd should do the trick
:au BufWinEnter * let w:m1=matchadd('Identifier', '\<[A-Z].\{-}\>', -1)
:h matchadd()

Custom syntax highlighting on current buffer in vim

From time to time I would like to apply some custom extra syntax highlighting on the current buffer.
How can this be done using the build in vim syntax/highlight system (I do not want to use the Highlight plugin)
As example, I would like to highlight all assert statements in current buffer.
If the highlighting is for certain filetypes (e.g. Java) only, and you want it all the time, I'd extend the original syntax with :syn match ... definitions placed in ~/.vim/after/syntax/java.vim.
For spontaneous highlighting, use :match (or :2match), as dwalter has shown.
If you're gonna write a more elaborate mapping, maybe with toggling on/off logic, use matchadd() / matchdelete().
Finally, if you need highlighting for arbitrary words / strings, like marking up a document with a text marker, I'd suggest the comfort of a plugin like Mark (which I have taken over maintaining).
You can use match and highlight if you want.
example:
:hi MyAsserts term=bold ctermbg=Cyan
:match MyAsserts /assert(.*)/
to highlight your assert() statements with Cyan background.
:match without any arguments will reset it.
for more information on either highlight or match look at the documentation via
:help hi or :help match
To reuse your highlighting you can save those commands in a file and use :source file.vim to load it anytime you want. Another way would be to define a command inside your .vimrc.
hi MyAsserts ctermbg=Cyan
command -bar -nargs=0 HiAsserts match MyAsserts /assert(.*)/
"highlight any given regex
command -bar -nargs=1 HiIt match MyAsserts /<args>/
and call it with :HiAsserts to highlight your assert() statements or :HiIt foo to highlight every foo in your buffer.

Changing background color in vim at a certain column

I'd like to be able to highlight the wrap margin/text width in vim by changing the background color (or maybe just a line?). A lot of IDEs have this. I mocked up what I'm talking about:
Anyone know if this can be done in macvim or gvim?
Since Vim 7.3 it's possible to have columns highlighted like this:
To set it to the current textwidth:
:set cc=+1
Or you can set it to predefined value:
:set cc=80
You can change its color like this:
:hi ColorColumn ctermbg=lightgrey guibg=lightgrey
See help for more details:
:help colorcolumn
Try this:
:match ErrorMsg '\%>80v.\+'
It will highlight text beyond 80 characters, you can replace '80' with whatever wrap-width you have. However, it will only highlight the characters that exceed the width, and then only on lines that are actually longer than the width.
Check http://vim.wikia.com/wiki/Highlight_long_lines for more info, but they all pretty much accomplish the same thing.
autocmd FileType * execute "setlocal colorcolumn=" . join(range(&textwidth,250), ',')
highlight ColorColumn guibg=#303030 ctermbg=0
Big problem with this is that the colorcolumn highlighting has higher priority then hlsearch! So basically you wont be able to see highlighted search items beyond that margin...

Resources