set ff=dos problems

163 views
Skip to first unread message

Mike Hoy

unread,
Nov 30, 2008, 7:52:18 PM11/30/08
to vim...@googlegroups.com
In my .vimrc file I have:

set ff=dos

and it doesn't format my files for dos. I have to type it out before
saving it. Any way to automate this? My instructor only uses notepad
to view code and I'm on GNU/Linux. I'd like to be able to just use vim
and it automatically saves it in dos format.

--
Mike Hoy

Marc Weber

unread,
Nov 30, 2008, 8:32:48 PM11/30/08
to vim...@googlegroups.com
On Sun, Nov 30, 2008 at 05:52:18PM -0700, Mike Hoy wrote:
> In my .vimrc file I have:
> set ff=dos
> and it doesn't format my files for dos. I have to type it out before

Hi Mike.

The problem is that it's set explicitely when opening a file per buffer.
And per buffer settings override the default one.
However I strongly discourage this way to do it *for all* files.
Because you'll happen to edit you ~/.bashrc file as well and then: oh
dear, bash won't read it :-)

The way to go is using autocommands after creating, reading or before
writing them.
But I'd either use a localvimrc http://www.vim.org/scripts/script.php?script_id=441
to setup this autocommand or use a confirmation to remember you that you
should use dos format for files.

For example something like this should do it (untested):
autocmd BufWritePre * if &ff != 'dos' && input('set ff to dos [y]') == 'y' | setlocal ff=dos | endif

Then you'll continue to edit other files with vim as well..

Enjoy
Marc Weber

Tony Mechelynck

unread,
Nov 30, 2008, 9:02:13 PM11/30/08
to vim...@googlegroups.com

When Vim open an existing file for editing, it checks the ends-of-files,
and remembers how they were set in order not to change the setting when
writing. This is good and proper, and governed by the 'fileformats'
options. With recent Vim versions (7.2.040 or later) this can be
overridden both when reading and writing. This is what I recommend:

- keep 'fileformats' at its default, or maybe make it universal by
" in the vimrc
set ffs=unix,dos,mac
- to create _new_ files with 'dos' fileformat by default (I'm not sure
this blanketwise use of ff=dos is something that can be recommended on
Linux; if I were you I would try to limit it to ONLY the files which
your instructor will ever see -- see below).
" in the vimrc
setglobal ff=dos
- to _change_ the fileformat of an existing file: after reading and
before saving
" at the keyboard while editing
:setlocal ff=dos
- It is NOT recommended to change every file to "dos" fileformat, even
those which will never be sent to your instructor, because if you do,
you will probably create (not always intentionally) a mixture of
dos-like and Unix-like files on your computer, with the risk of getting
error messages from gcc, patch, make, bash, or other typical "unix"
utilities because they found ^M bytes at the end of almost every line in
some file. Even Vim for Unix/Linux cannot understand vim scripts
(including your vimrc) if they have dos-like ends of lines. (OTOH, with
their default settings, Vim for Windows and Vim for Mac do understand
unix-like scripts.) It is possible but *very dangerous* -- IF I WERE YOU
I WOULDN'T DO IT. Here's how:
" in the vimrc
au BufWritePre * if &ma && !&ro | set ff=dos | endif
The test on (&ma && !&ro) is meant to avoid doing it for 'nomodifiable'
and 'readonly' files but IT IS NOT FOOLPROOF. If you use this
autocommand (which, again, I do *not* recommend) you will have to make
DEAD SURE that you set 'readonly' (and ff=unix) on EVERY Unix file you
edit, before saving it (even saving it implicitly if you use the
'autowrite' and/or 'autowriteall' options). Even so, you will then have
a hard time modifying Unix files for use on your own computer.

Note for your instructor (in the "been there, done that" line):
------------------------
Notepad is a broken editor, it cannot open correctly any files having
the standard ends-of-lines used by default on all Unix/Linux systems.
Use WordPad instead, it is just as easy to use and works much better.


Best regards,
Tony.
--
There was a young man from Bel-Aire
Who was screwing his girl on the stair,
But the banister broke
So he doubled his stroke
And finished her off in mid-air.

John Beckett

unread,
Nov 30, 2008, 9:04:19 PM11/30/08
to vim...@googlegroups.com
Mike Hoy wrote:
> In my .vimrc file I have:
> set ff=dos
> and it doesn't format my files for dos.

The following tip is a bit of a mess, but there is some useful info that might
assist:
http://vim.wikia.com/wiki/Change_end-of-line_format_for_dos-mac-unix

I think the problem is that Vim tries to retain the fileformat (line ending) that it
finds when reading the file. The command 'set ff=dos' is talking about the current
buffer, not files that you open in the future.

An autocmd that sets ff for all files in a certain directory might suit, or you
could have a script that you run to prepare files before sending.

John

Mike Hoy

unread,
Dec 1, 2008, 12:58:24 PM12/1/08
to vim...@googlegroups.com
Thanks everyone for your replies.
--
Mike Hoy

Noel Henson

unread,
Dec 1, 2008, 1:30:58 PM12/1/08
to vim...@googlegroups.com

Mike,

If you need it, there is a script to convert between dos/unix/mac EOLs
here: www.noels-lab.com/crlf.html.

I know it's not the same, but it might help.

Noel

--

------------------------------------------------------------------
Noel Henson
www.noels-lab.com Chips, firmware and embedded systems
www.vimoutliner.org Work fast. Think well.

fritzophrenic

unread,
Dec 2, 2008, 11:00:49 AM12/2/08
to vim_use
For *NEW* files to default to dos format, all you'd need to do is (in
your vimrc) :set ffs=dos,unix,mac or :set ffs=dos,unix instead of the
default ffs=unix,dos.

This will still detect existing files, and you will need to convert
them if you created them as unix files.

I actually used something similar to fix the opposite problem, as I do
most editing in Windows and log into a Unix terminal to use various
utilities on them, some of which require unix line endings.

Yongwei Wu

unread,
Dec 2, 2008, 11:58:51 PM12/2/08
to vim...@googlegroups.com
2008/12/3 fritzophrenic <fritzo...@gmail.com>:

>
> For *NEW* files to default to dos format, all you'd need to do is (in
> your vimrc) :set ffs=dos,unix,mac or :set ffs=dos,unix instead of the
> default ffs=unix,dos.

It does not work for me. :set ff=unix in _vimrc works for me, but
only before I open a new a DOS file. It seems nothing works stably
for new file (on Windows) to achieve your purpose.

--
Wu Yongwei
URL: http://wyw.dcweb.cn/

fritzophrenic

unread,
Dec 3, 2008, 8:46:39 AM12/3/08
to vim_use


On Dec 2, 10:58 pm, "Yongwei Wu" <wuyong...@gmail.com> wrote:
> 2008/12/3 fritzophrenic <fritzophre...@gmail.com>:
>
>
>
> > For *NEW* files to default to dos format, all you'd need to do is (in
> > your vimrc) :set ffs=dos,unix,mac or :set ffs=dos,unix instead of the
> > default ffs=unix,dos.
>
> It does not work for me.  :set ff=unix in _vimrc works for me, but
> only before I open a new a DOS file.  It seems nothing works stably
> for new file (on Windows) to achieve your purpose.
>

What exactly "doesn't work" about it?

If you set ffs (note the 's' at the end, to get the fileformatS option
and not the fileformaT option) to unix,dos in Windows, every NEW file
you create WITHIN Vim should have a unix encoding. However, if you
edit an EXISTING file from within Vim, possibly even an "empty" one
that you just created from somewhere else, Vim will automatically
detect the line ending style that already exists in the file. This is
most certainly desired behavior!

If you want to override the 'ffs' option for certain file types on a
read, you can put "setlocal ff=unix" or whatever is desired in an
autocmd or ftplugin file, as has already been suggested.

For example, although I like most of my files to have unix line
endings, and thus have "set ffs=unix,dos" in my vimrc, I like to have
dos line endings in my DOS batch files, so in $HOME/vimfiles/ftplugin/
dosbatch.vim, I have "setlocal ff=dos" to automatically enforce the
dos line endings on all dos files when created or read.

Yongwei Wu

unread,
Dec 3, 2008, 8:47:34 PM12/3/08
to vim...@googlegroups.com
2008/12/3 fritzophrenic <fritzo...@gmail.com>:

> On Dec 2, 10:58 pm, "Yongwei Wu" <wuyong...@gmail.com> wrote:
>> 2008/12/3 fritzophrenic <fritzophre...@gmail.com>:
>>
>> > For *NEW* files to default to dos format, all you'd need to do is
>> > (in your vimrc) :set ffs=dos,unix,mac or :set ffs=dos,unix
>> > instead of the default ffs=unix,dos.
>>
>> It does not work for me. :set ff=unix in _vimrc works for me, but
>> only before I open a new a DOS file. It seems nothing works stably
>> for new file (on Windows) to achieve your purpose.
>>
>
> What exactly "doesn't work" about it?

Exactly as it literally means. When I have :set ffs=unix,dos in
_vimrc, the empty file on opening vim, or results of :enew, still has
ff=dos on Windows.

I even tried putting set ffs=unix,dos in a test.vim, and started vim
with gvim -u test.vim --noplugin, and the result was still the same:
ff=dos. Putting in set ff=unix has effects.

I really can't imagine why it works for you.

I am using gvim 7.2.60 on Windows XP.

Best regards,

Yongwei

fritzophrenic

unread,
Dec 4, 2008, 9:03:08 AM12/4/08
to vim_use


On Dec 3, 7:47 pm, "Yongwei Wu" <wuyong...@gmail.com> wrote:
> When I have :set ffs=unix,dos in
> _vimrc, the empty file on opening vim, or results of :enew, still has
> ff=dos on Windows.
>
> I even tried putting set ffs=unix,dos in a test.vim, and started vim
> with gvim -u test.vim --noplugin, and the result was still the same:
> ff=dos. Putting in set ff=unix has effects.

That's bizarre. What about the output of ":verbose set ffs?" in both
cases?

I don't think it should matter, but:

"Using the '-u' argument has the side effect that the 'compatible'
option will be on by default." (:help -u)

Maybe you are setting 'nocompatible' somewhere after you set the ffs
value, resetting it to the default?

>
> I really can't imagine why it works for you.
>

And I can't imagine why it doesn't for you :-)

> I am using gvim 7.2.60 on Windows XP.
>

Me, too. The 'cream' compile from sourceforge.

Yongwei Wu

unread,
Dec 4, 2008, 9:24:36 AM12/4/08
to vim...@googlegroups.com
2008/12/4 fritzophrenic <fritzo...@gmail.com>:

> On Dec 3, 7:47 pm, "Yongwei Wu" <wuyong...@gmail.com> wrote:
>> When I have :set ffs=unix,dos in
>> _vimrc, the empty file on opening vim, or results of :enew, still has
>> ff=dos on Windows.
>>
>> I even tried putting set ffs=unix,dos in a test.vim, and started vim
>> with gvim -u test.vim --noplugin, and the result was still the same:
>> ff=dos. Putting in set ff=unix has effects.
>
> That's bizarre. What about the output of ":verbose set ffs?" in both
> cases?

Path to my test.vim.

> I don't think it should matter, but:
>
> "Using the '-u' argument has the side effect that the 'compatible'
> option will be on by default." (:help -u)

Adding :set nocompatible in test.vim makes no difference.

> Maybe you are setting 'nocompatible' somewhere after you set the ffs
> value, resetting it to the default?

And it does not matter, since I checked the value of ffs in vim.

>> I really can't imagine why it works for you.
>>
> And I can't imagine why it doesn't for you :-)

Strictly speaking, my results make more sense. Vim help file does not
say that the first value in ffs will be used as the default encoding.
On the contrary, I know that setting the value of fenc will affect the
encoding of the empty file. And it does not persist, since neither
fenc nor ff is really 'global': Vim help states clearly that they are
local to buffer.

>> I am using gvim 7.2.60 on Windows XP.
>>
>
> Me, too. The 'cream' compile from sourceforge.

I use my own build (executables only), available at:

http://wyw.dcweb.cn/download.asp?path=vim&file=gvim72.zip

You may have a try, but I do not think it can make a difference. I
would rather think your environment affected. Did you try with -u
test.vim, as I did?

Tony Mechelynck

unread,
Dec 4, 2008, 3:05:52 PM12/4/08
to vim...@googlegroups.com

If you want to start Vim with standard startup scripts, it is also possible:

cd "C:\Program Files\vim\vim72"
.\gvim -u vimrc_example.vim

starts with a standard vimrc which sets 'nocompatible'. It also loads
all global plugins.

gvim -u NORC -N

starts gvim with plugins but no vimrc, in 'nocompatible' mode.

gvim -u NONE -N

starts gvim in 'nocompatible' mode with no vimrc and no plugins.

gvim -u test.vim --noplugin

starts with no plugins and test.vim as the only vimrc. 'compatible' will
be on unless and until test.vim sets 'nocompatible'.

And so on.

I'm using an own-compiled gvim 7.2.64 with GTK2 GUI on openSUSE Linux,
so I don't think my results are easily comparable with yours what
concerns platform-dependent options like 'ff' and 'ffs'.


Best regards,
Tony.
--
It's the thought, if any, that counts!

Tony Mechelynck

unread,
Dec 4, 2008, 3:18:46 PM12/4/08
to vim...@googlegroups.com
On 04/12/08 15:24, Yongwei Wu wrote:
[...]

> Strictly speaking, my results make more sense. Vim help file does not
> say that the first value in ffs will be used as the default encoding.
> On the contrary, I know that setting the value of fenc will affect the
> encoding of the empty file. And it does not persist, since neither
> fenc nor ff is really 'global': Vim help states clearly that they are
> local to buffer.
[...]

All buffer-local option have a global counterpart. ":set option=value"
sets both. ":setlocal option=value" sets only the current local value.
":setglobal option=value" sets only the global default. OTOH, ":set
option?" displays the current local value if any; you need ":setglobal
option?" to display the global value. In the latter two cases, prefixing
with ":verbose " tells you where the value in question was set.

See ":help local-options".

Normally, the global setting is used when creating a file which doesn't
yet exist. However, in the case of 'fenc' 'ff' and 'binary', the ++enc,
++ff, ++bin and ++nobin arguments to ":edit" (and some other commands)
can override this.

See ":help ++opt".


Best regards,
Tony.
--
"I'd love to go out with you, but I'm doing door-to-door collecting for
static cling."

fritzophrenic

unread,
Dec 4, 2008, 4:29:13 PM12/4/08
to vim_use


On Dec 4, 8:24 am, "Yongwei Wu" <wuyong...@gmail.com> wrote:
> Strictly speaking, my results make more sense.  Vim help file does not
> say that the first value in ffs will be used as the default encoding.

Indirectly, it does.

:help 'ffs' gives:

1. If all lines end in <CR><NL>, and 'fileformats' includes "dos",
'fileformat' is set to "dos".
2. If a <NL> is found and 'fileformats' includes "unix",
'fileformat'
is set to "unix". Note that when a <NL> is found without a
preceding <CR>, "unix" is preferred over "dos".
3. If 'fileformats' includes "mac", 'fileformat' is set to "mac".
This means that "mac" is only chosen when "unix" is not present,
or when no <NL> is found in the file, and when "dos" is not
present, or no <CR><NL> is present in the file.
Also if "unix" was first chosen, but the first <CR> is before
the first <NL> and there appears to be more <CR>'s than <NL>'s
in
the file, then 'fileformat' is set to "mac".
4. If 'fileformat' is still not set, the first name from
'fileformats' is used.

For new files created in Vim, there are no line endings yet in the
file, so 1, 2, and 3 all fail. Hence, 4 "defaults" to deciding that
"the first name from 'fileformats' is used.

This is actually specified explicitly in :help :new and :help :enew:

"If 'fileformats' is not empty, the first format given will be used
for the new buffer."

> I
> would rather think your environment affected.  Did you try with -u
> test.vim, as I did?
>

No, but I tried like this:

gvim -N -u NONE -i NONE
:set ffs=unix,dos
:tabnew
:set ff?

fileformat=unix

gvim -N -u NONE -i NONE
:set ffs=unix,dos
:new
:set ff?

fileformat=unix

gvim -N -u NONE -i NONE
:set ffs=unix,dos
:enew
:set ff?

fileformat=dos

That last one is interesting. Especially since it is in direct
contradiction to :help :enew above.

Yongwei Wu

unread,
Dec 4, 2008, 9:54:22 PM12/4/08
to vim...@googlegroups.com
2008/12/5 fritzophrenic <fritzo...@gmail.com>:

Our results are aligned then. I only check the ff of the empty file on
opening vim and the result of :enew. Neither is affected by ffs. When
I try :new and :tabnew now, you results can be reproduced.

However, since :enew and the default empty file does not work as you
expected, your method is not reliable. Probably you want to file a bug
report to Bram.

Tony Mechelynck

unread,
Dec 5, 2008, 6:45:04 AM12/5/08
to vim...@googlegroups.com

In the meantime (until Bram changes, if ever, the behaviour of ":enew"
to make it match the help), you may define a new command, let's say

command -bang -bar -nargs=0 Enew enew<bang> | set ff=dos


Best regards,
Tony.
--
Different all twisty a of in maze are you, passages little.

Reply all
Reply to author
Forward
0 new messages