Replacing current line with last-yanked register

43 views
Skip to first unread message

Dotan Cohen

unread,
Oct 14, 2012, 6:38:00 AM10/14/12
to vim use
I am currently using Vp to replace the current line with the
last-yanked register (usually populated by simply Y on another line).
However, these two disadvantages are apparent:
1) This can only be done once. After pasting, the last-yanked register
becomes the text of the replaced line.
2) This operation cannot be repeated with the '.' key.

Is there a simple (no more than two or three keystrokes) way to
replace the current line with last-yanked register, in a manner that
does not have one or both of the disadvantages stated above?

Thanks!

--
Dotan Cohen

http://gibberish.co.il
http://what-is-what.com

Marcin Szamotulski

unread,
Oct 14, 2012, 7:54:36 AM10/14/12
to vim...@googlegroups.com
If you yanked the line then it is still in the 0 register. You can paste
from it. Though this will be four keystrokes: V"0p

If you do that often you could use a map:

vnoremap P "0p

and then there are only two keystrokes: VP

Best,
Marcin
> --
> 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

Dotan Cohen

unread,
Oct 14, 2012, 9:16:02 AM10/14/12
to vim...@googlegroups.com, Marcin Szamotulski
On Sun, Oct 14, 2012 at 1:54 PM, Marcin Szamotulski <msz...@gmail.com> wrote:
> If you yanked the line then it is still in the 0 register. You can paste
> from it. Though this will be four keystrokes: V"0p
>
> If you do that often you could use a map:
>
> vnoremap P "0p
>
> and then there are only two keystrokes: VP
>
> Best,
> Marcin
>

Thank you Marcin. I don't want to create a map as I've already got
every short combination that is mappable mapped and used often!

`V"0p` would be a good solution if it were repeatable with the period
character. Is there any solution which is repeatable by the period
character? I tried `I<C-o>Vp` with the hope that the `i` would make it
repeatable, but this is not so!

Marcin Szamotulski

unread,
Oct 14, 2012, 9:30:16 AM10/14/12
to Vim Use
> Thank you Marcin. I don't want to create a map as I've already got
> every short combination that is mappable mapped and used often!
>
> `V"0p` would be a good solution if it were repeatable with the period
> character. Is there any solution which is repeatable by the period
> character? I tried `I<C-o>Vp` with the hope that the `i` would make it
> repeatable, but this is not so!
>
> --
> Dotan Cohen
>
> http://gibberish.co.il
> http://what-is-what.com

Yes there is one, you could use C at the beginning of a line, but you
have to yank the line with y$ rather than Y (to not catch the EOL)
Explicitly you can use this: press C, now you are in the insert mode,
and press ^r0 (i.e. CTRL-R0) and ESC. The `-` might also be helpful, it
jumps to the beginning of the previous line.

Best,
Marcin

Dotan Cohen

unread,
Oct 14, 2012, 10:57:48 AM10/14/12
to vim...@googlegroups.com
On Sun, Oct 14, 2012 at 3:30 PM, Marcin Szamotulski <msz...@gmail.com> wrote:
> Yes there is one, you could use C at the beginning of a line, but you
> have to yank the line with y$ rather than Y (to not catch the EOL)
> Explicitly you can use this: press C, now you are in the insert mode,
> and press ^r0 (i.e. CTRL-R0) and ESC. The `-` might also be helpful, it
> jumps to the beginning of the previous line.
>

Great, Marcin, thank you! I must do `0.` for each line but that is fine.

This is quite why I love VIM and don't even consider any other text
editor even with all my contemporaries on IDEs. I _felt_ that there
had to be a simple way to do this task in VIM. That's just VIM. Thank
you Marcin for showing me the bits of VIM that I was missing.

Cesar Romani

unread,
Oct 15, 2012, 8:40:51 AM10/15/12
to vim...@googlegroups.com
In tha case you could use S instead of C, then CTRL-R0 and ESC. Then you
only need to use '.'

--
Cesar

Dotan Cohen

unread,
Oct 15, 2012, 9:30:27 AM10/15/12
to vim...@googlegroups.com
On Mon, Oct 15, 2012 at 2:40 PM, Cesar Romani <cesar....@gmail.com> wrote:
> In tha case you could use S instead of C, then CTRL-R0 and ESC. Then you
> only need to use '.'
>

Thanks, Cesar, that ties up the last missing piece of this puzzle! Terrific!

Ben Schmidt

unread,
Oct 15, 2012, 7:34:52 PM10/15/12
to vim...@googlegroups.com, Dotan Cohen, Marcin Szamotulski
Well, if instead of yanking with yy, you yank with 0y$ or something like
that, so you don't have the end-of-line in the buffer, you could put it
in a repeatable way with S<C-r>0 I suppose.

Ben.



Dotan Cohen

unread,
Oct 16, 2012, 5:18:28 AM10/16/12
to Ben Schmidt, vim...@googlegroups.com, Marcin Szamotulski
On Tue, Oct 16, 2012 at 1:34 AM, Ben Schmidt
<mail_ben...@yahoo.com.au> wrote:
> Well, if instead of yanking with yy, you yank with 0y$ or something like
> that, so you don't have the end-of-line in the buffer, you could put it
> in a repeatable way with S<C-r>0 I suppose.
>

‎Thanks, Ben. That is the exact solution that I'm now using!

ping

