9

Lately I've found myself working more and more with remote servers over SSH. Most of those severs don't have Vim, but just Vi. Using it hasn't been too rough with just a few problems.

One is this:

How to replace a word with a yanked word in normal mode

The accepted answer (and the way I've always done things) uses visual mode which often isn't compiled into what I need to use (yank text, visual over text to be replace, put).

How can I do this in Vi?

7
  • 5
    Did you see statox's answer? Because that seems like something that would work for you! Commented Jan 20, 2016 at 7:34
  • 3
    Use cw and then paste/put from your register? Commented Jan 20, 2016 at 7:59
  • 3
    Just in case, are you aware that you can open remote files with vim? See :h scp Commented Jan 20, 2016 at 9:51
  • @Carpetsmoker yes I saw it, but I was hoping for something less typey.
    – Robin
    Commented Jan 20, 2016 at 14:26
  • 2
    What about Pde? if you yanked the word word with something like yw then in most cases it will include the space after word, so if you move to another word and do Pde you will insert new one and erase existing. In some cases you may need Plde - paste, move left and then delete util the end. Commented Jan 20, 2016 at 18:14

1 Answer 1

9

This is answered to some degree in statox's answer to the question you linked.

The key to your question is an understanding of vi's registers.

When you yank text, it gets placed in register 0, and the unnamed register gets pointed to register 0. When you delete text, it gets placed in register 1, and the unnamed register gets pointed to register 1.

When you "put" text, it gets pasted from the unnamed register by default.

However, you can specify what register you want to paste from or copy into. That's what the double-quote (") key does: specify a register.

So there are several simple approaches, any of which will work. The two basic approaches are:

  1. Yank the text you are going to paste just as you usually would (perhaps with yw, delete the text you want to overwrite just as you usually would (with dw), then paste from register 0 by typing "0P or "0p.
  2. Yank the text as usual with yw, then delete the text you want to overwrite into the black hole register by typing "_dw, then paste with p or P.

You could also yank into a named register ("kye to yank into register k, for example) and then paste from that register.

If you want the operation to be repeatable with the . command and perform it in one step, you could also use "_ce to "change to the end of the word", dumping the word being changed into the black hole register, and then type <C-r>0<Esc> while in insert mode to paste from register 0 and exit insert mode.

EDIT: As pointed out in the comments, some of the above approaches may use Vim extensions. An approach that strictly uses POSIX vi features but is perhaps not as handy is, yank the word you want into a named register (I'll say register k) by using "kye, then delete the word you don't want with de, and paste in the word you yanked with "kP.

3
  • 1
    Again, this does not work with original vi Commented Jan 21, 2016 at 8:24
  • 1
    @ChristianBrabandt, how have you determined that? POSIX specs for vi include registers. (They're called "buffers" which can be confusing since there are also "editing buffers", but they're there.) What part of my answer specifically doesn't work in what version of vi?
    – Wildcard
    Commented Jan 21, 2016 at 8:38
  • 1
    <c-r>0 is a vim extension. black-hole register is a vim extensions. I am not sure about register 0 Commented Jan 21, 2016 at 8:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.