Phillip
Phillip

Reputation: 43

Any way to ignore ^M carriage returns in VIM?

Most files I'm editing in VIM have carriage returns, so I'm often seeing every line with a ^M at the end. One thing I used to do was to replace them with a command like +e ff=dos, but when I commit with git, it thinks I've changed the entire file. So, I was just wondering if there was a way to keep everyone happy here, whether that's to change some VIM setting, change the way I'm editing, or change some git config?

Upvotes: 4

Views: 209

Answers (2)

Crapo Wolf
Crapo Wolf

Reputation: 2351

You can:

:hi! link SpecialKey Ignore 

which will hide them. They'll still be in your text and can be deleted accordingly, but at least they're not visually intrusive.

On the other hand, since everybody else's editors are so rude as to mess up line endings (or at least fail to sniff them properly and behave accordingly), you can return the favor and normalize them all on load so when you save them back out, they're all at least in the same format. Something like an autocmd that does

:%s/\r//g 
:w! ++ff=dos 

or something of the sort.

Upvotes: 2

mipadi
mipadi

Reputation: 411360

You can set up Git to convert line endings. That is to say, when you check out files from a Git repo, it can convert to LF on Unix, and CRLF on Windows. That way, you'll have the correct line endings for your platform (and so will everyone else).

If you're on Unix, set the core.autocrlf option to input; on Windows, set it to true.

More info is available here.

Upvotes: 4

Related Questions