UNIX - How to copy and paste between different bash windows with files opend with VI?

4

1

Lets say I log in to bash, open a file in vi, then using alt-f2 I open a new terminal. After logging in I navigate to a second file and open that in vi. How can I CnP between these two files?

I found this from wikia.com. I'm not familiar with this command shift-8-y-y. I follow the vimtutor and use virtual mode. I tried this sequence and was unsuccessful.

And, I read this about registers. I found a mention of using double_quote-p to 'put' or paste the register, but this does not work in a different file. The second file reported the register empty.

Alternatively, I read in the VIM docs, it is possible to open multiple files under split screens. This may be a course to a solution. The need I often encounter has me navigating in a different bash window to find a file, then wishing to copy between the two. Whereas, I imagine, a split VIM window useful for files in the same directory.

xtian

Posted 2011-06-15T22:15:05.507

Reputation: 782

I think the two sessions would have to be in the same terminal to use copy and past in vi. You can try an experiment where you have two terminals open, use yy to copy a line into a buffer and see that pasting in each window results in the content that was yanked in that window. You could of course use the windowing enviroment to copy and paste (say CNTL-C and CNTL-V and that would work if you put vi into its correct mode before the pasting). – Peter Grill – 2011-06-15T22:19:56.403

FYI, you don't really "log into bash", and there is no such thing as a "bash window". Do you mean Linux console? – Keith – 2011-06-15T23:29:48.577

(>_<) Linux console sounds good. And, to "open a new console" and "I have multiple consoles open." would also be correct terminology? Is console synonymous with terminal? To me, ANYTHING which displays on a monitor is generically a window. ehehe. – xtian – 2011-06-16T00:41:19.750

Consoles and terminals are not synonymous in the Unix/Linux worlds. You're talking about Linux "virtual terminals". – JdeBP – 2011-06-16T09:38:32.453

Answers

1

You cannot do it unaided in a standard generic way with the vim you have.

Firstly make sure you have a good version compiled and installed on your distro.

Then I would suggest either using gvim with separate windows, or using vim -o to open multiple files and just between the frames using ^W^W to switch and then copy and paste.

Marius

Posted 2011-06-15T22:15:05.507

Reputation: 891

3

It's no wonder that it didn't work for you. You're mis-spelling the commands. It's not shift-8-y-y. Nor is it double_quote-p. The commands are:

  • Yank current line to X11 clipboard: "+yy
  • Paste from X11 clipboard: "+p
  • Yank current line to X11 (primary) selection: "*yy
  • Paste from X11 (primary) selection: "*p

Modify the motion part according to taste. vim has to know that an X server is there, of course. So you'll have to explicitly tell it where the X server is and what display on that server to use if you aren't running it from within an X session where it implicitly inherits the DISPLAY environment variable and so forth.

JdeBP

Posted 2011-06-15T22:15:05.507

Reputation: 23 855

This is running on a laptop which boots to the terminal. Are you referring to what garyjohn called the "X Windows system clipboard" when you refer to the X server? – xtian – 2011-06-16T00:46:34.903

And, I tried the commands "+yy and "*yy; when I type "+ together then y I get "E78 unknown mark" error; If I type "+y then y I get E20 Mark not set error. I am close? – xtian – 2011-06-16T00:47:02.297

You're still mis-spelling the commands. I wrote " yet you're typing '. What garyjohn says about different binaries compiled with and without X support is important, too. – JdeBP – 2011-06-16T09:22:22.723

Then what is *? – xtian – 2011-06-16T17:54:53.677

1

You may want to install the "general purpose mouse" (gpm) package, which allows the use of mouse copy-and-paste without a GUI.

CarlF

Posted 2011-06-15T22:15:05.507

Reputation: 8 576

this will not work with larger buffers... And when pasting it may reformat as-you-type undesirably. – Marius – 2011-06-15T23:19:32.557

If you're worried about the formatting, vim in vi emulation mode may not have the 'paste' option, but still has the = operator. – evaryont – 2011-06-16T05:32:34.397

1

The vi that's included in many GNU/Linux distributions is a tiny version of Vim which has been compiled without many of Vim's features, including the interface to the X Window System's clipboard. Even their terminal-mode vim program is often built without support for X. Without support for X, your vi and vim will be unable to access the clipboard and you will be unable to copy and paste between different instances of the program.

One solution to that is to run the gvim program in terminal mode by using the -v option, as

$ gvim -v your_file

To avoid typing that, or forgetting to type that, every time, you could add these aliases to your ~/.bashrc file:

alias vim='gvim -v'
alias vi='gvim -v'

Once you're using a version of Vim compiled with support for X, you can use the commands described in JdeBP's answer. You can also read about those command's in Vim's internal user manual and reference manual by executing

:help 04.7
:help 09.3

garyjohn

Posted 2011-06-15T22:15:05.507

Reputation: 29 085

To use the X system's clipboard wouldn't X need to be running, then? If I'm using X I don't suffer this problem, I can simply use the mouse :P – xtian – 2011-06-16T18:15:00.763

Yes. When you wrote about opening a new terminal and using different bash windows, I overlooked that you were using Alt-F2 from a console to open another console and assumed that you were opening terminal windows in some sort of X window manager. My mistake. As for the mouse, I try to avoid using it, so I copy and paste between my various Vim windows with the yank and put commands. – garyjohn – 2011-06-16T18:31:10.853

1

Since you seem to be using the console (no X windows, so many of the other answers are not relevant) you might just edit the multiple files in one vim session. Try :split ... to edit the two files at once and use vim's cut buffers to move text between them.

Alternatively you can use gpm, and cut and past between consoles.

Keith

Posted 2011-06-15T22:15:05.507

Reputation: 7 263

0

I don't know about vi, but with emacs you can have the other terminals attach to the primary emacs session with emacsclient and then they will all share the same kill ring ( and buffers, etc ) so you can kill and yank between them.

psusi

Posted 2011-06-15T22:15:05.507

Reputation: 7 195