Cut Current Line and Write to New File

0 views
Skip to first unread message

Roy Fulbright

unread,
Jan 11, 2008, 10:13:39 PM1/11/08
to v...@vim.org
I know that ":.w myfile" will write the current line to a new file, but what I want is to write the current line to a new file AND cut it from the current file at the same time. I have searched help and the Vim wiki but cannot find any  single command to do this. Does anyone know how to do this with one command? Thanks in advance.


Share life as it happens with the new Windows Live. Start sharing!

Tony Mechelynck

unread,
Jan 12, 2008, 3:48:26 AM1/12/08
to vim...@googlegroups.com, v...@vim.org
Roy Fulbright wrote:
> I know that ":.w myfile" will write the current line to a new file, but
> what I want is to write the current line to a new file AND cut it from
> the current file at the same time. I have searched help and the Vim wiki
> but cannot find any single command to do this. Does anyone know how to
> do this with one command? Thanks in advance.

If you don't know how to do it with one command (and neither do I), we can
always create a mapping to do it with one key:

:map <F3> :exe ".w! myfile" | .d<CR>

The ":execute" wrapper around the ":write" command is necessary because
otherwise the bar would be seen as an argument to ":w", see ":help :|".

Best regards,
Tony.
--
"Ah, you know the type. They like to blame it all on the Jews or the
Blacks, 'cause if they couldn't, they'd have to wake up to the fact
that life's one big, scary, glorious, complex and ultimately
unfathomable crapshoot -- and the only reason THEY can't seem to keep
up is they're a bunch of misfits and losers."
-- A analysis of Neo-Nazis, from "The Badger" comic

Charles E. Campbell, Jr.

unread,
Jan 12, 2008, 9:04:17 AM1/12/08
to vim...@googlegroups.com
Tony Mechelynck wrote:

>Roy Fulbright wrote:
>
>
>>I know that ":.w myfile" will write the current line to a new file, but
>>what I want is to write the current line to a new file AND cut it from
>>the current file at the same time. I have searched help and the Vim wiki
>>but cannot find any single command to do this. Does anyone know how to
>>do this with one command? Thanks in advance.
>>
>>
>
>If you don't know how to do it with one command (and neither do I), we can
>always create a mapping to do it with one key:
>
> :map <F3> :exe ".w! myfile" | .d<CR>
>
>The ":execute" wrapper around the ":write" command is necessary because
>otherwise the bar would be seen as an argument to ":w", see ":help :|".
>
>

Or you could use <bar>, as in

:map <f3> w! myfile<bar> .d<cr>

Regards,
Chip Campbell

Tony Mechelynck

unread,
Jan 12, 2008, 9:24:34 AM1/12/08
to vim...@googlegroups.com

You should use <Bar> because ":map" doesn't see | as part of its arguments,
but you should also wrap the ":write" statement because it does see | (which
is what ":map" would transform <Bar> into) as part of _its_ arguments. So:

:map <F3> :exe ".w! myfile" <Bar> .d<CR>


Best regards,
Tony.
--
People will buy anything that's one to a customer.

Roy Fulbright

unread,
Jan 12, 2008, 11:34:58 AM1/12/08
to vim...@googlegroups.com
Thank you all for your help. I works great!
V/R,
Roy Fulbright

> Date: Sat, 12 Jan 2008 15:24:34 +0100
> From: antoine.m...@gmail.com
> To: vim...@googlegroups.com
> Subject: Re: Cut Current Line and Write to New File

Gary Johnson

unread,
Jan 14, 2008, 12:30:10 PM1/14/08
to vim...@googlegroups.com
On 2008-01-11, Roy Fulbright <rful...@hotmail.com> wrote:
> I know that ":.w myfile" will write the current line to a new file, but what I
> want is to write the current line to a new file AND cut it from the current
> file at the same time. I have searched help and the Vim wiki but cannot find
> any single command to do this. Does anyone know how to do this with one
> command? Thanks in advance.

!!cat > newfile

which will overwrite newfile or

!!cat >> newfile

which will append the line to newfile.

HTH,
Gary

Ben Schmidt

unread,
Jan 14, 2008, 10:49:55 PM1/14/08
to vim...@googlegroups.com
> !!cat > newfile
>
> which will overwrite newfile or

if it isn't actually a new file...

> !!cat >> newfile
>
> which will append the line to newfile.

if it isn't actually a new file...

Grins,

Ben.

Send instant messages to your online friends http://au.messenger.yahoo.com

Reply all
Reply to author
Forward
0 new messages