Last Updated: February 25, 2016
·
1.835K
· gpakosz

Vim - hiding carriage returns (^M)

Sometimes, you end up editing files with mixed line endings.

You would like to have less visual distraction by removing those ^M Vim displays.

Yet you don't want to convert the whole file to LF or CRLF (for various reasons like not creating meaningless diffs in the SCM).

Just add the following snippet to your .vimrc:

for i in ['cterm', 'gui']
  for j in ['fg', 'bg']
    let c = synIDattr(hlID('Normal'), 'bg', i)
    if (!empty(c))
      exec 'highlight CarriageReturn ' . i . j . '=' . c
    endif
  endfor
endfor
if hlID('CarriageReturn')
  match CarriageReturn /\r$/
endif

Not ideal, but better than nothing.