You are not logged in.

#1 2010-06-28 13:37:48

dtw
Forum Fellow
From: UK
Registered: 2004-08-03
Posts: 4,439
Website

Save in Vim causes invisible changes to file

I'm editing an XML file in geany in Windows.

geany says this file is UTF-8 with CR/LF line endings, so I have this modeline in the file:

<!-- vim: set ts=4 ff=dos : -->

When I open the file for editing in Vim I have confirmed the filetype is dos with :set ff? and the encoding is utf8 with :set enc?

However, if I :q it prompts to save changes even if I don't change anything.  If I give in and :wq then svn diff shows the whole file has changed.

However, if I save a new version of the file from Vim, rather than overwriting and compare the with normal diff it finds no changes at all.

This is annoying because if I make a single change in Linux then SVN commits show a total file change, which is obviously useless.

Vim seems to be changing something which diff ignores and svn diff detects - but I dunno what.

Suggestions, please!

Offline

#2 2010-06-28 13:52:35

falconindy
Developer
From: New York, USA
Registered: 2009-10-22
Posts: 4,111
Website

Re: Save in Vim causes invisible changes to file

The difference is in non-printable characters. Look at cat's man page -- there's a way to show line endings.

Offline

#3 2010-06-28 14:57:07

dtw
Forum Fellow
From: UK
Registered: 2004-08-03
Posts: 4,439
Website

Re: Save in Vim causes invisible changes to file

OK, cat does indeed show that my original file doesn't have the ^M carriage returns but after a save in Vim it does.

What seems paradoxical to me is that geany is set to CR/LF line endings already, so why do they only "appear" after a save in Vim, should geany not put them in too?

I don't get it.

Offline

#4 2010-06-28 15:39:56

falconindy
Developer
From: New York, USA
Registered: 2009-10-22
Posts: 4,111
Website

Re: Save in Vim causes invisible changes to file

Been a while since I've used Geany, but I suspect it probably only sets new line endings and doesn't actually convert existing line endings.

Offline

#5 2010-06-28 15:44:06

dtw
Forum Fellow
From: UK
Registered: 2004-08-03
Posts: 4,439
Website

Re: Save in Vim causes invisible changes to file

I've resolved the problem by using:

<!-- vim: set ts=4 ff=unix : -->

geany is still set to CR/LF but it doesn't appear to be saving any CR...

So, I just guess just go with what works?!

Offline

#6 2010-06-28 15:49:45

Trent
Member
From: Baltimore, MD (US)
Registered: 2009-04-16
Posts: 990

Re: Save in Vim causes invisible changes to file

Good reason to use a real editor. wink

You're right, that does sound odd.  Was the file created in Geany, or just opened that way?

... wait, here's what's going on.  Geany opened a file with Unix line endings.  It displayed correctly because Geany isn't stupid like Notepad, but Geany didn't convert the line endings when you closed the file because you didn't tell it to.  But Vim modelines take effect as if you had typed them in after opening the file.  So Vim detected that the file had Unix line endings, and opened it with fileformat=unix.  Then the modeline set fileformat=dos.  When you hit :w, Vim saved it with DOS line endings.

The moral of the story?  Don't use modelines, and if you do, don't use them to set the file format.  (Vim detects that automatically.)

Offline

Board footer

Powered by FluxBB