Writing files without EOL

502 views
Skip to first unread message

Peter Hodge

unread,
Aug 15, 2010, 9:03:33 PM8/15/10
to vim_use
Dear Vimmers,

Is there a way to make vim write a file using dos line endings (<CR> <NL>) but *without* the EOL at the end?

Currently it seems vim will only let me:

A) Add newlines for no good reason

B) Convert files to Unix line endings

It looks like I will need to write a shell script to strip the EOL after writing, set up an autocmd to run it, and also set up an autocmd to skip the FileChanged warning ... isn't there a better way?

Peter



Sven Guckes

unread,
Aug 15, 2010, 9:13:17 PM8/15/10
to vim_use
* Peter Hodge <toomuch...@yahoo.com> [2010-08-16 03:08]:

> Is there a way to make vim write a file using dos line
> endings (<CR> <NL>) but *without* the EOL at the end?

yes... use ":set noeol"

Sven

Stahlman Family

unread,
Aug 15, 2010, 9:15:38 PM8/15/10
to vim...@googlegroups.com

Peter Hodge wrote:
> Dear Vimmers,
>
> Is there a way to make vim write a file using dos line endings (<CR> <NL>) but *without* the EOL at the end?

Isn't this just another way of saying "mac format"?

:set ff=mac
:help ff

Brett Stahlman

Tim Chase

unread,
Aug 15, 2010, 9:15:28 PM8/15/10
to vim...@googlegroups.com, Peter Hodge
> Is there a way to make vim write a file using dos line endings
> (<CR> <NL>) but *without* the EOL at the end?

Have you experimented with the 'noeol' option?

:help 'eol'

> Currently it seems vim will only let me:
>
> A) Add newlines for no good reason

It's actually for very good reasons[1]

It's one of those "just because Vim allows you to do it, doesn't
mean that it's a good idea" options. But if you're willing to
deal with the consequences of ":set noeol", you're welcome to and
Vim won't stop you. :)

-tim


[1]
http://stackoverflow.com/questions/729692/why-should-files-end-with-a-newline

Peter Hodge

unread,
Aug 15, 2010, 9:18:26 PM8/15/10
to vim...@googlegroups.com
--- On Mon, 16/8/10, Sven Guckes <guc...@guckes.net> wrote:

No, the 'eol' option is ignored unless I set 'binary' also. But if I set 'binary', the file is written as though fileformat=unix, I need it to be written with fileformat=dos.

Peter



Stahlman Family

unread,
Aug 15, 2010, 9:21:24 PM8/15/10
to vim...@googlegroups.com

Stahlman Family wrote:
>
>
> Peter Hodge wrote:
>> Dear Vimmers,
>>
>> Is there a way to make vim write a file using dos line endings (<CR>
>> <NL>) but *without* the EOL at the end?
>
> Isn't this just another way of saying "mac format"?

Ah. It occurred to me after I'd read Sven's reply that you probably
meant "without the EOL at the end of the file". I was thinking you mean
without the newline at the end of the line. Sorry for the confusion...

Brett Stahlman

...[snip]...
>>
>>
>>
>

Peter Hodge

unread,
Aug 15, 2010, 9:21:22 PM8/15/10
to vim...@googlegroups.com
--- On Mon, 16/8/10, Stahlman Family <bretts...@comcast.net> wrote:

> From: Stahlman Family <bretts...@comcast.net>
> Subject: Re: Writing files without EOL

> To: vim...@googlegroups.com
> Received: Monday, 16 August, 2010, 11:15 AM
>
>
> Peter Hodge wrote:
> > Dear Vimmers,
> >
> > Is there a way to make vim write a file using dos line
> endings (<CR> <NL>) but *without* the EOL at the
> end?
>
> Isn't this just another way of saying "mac format"?

Sorry, I do mean fileformat=dos, but I need vim to NOT write a CRNL at the very end of the file, unless there was already one there.

Peter



Peter Hodge

unread,
Aug 15, 2010, 9:27:58 PM8/15/10
to vim...@googlegroups.com
--- On Mon, 16/8/10, Tim Chase <v...@tim.thechases.com> wrote:

> From: Tim Chase <v...@tim.thechases.com>
> Subject: Re: Writing files without EOL
> To: vim...@googlegroups.com
> Cc: "Peter Hodge" <toomuch...@yahoo.com>
> Received: Monday, 16 August, 2010, 11:15 AM

