Writing files without EOL

classic Classic list List threaded Threaded
20 messages Options
Reply | Threaded
Open this post in threaded view
|

Writing files without EOL

Peter Hodge-2
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


     

--
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
Reply | Threaded
Open this post in threaded view
|

Re: Writing files without EOL -> :set noeol

Sven Guckes-3
* Peter Hodge <[hidden email]> [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

--
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
Reply | Threaded
Open this post in threaded view
|

Re: Writing files without EOL

Stahlman Family
In reply to this post by Peter Hodge-2


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

>
> 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
>
>
>      
>

--
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
Reply | Threaded
Open this post in threaded view
|

Re: Writing files without EOL

Tim Chase
In reply to this post by Peter Hodge-2
> 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





--
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
Reply | Threaded
Open this post in threaded view
|

Re: Writing files without EOL -> :set noeol

Peter Hodge-2
In reply to this post by Sven Guckes-3
--- On Mon, 16/8/10, Sven Guckes <[hidden email]> wrote:

> From: Sven Guckes <[hidden email]>
> Subject: Re: Writing files without EOL -> :set noeol
> To: "vim_use" <[hidden email]>
> Received: Monday, 16 August, 2010, 11:13 AM
> * Peter Hodge <[hidden email]>
> [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"

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


     

--
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
Reply | Threaded
Open this post in threaded view
|

Re: Writing files without EOL

Stahlman Family
In reply to this post by Stahlman Family


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]...
>>
>>
>>      
>

--
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
Reply | Threaded
Open this post in threaded view
|

Re: Writing files without EOL

Peter Hodge-2
In reply to this post by Stahlman Family
--- On Mon, 16/8/10, Stahlman Family <[hidden email]> wrote:

> From: Stahlman Family <[hidden email]>
> Subject: Re: Writing files without EOL
> To: [hidden email]
> 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


     

--
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
Reply | Threaded
Open this post in threaded view
|

Re: Writing files without EOL

Peter Hodge-2
In reply to this post by Tim Chase
--- On Mon, 16/8/10, Tim Chase <[hidden email]> wrote:

> From: Tim Chase <[hidden email]>
> Subject: Re: Writing files without EOL
> To: [hidden email]
> Cc: "Peter Hodge" <[hidden email]>
> 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


     

--
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
Reply | Threaded
Open this post in threaded view
|

Re: Writing files without EOL -> :set noeol

Tony Mechelynck
In reply to this post by Peter Hodge-2
On 16/08/10 03:18, Peter Hodge wrote:

> --- On Mon, 16/8/10, Sven Guckes<[hidden email]>  wrote:
>
>> From: Sven Guckes<[hidden email]>
>> Subject: Re: Writing files without EOL ->  :set noeol
>> To: "vim_use"<[hidden email]>
>> Received: Monday, 16 August, 2010, 11:13 AM
>> * Peter Hodge<[hidden email]>
>> [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"
>
> 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
>
>
>
>

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

--
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
Reply | Threaded
Open this post in threaded view
|

Re: Writing files without EOL -> :set noeol

Peter Hodge-2


--- On Mon, 16/8/10, Tony Mechelynck <[hidden email]> wrote:

> From: Tony Mechelynck <[hidden email]>
> Subject: Re: Writing files without EOL -> :set noeol
> To: [hidden email]
> Cc: "Peter Hodge" <[hidden email]>
> Received: Monday, 16 August, 2010, 1:48 PM
> On 16/08/10 03:18, Peter Hodge
> wrote:
> > --- On Mon, 16/8/10, Sven Guckes<[hidden email]
> wrote:
> >
> >> From: Sven Guckes<[hidden email]>
> >> Subject: Re: Writing files without EOL -> 
> :set noeol
> >> To: "vim_use"<[hidden email]>
> >> Received: Monday, 16 August, 2010, 11:13 AM
> >> * Peter Hodge<[hidden email]>
> >> [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"
> >
> > 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
> >
> >
> >
> >
>
> 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.

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


     

--
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
Reply | Threaded
Open this post in threaded view
|

Re: Writing files without EOL -> :set noeol

Christian Brabandt
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."

--
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
Reply | Threaded
Open this post in threaded view
|

Re: Writing files without EOL

Lenin Lee
In reply to this post by Peter Hodge-2
I am mad with this problem too, exactly the same with Peter's.

No one has any solution ?

2010/8/16 Peter Hodge <[hidden email]>
--- On Mon, 16/8/10, Tim Chase <[hidden email]> wrote:

> From: Tim Chase <[hidden email]>
> Subject: Re: Writing files without EOL
> To: [hidden email]
> Cc: "Peter Hodge" <[hidden email]>
> 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




--
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

--
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
Reply | Threaded
Open this post in threaded view
|

Re: Writing files without EOL

Christian Brabandt
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

--
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
Reply | Threaded
Open this post in threaded view
|

Re: Writing files without EOL

Lenin Lee
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 <[hidden email]>
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

--
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

--
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
Reply | Threaded
Open this post in threaded view
|

Re: Writing files without EOL

Christian Brabandt
(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

--
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
Reply | Threaded
Open this post in threaded view
|

Re: Writing files without EOL

Benjamin Fritz
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.

--
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
Reply | Threaded
Open this post in threaded view
|

Re: Writing files without EOL

Paul-425
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.

--

.

--
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
Reply | Threaded
Open this post in threaded view
|

Re: Writing files without EOL

Bram Moolenaar
In reply to this post by Benjamin Fritz

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 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.

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 -- [hidden email] -- 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    ///

--
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
Reply | Threaded
Open this post in threaded view
|

Re: Writing files without EOL

Lenin Lee
In reply to this post by Christian Brabandt
Sorry to reply late.

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

2010/11/16 Christian Brabandt <[hidden email]>
(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

--
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

--
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
Reply | Threaded
Open this post in threaded view
|

Re: Writing files without EOL

Till Maas
In reply to this post by Benjamin Fritz
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 work at a

There are other reasons to want to create such files or why this change can
break stuff. I noticed this problem when a file containing a prefix of an
HTTP request was meant to be edited. It contains \r\n line endings, but the
last line did not contain \r\n intentionally.

Regards
Till


--
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