39

How can I display line feeds and/or carriage-return characters in vi/vim?

I know that set list shows all the whitespace characters (?), it also replaces tabs \t etc (but that's not what I want). Basically I just want to display certain characters like \r (on Debian this seems to be the default, however on Gentoo it's different).

Kind regards

3 Answers 3

19

In recent versions of Vim there's a 'listchars' setting that lets you specify which characters should be used for the EOL and TAB characters, and for trailing spaces.

You could:

set listchars=eol:$,tab:\[SPACE]\[SPACE]

...to display eol chars specially without collapsing tabs (type a space character, not [,S,P,A,...).

I don't know of anything specifically about return chars in the 'listchars' setting, but I suspect you can use syntax highlighting for this. I think the default display of \r characters is to show them with SpecialKey highlighting.

So the default SpecialKey highlighting of \r characters, combined with setting 'listchars' as above, should be close to what you need.

:highlight SpecialKey ctermfg=5

...if you're fond of magenta.

3
  • Thanks, listchars already helped. Would you mind to explain me how to use SpecialKey? I can't find any useful information, vimdoc.sourceforge.net/htmldoc/syntax.html#hl-SpecialKey didn't help much either.
    – watain
    Jan 19, 2010 at 11:38
  • SpecialKey is the built-in syntax-highlighting label for any characters which are displayed "specially": control chars and the like. If you type ":highlight SpecialKey", that'll tell you how those characters will be displayed.
    – njd
    Feb 9, 2010 at 10:21
  • 1
    When I try the set listchars I get the following error: E474: Invalid argument: listchars=eol:$,tab:\[SPACE]\[SPACE]. I am using MacVim 8.0.596 (133)
    – Eliot
    May 10, 2017 at 20:55
25

reopen DOS-formated text file in UNIX formart:

:e ++ff=unix

you will see ^M(\r) at the end of line.
if you want display ^M in hex:

:set dy=uhex
1
  • 1
    Not sure why these methods do not work for me. I have just found a convenient way to indicate the \n or \r or \r\n. Just search them and they will be highlighted. Type /\n or /\r or /\r\n.
    – midnite
    Dec 29, 2021 at 5:53
6

Add the following line to your .vimrc file:

set fileformats=unix

This causes vim to support only unix-style files natively. For non-Unix style line-endings, the carriage return character \r will be displayed explicitly in vim as ^M.

1
  • Unfortunately, it doesn't show the line feed character as well.
    – jpaugh
    Sep 20, 2017 at 21:45

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .