3

To copy a line to the alternate file in original vi I use something like

"ayy
:e#
"aP

The first vi version did have the alternate file feature (with switch command :e`) but did not have named buffers. Is there a reasonable way to copy lines between two files in original vi without using named buffers?

5
  • "To copy a line to the alternate file in original vi I use something like" did you mean "in vim"?
    – romainl
    Jun 19, 2016 at 21:53
  • No, in vim copying between buffers works as simple as copying in just one file. In original vi you can't just use Y to copy between files since the copy buffer is cleared when switching to the alternate file. Jun 19, 2016 at 22:02
  • I don't know the answer, but could you not just add a mapping for this? Jun 20, 2016 at 8:28
  • Macros (:map) had been introduced in version 3.1. My question is regarding version 1.1. (Not related: I thought that something like :3,2w temp, `` :e` ``, :r temp could do the job, but this sets the alternate filename to temp, which is undesired...) Jun 20, 2016 at 10:43
  • You can use :r!/bin/cat file instead of :read to avoid setting the alternate file.
    – Antony
    Jun 20, 2016 at 23:09

1 Answer 1

3

When I used to use a real vi, I'd write to and read from a file, like you mention in your comment. I had something like this in my .exrc:

ab wtemp w !/bin/cat > ~/temp
ab rtemp r !/bin/cat ~/temp

So to yank the current line:

:.wtemp

Or set marks, then:

:'a,'b wtemp

Then hit ^6 to switch to the alternate file (same as :e #), then:

:rtemp

You can change the abbreviations to mappings if that's easier.

Note that using :read ! and :write ! with cat avoids setting the alternate file.

As an aside, should you ever use Vim, you can remove the a flag from 'cpoptions' to prevent :read with a filename from setting the alternate file, and similarly with A for writing.

EDIT

Looking at the reference manual for v1.1, the command set appears to be highly restrictive. The only way I can see to achieve the copy in this early version of vi is to append to the alternate file:

:[range] write! >> `

Then switch to the alternate file and move the appended lines to where you want.

5
  • I gave a +1 for the very useful information on later vi versions (especially ^6), but unfortunately it doesn't work with with ex-1.1 (github.com/n-t-roff/ex-1.1). This version did not have :ap, and w ! /bin/cat > ~/temp gives the error "Multiple file names allowed only on next command". Jun 21, 2016 at 8:32
  • Thanks for the reference. That is indeed a highly restricted command set. Maybe it's time for an upgrade! :-) See the edit for a possible workaround; I can't test it myself however. I'm assuming ` is expanded to the alternate filename for :write like for :! -- the manual isn't clear.
    – Antony
    Jun 21, 2016 at 10:10
  • It does unfortunately remove all other lines from the alternate file. Nethertheless I accepted the answer. I has very useful information and in the original question I forgot to restrict the problem to vi version 1. Jun 21, 2016 at 10:39
  • Thanks! That's unfortunate: the manual claims it should append. Oh well!
    – Antony
    Jun 21, 2016 at 10:55
  • It does. It was a porting bug--the lseek(2) return value differs from seek(2). Jun 30, 2016 at 14:56

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.