4

I am familiar with the y, p method of copying and pasting a line within Vim; however, I have been unsuccessful in finding a straight forward method for copying multiple lines which have been yanked in visual mode.

the idea would be:

Shift + v
down arrow n times
y
:q!
open new file, or switch buffer
p

When yanking with visual mode, it will copy the first line of the yanked block to your Registers in .viminfo, but nothing else

I would assume you need to create an instance of a clipboard which is global to all sessions of vim attached to a specific user, but I am not well versed enough in Vim to code this up myself.

edit: As it would appear, I made a mistake in my approach. Using Shift+v is the prescribed method to use when copying and pasting between sessions. Thanks for the help @Doorknob

3
  • What do you mean exactly with 'between sessons'? That you have 2 Vim processes open next to each other, and want to share registers somehow? Or that you want your registers from the previous (now closed) Vim process? ... Also, I am not an alien! I AM A HUMAN EARTH MONKEY LIKE YOU! hehe ;-) Feb 8, 2015 at 21:57
  • The use case would be: I have two files open in VIM. I have a configuration block I need to copy from one to the other. I dont necessarily need to close the first buffer. I simply need the lines of text to be copied to a global space where I can then paste it to the second buffer. also, what's a squeedlyspooch? Feb 8, 2015 at 23:13
  • 1
    You could yank the stuff to the system clipboard by using "+y and paste it into which ever other application (vim included) you want.
    – Bhargav
    Feb 9, 2015 at 4:13

2 Answers 2

4

Assuming you have a viminfo file (located at ~/.viminfo on Linux/Unix systems and $HOME\_viminfo on Windows), all of your buffers should be persistent across Vim sessions.

Note that if you're on a Linux/Unix system, this file must be owned by you (ls -l ~/.viminfo to verify that it is and chown $USER ~/.viminfo if it's not). Otherwise, it will have no effect.

7
  • So, I would define a "clipboard" of sorts which VIM knows about in my dot file. I could alias a command to copy text to the "clipboard", then globally I would have access to the text? Feb 8, 2015 at 23:19
  • @EricSabelhaus As long as your .viminfo is set up properly (just create an empty file called .viminfo in your ~ and Vim will do the rest), you can simply use y in one session of Vim, quit, and then p in another session later. (Of course, if you have two buffers open in the same Vim session, no .viminfo is necessary, simply yank some text, switch to your other buffer, and paste it there.)
    – Doorknob
    Feb 8, 2015 at 23:40
  • Fantastic! will this also work when coping using visual mode? Feb 9, 2015 at 1:56
  • Also, was this implemented recently? I vaguely remember trying to get this working about 2-3 months ago and pulling my hair out Feb 9, 2015 at 1:57
  • :( welp, part way answered the question... let me edit the question to be more specific. Feb 9, 2015 at 2:02
4

I know this is old but I came across it and thought a different option might be useful for other people searching for this question.

I would yank to the system register and then open the new file and paste from the system register.

First find out what symbol represents your system clipboard. In Linux you have two options + or *. I use + but more info can be found here: https://superuser.com/questions/624231/in-vim-what-is-the-difference-between-and-registers. For Windows Mac + and * are the same thing.

Then instead of yanking with y yank with "+y and instead of paste p use "+p

Your Answer

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

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