I would like to highlight overlong lines in Vim (like here: https://stackoverflow.com/a/235970/1329844) as well as trailing whitespace (like here: https://stackoverflow.com/a/4617156/1329844). However, whenever I use both highlights, only the last one is applied.
I have the following code in my .vimrc:
highlight OverLength ctermbg=0 ctermfg=197
match OverLength /\%>80v.\+/
highlight ExtraWhitespace ctermbg=0
match ExtraWhitespace /\s\+$/
When I open a file, only trailing whitespace is highlighted. If I exchange the order of the two highlight/match pairs, only overlength lines are highlighted. What do I need to change so that both patterns are matched and highlighted?
The :match command can only have one active pattern. If both of your highlights used the same colors, you could combine the patterns with \|. Here, you have to use one of the two alternative commands: either :2match or :3match, or you can use the (newer) matchadd() function, where you can specify arbitrary numbers (> 3) as the (last) {id} argument.
:call matchadd('OverLength', '\%>80v.\+', 10, 4)
:call matchadd('ExtraWhitespace', '\s\+$', 10, 5)
I think, Ingos solution is to be prefered, but nevertheless, you can use this:
:match MyCustomHighlight /\%(\s\+$\)\|\(\%>30v.\+\)/
:highlight MyCustomHighlight ctermbg=0 ctermfg=197
Related
When I use } and {, vimtex+vim jumps somewhat randomly in the document, skipping several empty lines. See below.
How to restore the default vim behaviour not to skip the empty lines?
Find if there are any mappings for } using
:map }
Now, you can unmap any mappings you find with
:unmap }
If you wanna put this in your .vimrc, I'd suggest doing it in the after directory for that plugin.
like ~/.vim/after/plugin
Short answer
The fact that the skipping happens pretty randomly indicates that the empty lines are not really empty. They contain a whitespace or other special characters.
Move the cursor to these 'empty' lines and press $ to see if they are really empty.
How to avoid such problems:
(Yes, others had your problem too.)
Make vim show whitespace characters
Vim has a way to show these characters. Use set list and define listchars. From the vim help for listchars:
Strings to use in 'list' mode and for the :list command. It is a
comma separated list of string settings.
[...]
lcs-space
space:c Character to show for a space. When omitted, spaces
are left blank.
lcs-trail
trail:c Character to show for trailing spaces. When omitted,
trailing spaces are blank. Overrides the "space"
setting for trailing spaces.
See :help :list and :help listchars for more information.
Highlight trailing whitespace
I find it quite annoying to always have a character displayed for any space and my eyes are too bad to see a little dot at the end of a line (for trailing spaces). Therefore I use a highlight group to show all trailing whitespace characters:
" Show trailing whitespace
highlight ExtraWhitespace ctermbg=red " define highlight group
match ExtraWhitespace /\s\+$/ " define pattern
autocmd BufWinEnter * match ExtraWhitespace /\s\+$/ " apply match to all buffers
autocmd InsertEnter * match ExtraWhitespace /\s\+\%#\#<!$/ " don't match while in insert
autocmd InsertLeave * match ExtraWhitespace /\s\+$/
autocmd BufWinLeave * call clearmatches() " It's good for the RAM
Auto-remove trailing whitespace
There is also a way to automagically delete those characters when writing the buffer -- but there are some caveats (think of markdown's double trailing whitespace for line breaks).
Vim wiki has a good explanation.
The simplest (but maybe not the best) solution is to add
autocmd BufWritePre * %s/\s\+$//e
to your .vimrc or to the corresponding ftplugin files.
I personally have a function in my vimrc and disable it for file types, where I don't want/need it.
You may also be interested in Make { and } ignore lines containing only whitespace
Disclaimer
There might be characters that are not whitespace, but are also not shown by vim by default. I never had this problem, but what I said under 'Short Answer' still does apply.
There are plugins that show marks, e.g. vim-signature and ShowMarks, but they only show signs at sidebar.
Is there a way to highlight all lines that have marks?
Currently I'm using an expression to do this, for example:
:highlight currawong ctermbg=darkred guibg=darkred
:match currawong /\%12l\|\%34l\|\%5l/
This highlights line 12, 34 and 5.
It's working, but not very convenience.
Please use this command:
:highlight currawong ctermbg=darkred guibg=darkred
:match currawong /\v.*(%'a|%'b|%'c|%'d).*/
:nmap <F5> :redraw!<CR>
It will highlight lines which contains marks: a-d.
Type F5 to force a redraw.
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.
Is it possible to set alternating (one color for lines with odd line number and another for even line numbers) highlighting colors for each row in Vim?
This can do what you want with text background colors:
syn match Oddlines "^.*$" contains=ALL nextgroup=Evenlines skipnl
syn match Evenlines "^.*$" contains=ALL nextgroup=Oddlines skipnl
hi Oddlines ctermbg=yellow guibg=#FFFF99
hi Evenlines ctermbg=magenta guibg=#FFCCFF
Just add this to .vimrc or the right file type .rc you want.
Since this utilizes the syntax functionality, it only applies to matchable typed text. I don't know if there's a way to alternate the background color of the empty "space" after text that hi Normal ctermgb=darkblue guibg=darkblue does.
There's nothing built-in, so you would have to emulate that (and suffer from the consequences like slow performance, bad interference, etc.) A candidate would be :match / :call matchadd(), because that is independent of syntax highlighting. Demo:
hi Alternate guibg=LightGrey guifg=NONE
execute 'match Alternate /\%(' . join(map(range(1,100), '"\\%" . v:val * 2 . "l"'), '\|') . '\)/'
I want to get Vim (MacVim for now) to highlight non-ASCII characters. As per this answer I added these two lines to my vimrc:
syntax match nonascii "[^\x00-\x7F]"
highlight nonascii guifg=#ffffff guibg=#ff0000
" highlight link nonascii ErrorMsg (this didn't work either)
And I pasted some text containing a right single quote (hex 2019) into an HTML file. Vim didn't highlight it.
But when I replaced the two lines above with the following, it worked:
syn match ErrorMsg /[^\x00-\x7F]/
Why didn't the first version work?
EDIT: further investigation shows neither version works when I open vim with my file. But both work if I execute them by hand when vim is already open.
Your solution only works for the GUI (gvim); if you're using Vim from a terminal, add ctermbg and/or ctermfb, for example:
highlight nonascii guifg=#ffffff guibg=#ff0000 ctermbg=red
I was able to get my second version working by prepending autocmd BufEnter *:
autocmd BufEnter * syn match ErrorMsg /[\x00-\x7F]/
The idea came from this answer about non-filetype-specific syntax highlighting.