Showing trailing spaces in vim

后端 未结 5 1607
生来不讨喜
生来不讨喜 2021-01-30 16:19

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 use

相关标签:
5条回答
  • 2021-01-30 16:56

    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.

    0 讨论(0)
  • 2021-01-30 17:07

    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.

    0 讨论(0)
  • 2021-01-30 17:10

    To highlight trailing whitespace characters:

    :set hlsearch, then

    /\s\+$

    0 讨论(0)
  • 2021-01-30 17:11

    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.

    0 讨论(0)
  • 2021-01-30 17:14

    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

    0 讨论(0)
提交回复
热议问题