简体   繁体   中英

copy/paste between two instances of console vim

I have 2 opened windows with ssh sessions to the same remote linux box (putty). Console vim editor is running in both windows. I'd like to yank text in one window and paste it in another window. I don't like to copy/paste terminal screen using mouse. Are there any functionality in vim to do this?

If both vim sessions are running on the same system, you can write a portion of the buffer to a file in one session ( :w tmp ), then read the file into the buffer in the other ( :r tmp ). If they're running in two different directories, you'll need to specify a full path name.

A simple :w tmp will write the entire contents of the buffer. To write just a subset, you can specify a range of lines. You might use ma to set a marker at the beginning of the region and mb to set another at the end, and then

:'a,'bw tmp

to write that region to a file. Or you can set a marker at the beginning (say, mx ), move to the end, and:

:'x,.w tmp

Or you can specify line numbers; there are a lot of options. It's not as convenient as direct copy-and-paste, but it works.

Yet another alternative: Since both vim sessions are running on the same system, you can run a single vim session for both files, using tabs or windows; then you can directly copy-and-paste between them without leaving vim .

The easiest way is to copy the text from your terminal, so use your mouse or something like tmux which will allow you to copy arbitrarily text from your terminal using only your keyboard.

There may be a way to copy over SSH: Getting Items on the Local Clipboard from a Remote SSH Session

But I think you'd have a hard time linking it into Vim. I'd personally recommend tmux on your local machine, ssh inside, then copy the text using tmux.

It's done automatically through the system's clipboard. So if you ssh from machine A to machine B you can copy and paste from a Vim running on A to a Vim running on B, and the other way around, provided that:

  • machine A is running an X server
  • your ssh connection forwards X11 requests
  • your Vims on both machines are compiled with +clipboard and +xterm_clipboard
  • you have something like this in your vimrc s on both machines: set clipboard=unnamedplus,autoselect

It also works if you try pasting from one instance of Vim to another on the same machine running X, or on the same machine running Windows (for different reasons).

It doesn't work in a Linux console. I also don't know if it still works when you cross from Windows to X.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM