Showing trailing spaces in vim
Asked Answered
N

9

64

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:

enter image description here

Could you please recommend how to reach the desired result? Thanks!

Ninebark answered 6/1, 2011 at 16:18 Comment(0)
S
144

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.

Subtitle answered 6/1, 2011 at 16:29 Comment(5)
Thank. This is really what I looking for. But this match sometimes will be reset after doing some operations. How to always keep it on?Yesterday
put in in your .nvimrc. without the colonsJosettejosey
@Yesterday You can use :2match {group} /{pattern}/Armindaarming
I would also recommend adding autocmd BufWinEnter * match ExtraWhitespace /\s\+$/ autocmd InsertEnter * match ExtraWhitespace /\s\+\%#\@<!$/ autocmd InsertLeave * match ExtraWhitespace /\s\+$/ autocmd BufWinLeave * call clearmatches() as given in the linked page. It helps prevent highlighting while in the insert mode.Trolley
note that it is important to place those lines behind your colorscheme ... commandPahlavi
C
14

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.

Clovis answered 1/11, 2017 at 4:8 Comment(0)
A
8

To highlight trailing whitespace characters:

:set hlsearch, then

/\s\+$

Arturo answered 18/2, 2020 at 12:49 Comment(0)
H
7

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.

Hydroxyl answered 6/1, 2011 at 16:35 Comment(0)
U
3

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.

Unscathed answered 2/11, 2021 at 5:15 Comment(1)
Using matchadd() is practically the only reliable way to always highlight. If quoting :help match it says: Highlighting matches using |:match| are limited to three matches (aside from |:match|, |:2match| and |:3match| are available). |matchadd()| does not have this limitation and in addition makes it possible to prioritize matches. So to e.g. match both non-breaking spaces and trailing white-space, use: highlight InvalidWhitespace guibg=red | call matchadd('InvalidWhitespace', '\%u00a0') | call matchadd('InvalidWhitespace', '[[:blank:]]\+$') Drake
B
1
:set hlsearch

This should be able to highlight all the white spaces in your file

Becalmed answered 5/4 at 15:6 Comment(2)
Thanks for contributing to the community. Can you edit your answer to explain how this is different from @sky’s answer, which also suggests this?Sigmund
I think, there's something missingEducator
I
0

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

Intuitionism answered 16/10, 2019 at 13:55 Comment(0)
P
0

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.

Pyne answered 25/9, 2022 at 6:38 Comment(0)
C
0

In case anyone is looking for a neovim solution, here's how you could set up your lua config:

-- Set the highlight for trailing whitespace
vim.api.nvim_set_hl(0, "ExtraWhitespace", { ctermbg = "darkred", bg = "darkred" })

-- Autocommand to highlight trailing whitespace in all buffers
vim.api.nvim_create_autocmd("BufWinEnter", {
    pattern = "*",
    callback = function()
        vim.fn.matchadd("ExtraWhitespace", [[\s\+$]])
    end,
})
Cactus answered 9/7 at 4:4 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.