unread,
Oct 16, 2012, 8:48:33 AM10/16/12
to vim...@googlegroups.com, Ben Schmidt, Dotan Cohen, Marcin Szamotulski
this is a really nice tip , even if sound simple, vim-golf best practice
so far I heard.
I'll start re-train my finger on this...
not sure we have a place to maintain these "best-practice" stuffs that
people spend time to work out?

Dotan Cohen

unread,
Oct 16, 2012, 12:02:29 PM10/16/12
to ping, vim...@googlegroups.com, Ben Schmidt, Marcin Szamotulski
On Tue, Oct 16, 2012 at 2:48 PM, ping <songpi...@gmail.com> wrote:
> this is a really nice tip , even if sound simple, vim-golf best practice so
> far I heard.
> I'll start re-train my finger on this...
> not sure we have a place to maintain these "best-practice" stuffs that
> people spend time to work out?
>

There is the VIM-tips wiki, but that is more formatted for large, complex tips.

If you know any handy one-liners, send them to me. I have a few tens
of VIM one-liners and short tips like this that I will put on
dotancohen.com when I move it over to a new server (already
provisioned). There is no advertising on that site, just some helpful
tips for and by the community.

Christian Brabandt

unread,
Oct 16, 2012, 1:11:53 PM10/16/12
to vim...@googlegroups.com
Hi Dotan!
I guess, there would nothing be wrong with creating a One-Liners Page
for Vim at the wiki similar to the sed-1-liners.txt that is floating
around the net.

regards,
Christian

Dotan Cohen

unread,
Oct 16, 2012, 1:41:07 PM10/16/12
to vim...@googlegroups.com
On Tue, Oct 16, 2012 at 7:11 PM, Christian Brabandt <cbl...@256bit.org> wrote:
> I guess, there would nothing be wrong with creating a One-Liners Page
> for Vim at the wiki similar to the sed-1-liners.txt that is floating
> around the net.
>

That would make sense too. I just looked through the FAQ and a few
other pages, there seems to be no restriction about including multiple
tips on one page.

Andy Wokula

unread,
Oct 16, 2012, 1:43:19 PM10/16/12
to vim...@googlegroups.com
Just to mention it ... zzapper's list is still alive:
http://www.rayninfo.co.uk/vimtips.html

--
Andy

Ben Fritz

unread,
Oct 16, 2012, 3:05:29 PM10/16/12
to vim...@googlegroups.com
On Tuesday, October 16, 2012 12:41:16 PM UTC-5, dotancohen wrote:
> On Tue, Oct 16, 2012 at 7:11 PM, Christian Brabandt <cbl...@256bit.org> wrote:
>
> > I guess, there would nothing be wrong with creating a One-Liners Page
>
> > for Vim at the wiki similar to the sed-1-liners.txt that is floating
>
> > around the net.
>
> >
>
>
>
> That would make sense too. I just looked through the FAQ and a few
>
> other pages, there seems to be no restriction about including multiple
>
> tips on one page.
>

The best example that comes to mind:
http://vim.wikia.com/wiki/Short_mappings_for_common_tasks

There's also this, which I've never really liked but seems to get a lot of traffic:
http://vim.wikia.com/wiki/Best_Vim_Tips

John Beckett

unread,
Oct 16, 2012, 5:44:13 PM10/16/12
to vim...@googlegroups.com
Ben Fritz wrote:
> The best example that comes to mind:
> http://vim.wikia.com/wiki/Short_mappings_for_common_tasks
>
> There's also this, which I've never really liked but seems to
> get a lot of traffic:
> http://vim.wikia.com/wiki/Best_Vim_Tips

Best_Vim_Tips is an older version of Zapper's tips mentioned in
another post (with edits at the wiki).

I added the idea here:
http://vim.wikia.com/wiki/Repeat_last_change

although I was also thinking of putting it in the following
(and making it more generic than just replacing a word):
http://vim.wikia.com/wiki/Replace_a_word_with_yanked_text

John

Alejandro Exojo

unread,
Oct 21, 2012, 11:43:33 AM10/21/12
to vim...@googlegroups.com
El Domingo, 14 de octubre de 2012, Dotan Cohen escribió:
> Is there a simple (no more than two or three keystrokes) way to
> replace the current line with last-yanked register, in a manner that
> does not have one or both of the disadvantages stated above?

Do all of yourselves a favor and invest a little bit of time installing the
operator-replace plugin:

https://github.com/kana/vim-operator-replace

Note that also requires this plugin to work:
https://github.com/kana/vim-operator-user


I was skeptical when I installed it, and I've realized that I can't imagine my
editing without it in my toolbox. It can replace everything you want from any
register and replacing the contents of a text object.

Is too good for words, really. I strongly recommend it. And Kana is one of the
best plugin writers that I know.

--
Alex (a.k.a. suy) | GPG ID 0x0B8B0BC2
http://barnacity.net/ | http://disperso.net

stosss

unread,
Oct 22, 2012, 3:03:16 AM10/22/12
to vim...@googlegroups.com
Any URLs for the sed 1 liners?

Amit Agarwal

unread,
Oct 22, 2012, 4:26:59 AM10/22/12
to vim...@googlegroups.com
Hi,

> Any URLs for the sed 1 liners?

First hit for googling on sed 1 liners :
http://sed.sourceforge.net/sed1line.txt

Thanks,

Amit Agarwal

Websites: amit-agarwal.co.in
amit-agarwal.com

Blog: blog.amit-agarwal.co.in

Reply all
Reply to author
Forward
0 new messages