41

In gvim on windows if I have text with CRLF eol then the text will display ^M at the end of each line.

How do I 'hide' that special char from display?

The :set nolist command """ does not dismiss it.

UPDATE

I did :set fileformats=unix,dos as a list. It didn't work at first, but I closed the file and reopened it again and it worked.

By default I had set fileformats to only unix value.

Thanks for answers.

KyleMit
  • 30,350
  • 66
  • 462
  • 664
marekj
  • 1,235
  • 2
  • 10
  • 11

8 Answers8

40

You may want to set fileformat to dos.

:ed ++ff=dos %
ib.
  • 27,830
  • 11
  • 80
  • 100
Michael Krelin - hacker
  • 138,757
  • 24
  • 193
  • 173
  • 2
    This seems to "fix" the file adding dos linebreaks wherever they are missing, which may be what causes vim to display the ^M markers (was in my case). – numerodix Aug 10 '10 at 11:47
  • 4
    Both this answer and @jqno's answer are "correct", but in different ways. `:e ++ff=dos %` will re-open the current file in DOS/Windows mode. `:set fileformats=dos` will cause any subsequently opened files to be opened in DOS/Windows mode. A good explanation with all the details is available at http://vim.wikia.com/wiki/File_format – Kristopher Johnson Apr 13 '16 at 18:16
27

To hide them:

:set fileformats=dos

To remove them (so you can later save the file as a unix file):

:%s/\r//g
jqno
  • 15,133
  • 7
  • 57
  • 84
  • 5
    In my case, after doing `:set fileformats=dos` I had to re-load the file via `:e %`. Then the `^M` characters all disappeared. – arr_sea Jan 31 '17 at 02:55
  • If I use `:set fileformats=dos`, I assume that any new line breaks I add will also be in dos format. e.g., hitting `` in `normal` mode will add `\r\n`. That's what I'd expect, but if that's the case "hiding" might be a misleading way to to describe that. Specifically, they're being "hidden" because vim is now representing line breaks as `\r\n`. If this assumption is correct, can the answer be updated to make this more obvious? – gbromios May 02 '23 at 14:13
14

While convening on the DOS or Unix format once and for all is of course the preferable approach, sometimes some co-workers just don't care enough about proper source management to make their editors behave.

In those desperate cases, instead of converting the file completely (resulting in a file completely rewritten by yourself according to the SCM, rendering the “blame” function useless), I found it preferable to just pretend that the problem doesn't exist. If the compiler is accommodating, and PHP by all means is, you can have a mixed-EOL file look perfectly cool with the following command:

:match Invisible /\r$/

Or in newer versions of VIM 7.4+

:match Ignore /\r$/

To make things even worse, most GUI editors don't end a text file with a newline, and when a file does end with a newline, they show an empty line at the bottom. Since this is kind of annoying, most people will remove that empty line, which will result in a mixed-EOL file (and the dreadful ^Ms shown in Vim) if the file format was DOS.

If anyone knows how to make Eclipse or NetBeans honor the newline termination without showing the empty last line (as Vim cleverly does), please share your knowledge and you'll make a coder happy here. ;-)

Kevin Seifert
  • 3,494
  • 1
  • 18
  • 14
wiz
  • 606
  • 5
  • 9
  • I get "E28: No such highlight group name: Invisible" when I tried this on gvim 7.4 running on Windows ? Is there something else I have to do first ? – monojohnny Apr 07 '16 at 11:07
  • Interesting, I haven't been using this trick for a while (because of new job and more careful co-workers), and now that I try it on OSX with Vim 7.4 I get the same error. I guess the definition for `Invisible` disappeared from the distribution's syntax files, so we might have to re-implement it in our own `.vimrc`. Unfortunately I have no idea how it was originally encoded. – wiz Apr 19 '16 at 11:24
  • 1
    I think the newer syntax is `:match Ignore /\r$/` – Kevin Seifert Mar 14 '17 at 17:02
  • @KevinSeifert Neither of those worked for me, but see https://vi.stackexchange.com/a/39303/2380 – Keith Thompson Dec 06 '22 at 05:54
1

The file format is usually automatically detected. You must have mixed Unix and DOS/Windows lines in your file.

try this to clean it up (where "clean" = unix format):

% tr -d '\015' < old.file > new.file
Roboprog
  • 3,054
  • 2
  • 28
  • 27
  • Er, you did say "on windows". Oops. Well, suffer, then? Everybody has cygwin, right? I suppose there's a lot more Windows programmers than *nix programmers, but I tend to forget this (despite starting on MS-DOS in the 80s). Can't stand the thing, myself... – Roboprog Jan 26 '10 at 01:44
0
:0,$ s/<ctrl-v><ctrl-m>//g
:set ff=dos
John Millikin
  • 197,344
  • 39
  • 212
  • 226
0

use the command:

:1,$ s/^v^M/ /g
ib.
  • 27,830
  • 11
  • 80
  • 100
Lonzo
  • 2,758
  • 4
  • 22
  • 27
  • touche` -- I missed that finer point as well (though I *did* make a copy, rather than whack the original) – Roboprog Oct 01 '09 at 20:10
0

I'd like to point out that if you are using full blown VIM (at least on my ubuntu 9.10 box) it "does what you want" and auto-hides it, but the stock vi (and vim-tiny) do NOT auto-hide the ^M. If you do a minimal install (or server install) you get vi and vim-tiny only. The fix I found was to install proper vim (apt-get install vim) on my ubuntu box. Hope that helps someone that comes along this topic for the same reason I did :-D

0

When my neovim showed me these ugly ^M in some files from node_modules, I fixed this by adding following code to BufWinEnter:

  if char2nr(getline(1)[-1:-1]) == 13
    e ++ff=dos
  endif

vatosarmat
  • 1,090
  • 10
  • 22