1

I have a file with mixed Unix-style (LF) and Windows-style (CRLF) line endings.

If a file has consistent endings on all lines, vim will display all the lines without any special marking on the end.

If it has mixed line endings, it will set fileformat=unix and display ^M on the end of each line that has a Windows-style CRLF line ending.

The particular file I'm working with is several hundred lines long and has CRLF line endings on all but two lines, so almost the entire file is displayed with ^M endings.

Just for ease of reading, I want vim not to display the ^M. I know how to modify the file so that it has consistent line endings, but I don't want to modify the file or even the buffer. I'm just viewing the file.

Answers to this question suggest modifying the file. I don't want to do that. I need to treat the file as read-only, making a copy of it would be too cumbersome, and I don't want the buffer to show as [Modified].

One of the answers to this question suggests one of these commands:

:match Invisible /\r$/
:match Ignore /\r$/

The latter is supposedly for vim 7.4+, but it gives me an error:

E28: No such highlight group name: Ignore

in every version I've tried, up to and including 9.0.0565. :highlight shows a list of all highlight groups, none of which are suitable. I'm using vim (not gvim) under a monochrome terminal emulator, so any solution involving colors will not work for me.

1 Answer 1

1

The highlight groups get defined by the colour scheme; it seems your colour scheme doesn't define them. I can't find any reference to Invisible in the Vim source directory; I guess it's just something that just so happens to work with that person's colour scheme. But most do seem to define Ignore, for example for the default one:

:verbose hi Ignore
Ignore         xxx ctermfg=15 guifg=bg
        Last set from /usr/share/vim/vim90/syntax/syncolor.vim line 54

You can modify the colour scheme you're using, or define this in your vimrc if you want (but be sure to do it before the :colorscheme command, as that will reset all highlights):

hi Ignore ctermfg=15 guifg=bg

As an alternative, you can also conceal characters with syntax highlighting:

set conceallevel=3
set concealcursor=nvic

autocmd BufReadPost * syn match CR /\r$/ conceal

You need to run this from an autocmd because syntax highlights are reset whenever you load a buffer.

You can set conceallevel and/or concealcursor for some control when it does get shown; this always hides it in all modes, but if you do want it to show in insert mode you can drop the i from concealcursor. See the help pages for more information.

3
  • That works in gvim, but not when I run vim in a plain-text terminal (even if I enable color in xterm, which I usually don't). vim is clever enough to hide ^M characters when it thinks it's part of the line ending for all lines. I'm looking for a way to treat trailing ^M characters the same way when some but not all lines have CRLF line endings (something that's far too common). Nov 22, 2022 at 20:32
  • I think you may have to tweak the ctermfg= colour value @KeithThompson, as you can only hard-code it for terminals (15 is white, as I use a white background in xterm, but if you use a black background you want 0). I also added a different method to the answer which should work for monochrome terminals. Nov 23, 2022 at 0:43
  • Your alternative is what worked for me. If I omit the set concealcursor=nvic, then the ^M is visible when the cursor is on the line, which I might actually prefer. I haven't looked at the file in question in a couple of weeks, but I'll probably add something like this to a function in my .vimrc so I can invoke it only when I need it. Thanks! Dec 6, 2022 at 4:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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