As far as I know the carriage return \r in Windows will be displayed in vim as ^M.
I have set set ff=unix in .vimrc.
The sample file.

xxd  /tmp/test
00000000: 6d79 2074 6573 740d 0a                   my test..

When I open it in my vim and set list:

enter image description here

Why is ^M not shown on my vim?

  • That's weird. Normally the only time you see experienced SE users ignoring thoughtful answers is when they have an ax to grind.
    – B Layer
    Oct 23 '20 at 1:26

As far as CR vs CRLF and Windows vs Unix go, Vim tries to keep things pretty and not expose expected control characters.

In other words, Vim, by default (see below), guesses what type of file ("dos" or "unix") is being edited based on the line endings. (I'm ignoring "mac" here for the sake of simplicity.) It's guess is stored in 'fileformat' (alias 'ff').

In your case, with CRLF line endings on Windows, you can expect 'ff' to be set to "dos". So the carriage return (^M) is expected and not shown.

In Vi you'd be more likely to see ^M...

If you use the good old Vi and try to edit an MS-DOS format file, you will find that each line ends with a ^M character. (^M is <CR>). The automatic detection avoids this.

If you really want to see the ^M, though, you can overrule the format with this:

:edit ++ff=unix file.txt

++ does per-command overriding of default values of a setting. ++ff is used for 'fileformat'. You could also use ++ff=dos for the reverse scenario. (Note: the only settings supported by ++ for override currently are 'fileformat' and 'encoding'.)

This is all from :h usr_23.txt under the section named "Overruling the Format".

Update: Right around the time I was submitting this I noticed your edit where you mention you tried :set ff=unix. That won't work on its own. There is another setting 'fileformats' (alias 'ffs') that influences the behavior. On Windows the default is "dos,unix". Multiple values in 'ffs' turn on auto-detection which overrides your manual setting of 'ff'.

If you unset 'ffs' with :set ffs=, though, that will disable auto-detection and :set ff=unix should work then.

When 'ffs' is empty then the format defined with 'fileformat' will be used always.

(From :h 'ffs')

(Why does ++ff=unix work without requiring you to change 'ffs'? Because that's how it's implemented. ;)


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.