0

For a time I have one vim configuration on my laptop and another on my desktop, so when I edited filed on my Dropbox between the two I would end up with format issues.

Specifically, I had ffs=mac,unix,dos on one and ffs=unix,dos on the other.

Now thankfully that setup is fixed! Yet stragglers remain (those files which haven't been opened, and converted and saved with the new setup).

Now sometimes when I open a file from where it was saved with an ff of "mac", I get a bunch of ^M.

So I do:

e ++ff=mac
setlocal ff=unix
w

To correct the issue.

What works to do this 3 line procedure automatically everytime I open a file?

Or even better how could it work to do it automatically everytime I open a file which has an ff that is not unix?

I've Googled [vim task on every buffer open] and [vim macro on every file open] and haven't seen anything that works.

(I'm sure vim in its seemingly infinite wisdom has something just for this, as it seems to have something just for anything [how did they imagine and make all these features?!].)

3
  • You are probably better off using grep -rlIP '[\x0d]' * to find the affected files and running dos2unix on them.
    – dotancohen
    Nov 23, 2014 at 6:27
  • It would work and it would be an extra step, ancillary to my work flow. Do more with less, just add a live to the vimrc I use all the time instead of doing anything additional.
    – Cris
    Nov 24, 2014 at 2:07
  • Cris: The idea would be that once you've converted the files, you would never need to do it again.
    – dotancohen
    Nov 24, 2014 at 6:18

2 Answers 2

2

I'd hesitate to automatically fix this, but the following autocmd seems to do the job.

autocmd BufRead * if line('$') == 1 && getline(1) =~# '\r' | edit ++ff=mac | w ++ff=unix | endif

It detects files that consist of only a single line that contains embedded CR characters, and automatically performs the conversion. I'd probably add a :echomsg to notify the user, too.

4
  • That's basically the heart of the somewhat complicated script here, for reloading mixed line-ending files in DOS. You could then save the file to write out the line ending change. vim.wikia.com/wiki/…
    – Ben
    Nov 20, 2014 at 16:37
  • I'm not looking for a complete hand hold, and yet I decide to ask you: could you explain a little bit this syntax? What do the "pipes" do ? And which words are variables? What's the =~ comparison operator mean? And what do the special symbols '$' and # mean?
    – Cris
    Nov 24, 2014 at 2:06
  • The pipes (:help :|) are command separators to put everything into a single line. =~ is regexp match, with # case-sensitive. Basically, it says "if the highest line number is one and the first line contains a carriage return, edit with changed fileformat and write. Vimscript is somewhat quirky, but the :help explains all of this very well. Nov 24, 2014 at 8:00
  • Oh, and please don't forget to close this question by accepting an answer. Just click on the outlined checkmark next to it. Thanks! Nov 24, 2014 at 8:01
1

Don't try to automate your workaround for a borked setup, fix the setup; i.e. consistently use a certain line ending 'fileformat' across all platforms, and ensure that this gets properly detected (by including it in 'fileformats') on each system!

For Vimscript files, that canonical format would be unix, because that is handled both by Vim on Windows and Unix (and Mac, I suppose), whereas dos would cause errors when sourcing on Unix.

2
  • It is solid reasoning. And the setup is fixed now. I want to automate the stragglers!
    – Cris
    Nov 19, 2014 at 12:19
  • @ingo, same on Mac. You can safely assume it's just another Unix.
    – romainl
    Nov 20, 2014 at 13:07

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .