Vim Tips Wiki
(Merge in syntax stuff from duplicate tips)
(Todo: Check duplicate 1274)
Line 148: Line 148:
 
*Recheck that the commands work.
 
*Recheck that the commands work.
 
*Rename tip, perhaps to "Highlight unwanted spaces".
 
*Rename tip, perhaps to "Highlight unwanted spaces".
  +
*Check script in [[VimTip1274]]. If it is useful, keep 1274 and add it to see-also here. If not, delete 1274.
   
 
----
 
----

Revision as of 05:47, 18 June 2008

Tip 396 Printable Monobook Previous Next

created January 9, 2003 · complexity basic · author Anon · version 6.0


It can be hard to maintain consistent use of whitespace characters (space and tab). You may not want spaces before tabs, or trailing whitespace at the end of a line. Sometimes you may want to avoid tabs, or just see where tabs occur.

This tip shows several ways to highlight unwanted whitespace.

Highlighting with a search

For occasional use, you can simply search using a suitable pattern to highlight what you want. The following examples assume you have used :set hlsearch. :help 'hlsearch'

" Show all tabs:
/\t

" Show trailing whitespace:
/\s\+$

" Show spaces before a tab:
/ \+\ze\t

Highlighting with the match command

The :match command specifies the name of a highlight group and a pattern. Any text matching the pattern will be displayed in the foreground and background colors defined by the highlight group. :help :match :help :2match

Some examples follow. These use a highlight group named ExtraWhitespace which could be defined with one of the following in your vimrc file. :help :highlight

:highlight ExtraWhitespace ctermbg=red guibg=red
" The following alternative may be less obtrusive.
:highlight ExtraWhitespace ctermbg=darkgreen guibg=lightgreen
" Try the following if your GUI uses a dark background.
:highlight ExtraWhitespace ctermbg=darkgreen guibg=darkgreen
" Show trailing whitespace:
:match ExtraWhitespace /\s\+$/

" Show trailing whitepace and spaces before a tab:
:match ExtraWhitespace /\s\+$\| \+\ze\t/

" Show tabs that are not at the start of a line:
:match ExtraWhitespace /[^\t]\zs\t\+/

" Show spaces used for indenting (so you use only tabs for indenting).
:match ExtraWhitespace /^\t*\zs \+/

" Switch off :match highlighting.
:match

Following is an alternative to show trailing whitespace. This does not match when you type in a trailing whitespace (it won't highlight while you are typing at the end of a line).

:match ExtraWhitespace /\s\+\%#\@<!$/

Any :match highlighting applies only to the current window. With the following in your vimrc, the command will be applied to the first window, and to any subsequent windows. The pattern * applies the highlight to all files.

" Show leading whitespace that includes spaces, and trailing whitespace.
:match ExtraWhitespace /^\s* \s*\|\s\+$/
:autocmd WinEnter * match ExtraWhitespace /^\s* \s*\|\s\+$/

Rather than an autocmd, you may prefer a mapping. With the following, and the default backslash Leader key, you can press \wn to switch highlighting on, and \wf to switch it off.

:nnoremap <Leader>wn :match ExtraWhitespace /^\s* \s*\<Bar>\s\+$/<CR>
:nnoremap <Leader>wf :match<CR>

Highlighting with the syntax command

Using the :match command is easy, but you may want to keep it for another purpose. An alternative is to use syntax highlighting. :help syntax

Here is an example using one of the patterns shown earlier. Put this in your vimrc, after the command :syntax on (which you may already have). Of course you would also need to define ExtraWhitespace with a :highlight command, as shown earlier.

" Show trailing whitepace and spaces before a tab:
:autocmd Syntax * syn match ExtraWhitespace /\s\+$\| \+\ze\t/

Using the list and listchars options

In Vim, 'list' is a boolean option that defaults to off. If 'list' is on, whitespace characters are made visible. The 'listchars' option can be used to customize the way whitespace characters are shown. The default displays "^I" for each tab, and "$" at each EOL (end of line, so trailing whitespace can be seen). :help 'list'

The command :set list displays whitespace, while :set nolist displays normally. It is convenient to use :set list! to toggle the option on, so that you can later press : followed by Up arrow to repeat the previous command, to toggle 'list' off.

The following example toggles list, then sets listchars to not display an end-of-line character, and to display > for the first character occupied by a tab, and - for any subsequent characters that the tab may occupy.

:set list!
:set listchars=tab:>-

Here are some alternatives that you can try (each sets list and listchars in one command):

:set list listchars=tab:>-,trail:.,extends:>
" Enter the middle-dot by pressing Ctrl-k then .M
:set list listchars=tab:\|_,trail:·
" Enter the right-angle-quote by pressing Ctrl-k then >>
:set list listchars=tab:»·,trail:·
" Enter the Pilcrow mark by pressing Ctrl-k then PI
:set list listchars=tab:>-,eol:¶
" The command :dig displays other digraphs you can use.

The listchars option uses the "NonText" highlighting group for "eol", "extends" and "precedes", and the "SpecialKey" highlighting group for "nbsp", "tab" and "trail". :help 'listchars'

Using syntax space errors

Vim has several syntax files that support the display of "space errors". For example, for the C programming language (:set filetype=c), you could put the following in your vimrc:

let c_space_errors=1

Supported languages are: ada, c, chill, csc, forth, groovy, icon, java, lpc, mel, nqc, nroff, ora, pascal, plm, plsql, python and ruby. The c settings also apply to cpp.

To highlight space errors in java files, you would use:

let java_space_errors=1

For C, if you don't want to see trailing space errors at end-of-line set:

let c_no_trail_space_error=1

If you only use spaces to indent, and don't want to see space errors in front of tabs:

let c_no_tab_space_error=1

If you are interested in learning more, open the syntax file for C (in Vim, put the cursor on the path $VIMRUNTIME/syntax/c.vim and press gf). Then look for the definition of the highlight group cSpaceError (near the end of the file). That group specifies the color used to display space errors in C files. The command :hi cSpaceError will show "xxx" in that color.

See also

Comments

 TO DO 

  • Recheck that the commands work.
  • Rename tip, perhaps to "Highlight unwanted spaces".
  • Check script in VimTip1274. If it is useful, keep 1274 and add it to see-also here. If not, delete 1274.

 TO DO 

  • Is following comment worthwhile, or should we just delete it?
  • Investigate its regex: Isn't \_^\|\_^\t\+ just \_^\t*
  • An alternative pattern was used above: /[^\t]\zs\t\+/

I added the following to my syntax file. See :help mysyntaxfile-add for how to add your own syntax options.

" Show tabs that are not at the start of a line:
syn match cTodo display "\(\_^\|\_^\t\+\)\@<!\t"