12

Rather than choosing one character to be displayed as the EOL character, I'd like one to display for both CR and LF separately and regardless of if the file is unix or dos. Is this possible?

  • 2
    So if I understand you correctly you want something like Hello%$ for DOS files (where % is a CR and $ is a LF)? This isn't possible with the list/listchars setting, and I can't really come up with a way to do this in VimScript either... – Martin Tournoij Feb 5 '16 at 6:51
  • 1
    @Carpetsmoker That's correct. Apart from set list is there a way to have CR and LF show as specific characters maybe? – Captain Man Feb 5 '16 at 14:59
  • 2
    You might be able to highlight them a certain color, but there isn't any way of showing them as specific characters that isn't hacky afaik. – EvergreenTree Feb 7 '16 at 19:00
  • I think either CR or LF can be seen at a time, not both. – SibiCoder May 12 '16 at 15:55
  • 1
    set stl+=\ [%{&ff=='mac'?'CR':&ff=='unix'?'LF':'CRLF'}] – Antony Sep 15 '16 at 13:41
2

With some caveats, you can do this using Vim's binary mode. It seems you might want to use the command

vim -b +'set list' somefile

Alternatively, you can put the following in your .vimrc:

:set binary
:set list

The important thing is that binary mode must get set before the file in question is read into a buffer. Once Vim has read the file in, it's too late; this trick relies on changing how Vim reads the file in. Specifically, what you need is for Vim to not attempt to guess the file's particular type of <EOL>.

Binary mode does what you want, disabling this automatic line-separator detection, but it does quite a few other things as well:

  • sets 'textwidth' to 0
  • sets 'wrapmargin' to 0
  • unsets 'modeline'
  • unsets 'expandtab'

So you might not want to have this in your .vimrc; it might be better to use the command-line version, and only for those files where you need this special kind of display.

For more information:

  • :help 'binary'
  • :help edit-binary
  • :help file-read
  • :help file-formats
  • :help 'fileformat'
  • but it does quite a few other things as well is this because binary is a FileType with some auto commands or is it some sort of built in, unavoidable thing? – Captain Man Sep 14 '16 at 14:36
  • By that phrase, I meant to introduce the list that immediately follows it. – Ptolemarch Sep 14 '16 at 14:49
  • Right, I just didn't quote the whole thing :) What I meant was does it do those things because it uses some FileType called binary with those auto commands or was it just some built in thing? – Captain Man Sep 14 '16 at 14:54
  • 1
    Ah, right. None of this has anything to do with file types (but it is quite caught up in file formats). this is part of binary mode itself. See :help 'binary'. And maybe :help 'fileformat'. – Ptolemarch Sep 14 '16 at 14:59
0

If you add to your ~/.vimrc:

set ffs=unix
set list

That will always show CR as ^M and LF as $.

By default, vim would interpret a file that only has CRLF endings as a dos file and show CRLF as $. By setting ffs=unix, you force vim to always open as a unix file, and therefore the CR is treated as an extra character.

Note: Be careful if modifying and saving dos files though. If you press ENTER, it will only insert a LF. You would need to manually insert a CR by typing CTRL+V, CTRL+M.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.