2

I asked a question a few days ago in the Linux/Unix SE about how to copy a block of text from one file to another in a way that I could use for a script of many similar files. I thought I came up with a pretty simple solution, but I have run into a snag: my code seems to work only the first time I execute it. Any time after it won't seem to do. For an example, here is the gist of my code:

ex -c "normal! 1gg19|^V48gg59|y" -cq file1

This goes into file1, creates a visual block from the point (1,19) to (48,59) in the file, yanks it, and then closes the file.

ex -c "normal! 9gg21|p" -cwq file2

This goes into file2, moves the cursor to the point (9,21), pastes, and then closes the file.

If I run this once, it seems to work fine (minus any hiccups I cause by messing up the line numbers). But then if I run it again, neither line of code (in particular the first line) does anything. I have tried performing what each line of code is supposed to do in just plain vi, but then the other line will do nothing.

One particular issue I've noticed is if I run the first line and then try to manually perform the second, I get an error message "E353: Nothing in register"

7
  • 1
    When you give the code snippet, are you running that from a command line I.e. vim ex -c"... or are you running it from vim, :ex -c "... or is it in a vimrc ex -c"...?
    – Tumbler41
    Commented Jun 13, 2017 at 21:26
  • @Tumbler41 I'm running these from the command line. The end goal was to incorporate them into a for loop.
    – Tyberius
    Commented Jun 13, 2017 at 21:41
  • Ok, the ex confused me for a bit, but I'm assuming that ex is just aliased to vim on your system? (That's standard for Linux machines these days right?)
    – Tumbler41
    Commented Jun 13, 2017 at 22:02
  • 1
    @Tumbler41 yeah it just opens the file in ex mode.
    – Tyberius
    Commented Jun 13, 2017 at 22:09
  • 1
    Could not reproduce on Windows7 Vim 8.0.586. I asked some people to try it on Linux, but they haven't got back.
    – Tumbler41
    Commented Jun 16, 2017 at 20:56

1 Answer 1

1
+50

I'm not sure how your approach could work as it would mean the unnamed register is available between two separate 'ex' processes.

If you can do a single invocation this works:

ex -c 'normal! 1gg19|^V48gg59|y' -c 'edit file2' -c 'normal! 9gg21|p' -cwq file1

Note: I tried your way except putting it in the star (*) register thinking the clipboard would persist it across ex calls but that didn't work for me.

5
  • 1
    Vim saves away registers in the .viminfo file. So the unnamed register is actually saved between instances.
    – Tumbler41
    Commented Jun 19, 2017 at 15:31
  • 1
    Before I answered I wondered if that was the case but when I tried it it did not work. The text does not get copied to the second file. For that matter it wasn't in my .viminfo file either. Thus I assumed it wasn't correct and answered with the single-command version. Maybe OP would be better off going that direction since at least two of us have issues with the two command approach. :)
    – B Layer
    Commented Jun 20, 2017 at 0:17
  • @BlairM. I gave this method a try and ran into the same problem; it seems to run, but then there is no change to the file1.
    – Tyberius
    Commented Jun 21, 2017 at 14:54
  • Do you mean file2? BTW, I just noticed the :wv and :rv commands. Maybe these can be used with the two-command use case. Check out :help viminfo-read-write. (Sorry I'm in a hurry...can't try it out myself until later.)
    – B Layer
    Commented Jun 21, 2017 at 19:27
  • @Tyberius :wv/rv make a lot of sense and I had high hopes to get the two-liner to work...but no luck. The register is just not read from viminfo with the second command so the put to file2 just complains about empty register. My one liner works great for me, though. Maybe you should focus on getting that to work. BTW, if you leave off the 'q' command you will still be in ex and you'll be able to easily read any. errors/messages, examine registers, etc.
    – B Layer
    Commented Jun 22, 2017 at 9:36

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.