> > Is there a way to make vim write
> a file using dos line endings
> > (<CR>  <NL>) but *without* the EOL at
> the end?
>
> Have you experimented with the 'noeol' option?
>
>   :help 'eol'

The 'eol' option only works if I also turn on 'binary', but if I use 'binary' I can only write the file using fileformat=unix. I need to write the file using fileformat=dos but without \r\n at the very end of the file, unless it was already there.

>
> > Currently it seems vim will only let me:
> >
> > A) Add newlines for no good reason
>
> It's actually for very good reasons[1]

I know there are reasons for having newlines at the end of a file, but I need to avoid changing code unnecessarily.

Peter



Tony Mechelynck

unread,
Aug 15, 2010, 11:48:33 PM8/15/10
to vim...@googlegroups.com, Peter Hodge

Well, then you'll just have to open the file with

:e ++bin ++ff=unix filename

then make sure that there is a visible ^M at the end of each line except
the last. You can put it there by hitting Ctrl-V (or Ctrl-Q if you use
Ctrl-V to paste) followed by <Enter> just before (and in addition to)
the <Enter> which breaks the line. You may even make the mappings (to be
sourced in an "after-plugin" of the ftplugin kind whenever you open one
of those files:

(untested)
map <buffer> o A<C-V><CR><CR>
map <buffer> O O<C-V><CR><Left>
imap <buffer> <CR> <C-V><CR><CR>

and maybe others. Of course this will still not take care of lines
broken by means of a \r in the "replace by" part of a :substitute -- I'm
not sure how to insert a "real" ^M at that point.

Or else, maybe you could write a BufWritePre autocommand to add a ^M at
the end of any line (other than the last) which hasn't yet got one.
Something like this (untested)

function! AddCarRet()
if getline('.') !~ '\r$'
exe "normal A\<C-V>\r\e"
endif
endfunction
au BufWritePre <buffer> 1,$-1call AddCarRet()

Note that if ever you try to concatenate two files, the first one of
which lacks a proper EOL on its last line, the resulting file will have
the last line of the first part and the first line of the second part
concatenated as one long line. Not something desirable IMHO.


Best regards,
Tony.
--
The opposite of a profound truth may well be another profound truth.
-- Bohr

Peter Hodge

unread,
Aug 16, 2010, 1:31:01 AM8/16/10
to vim...@googlegroups.com

--- On Mon, 16/8/10, Tony Mechelynck <antoine.m...@gmail.com> wrote:

> From: Tony Mechelynck <antoine.m...@gmail.com>
> Subject: Re: Writing files without EOL -> :set noeol

I certainly don't want to be making sure ^M is at the end of each line, so I have written a command-line script to erase the last linebreak and an autocmd to call it when the 'eol' option is off. I understand the importance of having a newline at the end of the file, but in this circumstance it is more important to not change anything unless absolutely necessary.

Regards,
Peter



Christian Brabandt

unread,
Aug 16, 2010, 8:55:54 AM8/16/10
to vim...@googlegroups.com
Hi Peter!

On So, 15 Aug 2010, Peter Hodge wrote:

> I certainly don't want to be making sure ^M is at the end of each
> line, so I have written a command-line script to erase the last
> linebreak and an autocmd to call it when the 'eol' option is off. I
> understand the importance of having a newline at the end of the file,
> but in this circumstance it is more important to not change anything
> unless absolutely necessary.

How about something like this:
fun! WriteFilewithoutEOF(filename)
let a=getline(1,line('$')-1)
let b=map(a, 'v:val . nr2char(13)')
call extend(b, getline('$', '$'))
call writefile(b,a:filename, 'b')
endfun

com! -complete=file -nargs=1 WriteFile :call WriteFilewithoutEOF(<q-args>)

You could call this via a BufWriteCmd autocommand.
(I think, this should write DOS like files without an EOF, but please
verify.)

regards,
Christian
--
Letzte Worte eines Informatikers:
"Ich bleibe hier, bis das Problem gel�st ist."

Lenin Lee

unread,
Nov 16, 2010, 3:11:01 AM11/16/10
to vim...@googlegroups.com
I am mad with this problem too, exactly the same with Peter's.

No one has any solution ?

2010/8/16 Peter Hodge <toomuch...@yahoo.com>




--
You received this message from the "vim_use" maillist.
Do not top-post! Type your reply below the text you are replying to.
For more information, visit http://www.vim.org/maillist.php

Christian Brabandt

unread,
Nov 16, 2010, 3:16:12 AM11/16/10
to vim...@googlegroups.com
On Tue, November 16, 2010 9:11 am, Lenin Lee wrote:
> I am mad with this problem too, exactly the same with Peter's.
>
> No one has any solution ?

I saw that there were mentioned several solutions. Did you try them?

regards,
Christian

Lenin Lee

unread,
Nov 16, 2010, 3:35:29 AM11/16/10
to vim...@googlegroups.com
Yes, Peter has made himself very clear, we do have tried some solutions, the problem is:

If we use

:set bin
:set noeol

files in DOS format will be converted to UNIX format.

What we want is:

1. Get rid of eol at the end of a file
2. Preserve DOS fileformat

2010/11/16 Christian Brabandt <cbl...@256bit.org>

--

Christian Brabandt

unread,
Nov 16, 2010, 3:44:01 AM11/16/10
to vim...@googlegroups.com
(Please don't top post.)

On Tue, November 16, 2010 9:35 am, Lenin Lee wrote:
> Yes, Peter has made himself very clear, we do have tried some solutions,
> the problem is:

Yes, I understand already from your previous request. But if I read
the whole thread at
http://groups.google.com/group/vim_use/browse_frm/thread/95fb82624647bab2
there were mentioned several solutions, like Tony's:
http://groups.google.com/group/vim_use/msg/32e2d3ad0041381c
or mine:
http://groups.google.com/group/vim_use/msg/0470265234fd4317

Did you try those?

regards,
Christian

Ben Fritz

unread,
Nov 17, 2010, 9:22:50 AM11/17/10
to vim_use
This is slightly off-topic, but do people actually work at a place
where someone would get upset about adding a single invisible
character with no negative impact on anything at all to the end of a
file, when the file is being changed already anyway? I work at a
company with a fairly strict change management process and nobody has
ever complained to me about adding a newline to the end of a DOS-
format file (or even about changing a mixed-format DOS/Unix file into
a strictly Unix or strictly DOS file). In code review, the reviewer
simply notices "oh, I guess nothing changed on that line" and if they
don't know what happened may think it's strange but nothing a simple
"hey, what did you do here?" can't fix. Hell, even adding to the
commit comment (you do use those, right?) something like "...and added
a newline to the end of the file" can tell the reviewer what happened
with no questions needed.

That said, I'm not really sure why the 'eol' option has no effect
unless you're in binary mode. I cannot think of a good reason for this
to be the case, unless it's just the "it's how vi did it" argument.

Paul

unread,
Nov 18, 2010, 6:55:09 AM11/18/10
to vim_use
On Wed, Nov 17, 2010 at 06:22:50AM -0800, Ben Fritz wrote:
>This is slightly off-topic, but do people actually work at a place
>where someone would get upset about adding a single invisible
>character with no negative impact on anything at all to the end of a
>file, when the file is being changed already anyway?

I used to, and I wouldn't be surprised if the person there is still that anal.

--

.

Bram Moolenaar

unread,
Nov 18, 2010, 4:15:21 PM11/18/10
to Ben Fritz, vim_use

Ben Fritz wrote:

A text file that does not end in a line break is usually considered
"broken". For some definition of "broken". Quite often it means it was
truncated. Or it was written by an editor which I would call "broken".

A binary file does not need to end in a line break. So that's how it
works. And yes, this is old vi behavior.

A line break is certainly not invisible. If it looks that way, your
change management system is hiding that from you.

--
(letter from Mark to Mike, about the film's probable certificate)
For an 'A' we would have to: Lose as many shits as possible; Take Jesus
Christ out, if possible; Loose "I fart in your general direction"; Lose
"the oral sex"; Lose "oh, fuck off"; Lose "We make castanets out of your
testicles"
"Monty Python and the Holy Grail" PYTHON (MONTY) PICTURES LTD

/// Bram Moolenaar -- Br...@Moolenaar.net -- http://www.Moolenaar.net \\\
/// sponsor Vim, vote for features -- http://www.Vim.org/sponsor/ \\\
\\\ an exciting new programming language -- http://www.Zimbu.org ///
\\\ help me help AIDS victims -- http://ICCF-Holland.org ///

Lenin Lee

unread,
Nov 26, 2010, 12:26:33 AM11/26/10
to vim...@googlegroups.com
Sorry to reply late.

Your solution is pretty cool, Christian, that really solves the problem, thank you very much !!!

2010/11/16 Christian Brabandt <cbl...@256bit.org>

regards,
Christian

Reply all
Reply to author
Forward
Message has been deleted
0 new messages