How do I hide the eol doc chars ^M in VIM
Asked Answered
B

8

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.

Bordelon answered 1/10, 2009 at 19:30 Comment(0)
J
40

You may want to set fileformat to dos.

:ed ++ff=dos %
Jegar answered 1/10, 2009 at 19:34 Comment(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).Muskogee
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 vim.wikia.com/wiki/File_formatMorphology
C
28

To hide them:

:set fileformats=dos

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

:%s/\r//g
Columbic answered 1/10, 2009 at 19:38 Comment(2)
In my case, after doing :set fileformats=dos I had to re-load the file via :e %. Then the ^M characters all disappeared.Frias
If I use :set fileformats=dos, I assume that any new line breaks I add will also be in dos format. e.g., hitting <CR> 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?Dopester
D
15

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. ;-)

Daemon answered 27/8, 2012 at 11:2 Comment(4)
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 ?Paramilitary
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.Daemon
I think the newer syntax is :match Ignore /\r$/Pointless
@KevinSeifert Neither of those worked for me, but see vi.stackexchange.com/a/39303/2380Finis
C
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
Cubage answered 1/10, 2009 at 19:37 Comment(1)
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...Cubage
T
0
:0,$ s/<ctrl-v><ctrl-m>//g
:set ff=dos
Trona answered 1/10, 2009 at 19:35 Comment(0)
T
0

use the command:

:1,$ s/^v^M/ /g
Taiwan answered 1/10, 2009 at 19:36 Comment(1)
touche` -- I missed that finer point as well (though I did make a copy, rather than whack the original)Cubage
W
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

Weatherbeaten answered 9/3, 2010 at 18:37 Comment(0)
K
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

Kuomintang answered 18/2, 2023 at 12:52 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.