I've set the following options in .vimrc
set listchars=tab:▸\ ,trail:·
set list
And expected to see dots in those places where spaces are used for tabulation in the code (I use spaces, not tabs).
However, the result is different:
Could you please recommend how to reach the desired result? Thanks!
You should check this link. I'm using the match command solution :
:highlight ExtraWhitespace ctermbg=red guibg=red
:match ExtraWhitespace /\s\+$/
This page also provides list based solutions which I haven't personally tried.
The vim-better-whitespace plugin incorporates many tips from the Vim Wiki page referenced in #icecrime's answer. It also has some nifty configuration options.
I installed pathogen.vim just to use this plugin and am happy with my life, all things considered.
And expected to see dots in those places where spaces are used for tabulation in the code (I use spaces, not tabs)
Actually this is the other way round, tab option is used to display a character when a tab character is inserted (\t) instead of spaces.
And trail is use to show trailing spaces at the end of lines.
You seem to have single empty line with trailing spaces, and dots are correctly displayed.
If you are only using spaces tab option is not used or displayed.
To highlight trailing whitespace characters:
:set hlsearch, then
/\s\+$
A more programmatic way to do this is via function matchadd():
hi TrailingWhitespace ctermbg=red guibg=red
call matchadd("TrailingWhitespace", '\v\s+$')
The 2nd paramter to matchadd() is the pattern we want to match. Here, we use single quote to avoid having to escape speical characters like backslashes etc, see also literal-string.
Based on the link posted by icecrime, I find this works quite well...
" Be clever about highlighting trailing whitespace (don't highlight it if we are
" in 'insert' mode and the cursor is at the end of the line). Also (regardless
" of 'insert' mode), highlight any tabs that immediately follow space(s).
" EOLWS and EOLWSInsert are colour group names; the latter being toned-down to
" make editing in 'insert' mode easier on the eye
autocmd InsertEnter * match EOLWS // | match EOLWSInsert /\s\+\%#\#<!$\| \+\ze\t/
autocmd InsertLeave * match EOLWSInsert // | match EOLWS /\s\+$\| \+\ze\t/
autocmd WinEnter,BufWinEnter,WinNew * match EOLWS /\s\+$\| \+\ze\t/
" Disable syntax-specific trailing space error handling because it conflicts
" with the above, mostly because the syntax highlighting doesn't take account of
" whether 'insert' mode is active or not. There are other '*_no_trail_space_error'
" settings - refer to syntax files in $VIMRUNTIME/syntax/
let c_no_trail_space_error = 1
let java_no_trail_space_error = 1
Also, make sure the 'Error' highlight group is NOT defined as inverse video - if it is, it conflicts on strange ways with the above
And expected to see dots in those places where spaces are used for tabulation in the code
Vim has 3 options for displaying spaces with listchars:
space show all spaces with the specified character.
lead Shows leading spaces, i.e. spaces at the starting of the line. I think this is what you want.
trail Shows trailing spaces, i.e. spaces at the end of the line.
There is also multispace but that is irrelevant to your problem.
Related
I am pretty noob at Vim still, pardon my ignorance
"Boundary" whitespace is any whitespace except single spaces between words, paraphrasing from VSCode docs:
I want to mimic this same behavior in Vim (ideally with no plugins).
An example of how this feature looks in VSCode:
There aren't many resources that I found on this topic. There is Render space if it have multi space in Vim, which addresses this exact same feature (previous example image shown is from that question), but the provided solution does not work exactly as expected.
This is an example of how boundary whitespaces look after trying out that stackoverflow solution:
While the '·' character is shown when there are 2 or more spaces in a row, which is desired, they are also shown highlighted, which is not what I expected
Moreover, if I place the cursor on a line that contains consecutive spaces, they disappear (or are highlighted into invisibility, whatever is happening that renders them invisible)
Excuse my newbie comments in my .vimrc; the rest of the file is commented out
This is another example, in VSCode, of the desired behavior:
Am I missing something from that previous question? Is the highlighting of whitespaces normal or a different issue on its own? Thanks in advance!
The feature you are looking for can be enabled with :help 'list' and customized with :help 'listchars':
The result above can be obtained by adding the following lines to your vimrc:
set list
set listchars=eol:↵,lead:·
If your color scheme makes those characters too visible, you can override its NonText and SpecialKey highlight groups (mentioned in :help 'listchars') to make them more bearable. For example:
augroup MyColors
autocmd!
autocmd ColorScheme * hi NonText ctermfg=235 | hi SpecialKey ctermfg=235
augroup END
colorscheme foobar
Note that listchars was more limited at the time the linked question was answered so, while it was more or less valid at the time, it is no longer necessary to use the conceal feature for this.
I want non-breaking space to be immediatly highlighted when I type one in VIM, because my keyboard's layout makes me sometimes write non-breaking space instead of normal space, and then it's a hard time debugging.
I found a cool trick, entering the command :
:syntax match ErrorMsg " "
does exactly what I want (the space between quotes is a non-breaking space)
Now when I add this command to .vimrc (without the beginning colon) it doesn't work… any idea?
May depend on how you are entering it into .vimrc, but try:
au VimEnter,BufWinEnter * syn match ErrorMsg " "
Personally I think it would make more sense to map the typing of that space to the Space -- how often do you need to type a non-breaking space? Or maybe you can fix the issue at its source: the keyboard layout.
A custom highlight rule can be used
highlight NonBreakingSpace ctermbg=red guibg=red
match NonBreakingSpace / / " (CTRL+V x a 0)
Note that NonBreakingSpace is an arbitrary rule name. The pattern passed to match is what actually finds the nbsp character.
I'm currently trying to switch from gedit to vim. I try to make vim look similar to gedit:
Especially, I would like to show spaces with dots.
I currently have:
There are some differences, but it looks quite similar (this is my current .vimrc file). But I don't get those dots for spaces.
How can I make vim show spaces?
I found some answers (like this one) that seem to suggest to replace spaces by a special visible character and then replace it back. I don't want to do this every time. I want to be able to open vim and see those spaces.
Show non-space whitespace with
set list
Additionally show spaces as . with
set lcs+=space:·
Turn it off with
set nolist
There are times when I absolutely need to see which whitespaces are tabs and which are true space characters, as well as when spaces appear at the end of a line.
When I'm using vim, I use:
:set list
(which I can turn off with :set nolist)
With :set list, literal spaces show up as spaces, tabs show up as ^I, and the end-of-line shows up as a bright-pink $. (So any blank space you see to the left of the pink $ you'll know is a true space character, and the blank spaces to the right of it is just the "nothing" between the end-of-line and the right side of the editor.)
It's ugly, but it works well.
I don't recommend using it all the time -- just those times when it is crucial to see where literal spaces and literal tab characters exist.
While you can't do exactly what you want here, if your reasoning is like mine, you're wanting to see those spaces because it helps verify proper indentation (do I have 2 spaces there, or is it 3?). For Vim >= 7.3, you can use the indentLine plugin to add a marker at each indentation. It's awesome.
To install if you're using Pathogen:
cd ~/.vim/bundle
git clone https://github.com/Yggdroot/indentLine
It may be worth using undercurl to do the job.
hi WhiteSpaces gui=undercurl guifg=LightGray
match WhiteSpaces / \+/
or you can put this in your .vimrc
autocmd ColorScheme * highlight WhiteSpaces gui=undercurl guifg=LightGray | match WhiteSpaces / \+/
Show leading spaces (indent spaces) using the above mentioned plugin indentLine.
If you use Vundle as plugin manager you can add Plugin 'Yggdroot/indentLine' to your .vimrc and then run vim +PluginInstall +qall to install the plugin.
Add the following two lines to your .vimrc to show leading spaces as ·.
let g:indentLine_leadingSpaceChar='·'
let g:indentLine_leadingSpaceEnabled='1'
Using the answers of J-L and Cedric Simon. Edit ~/.vimrc and add:
set lcs+=space:·
nmap <F2> :set invlist<CR>
imap <F2> <ESC>:set invlist<CR>a
and when you work with vim just pres F2
Vim has been providing the 'listchars' option to show Tab vs. Space, and space characters in critical places, i.e. trailing at the end of lines. In previous versions (when the question was written), it did not offer a modification for all spaces: a blank square is a space, period. Other answers provided some workarounds, though.
In current Vim versions, there are many space-related options; cp. :help 'listchars' for details.
You can get the effect seen in your screenshot; persist by putting that command into your ~/.vimrc:
:set list listchars+=space:. listchars-=eol:$
You don't need to install any plugin to this:
set listchars=tab:\|\
"set listchars=tab:\┊\
"set listchars=tab:\┆\
"set listchars=tab:\¦\
set list
The following snippet from my .vimrc highlights superfluous whitespace at line ends in a shade of gray:
autocmd Syntax * syntax match MySpace /\s\+$/
autocmd ColorScheme * highlight MySpace ctermbg=238
But this does not work when this whitespace is already matched by a syntax group. For example, trailing whitespace in various types of comments is not marked.
The manual talks about the contains=ALL option for syntax groups, but there seems to be no analogous containedin=ALL. Can I emulate it in any way? The only method I could come up would be to list all relevant syntax groups in the containedin= option of MySpace, and that's clearly tedious and not at all elegant.
Don't know how to do this with Syntax, but you can use the listchars options to highlight trailing spaces.
From my .vimrc:
" List chars
set listchars="" " Reset the listchars
set listchars+=tab:\|\ " show tabs as "|"
set listchars+=nbsp:· " show non-breaking spaces as "·"
set listchars+=trail:· " show trailing spaces as "·"
set listchars+=precedes:«
set listchars+=extends:»
You should use the :match command (or matchadd()), as described in the Vim Tips Wiki article about this particular topic.
If you like a ready-to-use solution for this, you can also try out my ShowTrailingWhitespace plugin, or one of the alternatives listed on the plugin page.
I'd like to have vim highlight entire lines that match certain patterns. I can get all the text in a line to highlight (by doing syn match MyMatch "^.*text-to-match.*$"), but it always stops at the end of the text. I'd like to to continue to the end of the term, like highlighting CursorLine.
I've tried replacing $ with a \n^, hoping that would wrap it around. No change. (I didn't actually expect this would work, but there's no harm in trying.) I also tried adjusting the syn-pattern-offset (which I read about here: http://vimdoc.sourceforge.net/htmldoc/syntax.html#:syn-pattern). Long story short, adding he=he-5 will highlight 5 fewer characters, but he=he+5 doesn't show any extra characters because there aren't characters to highlight.
This is my first attempt at making a vim syntax and I'm relatively new to vim. Please be gentle and include explanations.
Thanks!
(edit: Forgot to include, this is a multiline highlight. That probably increases the complexity a bit.)
It's not very adaptive as the filename (buffer) and line to full row highlight needs to be explicitly identified, but apparently the sign command can be used:
It is possible to highlight an entire
line using the :sign mechanism.
An example can be found at :help
sign-commands
In a nutshell:
:sign define wholeline linehl=ErrorMsg
:sign place 1 name=wholeline line=123 file=thisfile.txt
Obviously, you should pick a higlight
group that changes the color of the
background for the linehl argument.
source: Erik Falor, vim mailing list
From the documentation on syn-pattern:
The highlighted area will never be
outside of the matched text.
I'd count myself surprised if you got this to work, but then again, Vim is always full of surprises.
could also try
:set cursorline
:set cursorcolumn
change colors like this:
:hi cursorline
:hi cursorcolumn
using the usual term=, ctermfg=, ctermbg= etc
see this answer
VIM Highlight the whole current line