Copy from vim to python console in tmux - python-3.x

I have installed tmux.
tmux -V
tmux 2.3
Set my configure file.
cat ~/.tmux.conf
set -g mouse on
Enter tmux and open a two vertical windows in it,open python3 console in the left,open vim in the right.
Now move my cursor at the beginning of the first line in the right with mouse.
Enter into normal mode and input 2yy+, to copy two lines in my + register.
Move cursor at the left python3 console window ,how can i paste content in + register into the python console?
#Kent,do as you say:
1.Move cursor at the beginning of first line,and type "+2Y
2.Move cursor to the left window,and middle-click mouse,nothing happen.
3.press ctrl+b then press ] key.

first your vim should be compiled with +clipboard see vim --version | grep 'clipboard'
To copy ( or delete ) in any vim register you can use the following syntex
"<register name><oprator><motion> (see :h registers )e.g.
"ayy(copy current line in register a) or
"bdd(delete current line in register b) or
"*ce(delete to the end of the current work and place content in register * using c will also put you in insert mode
to copy whole line you can use yy
and system clipboard is mapped to either + or * ( depending on the os )
so to copy the whole line into system clipboard you can use
"*yy or "+yy (depending on the os)
or to copy 2 lines
"*2yy or "+2yy ( to copy current and the line after current line )
once the content is copied in the system clipboard you can paste in tmux using ( command + v or ctrl + shift + v )
or to map system clipboard with tmux paste buffer see https://unix.stackexchange.com/questions/67673/copy-paste-text-selections-between-tmux-and-the-clipboard#72340

2yy+ does NOT copy two lines into + reg, instead, it yanks two lines to " reg, then moves the cursor to first non-blank char in next line
You can on the vim side do: "*2Y then do a mouse middle-click on the python console.
or simply select the lines you want to copy in vim by mouse, then middle click in python console

I didn't quite like with the accepted solution that it depends on a graphical environment for the clipboard since this does not work when vim can not access the clipboard, which is almost always the case for ssh connections.
So I cam up with another solution:
Instead of using the external clipboard vim can pass the text directly into tmux' paste buffer by piping it into
tmux load-buffer -
After that you can paste the content of the buffer with prefix + ] into the active tmux pane.
There are various ways to pass the text from vim to tmux:
# to write the current line into the tmux buffer:
:.w !tmux load-buffer -
# to write all *lines* within the visual selection into the tmux buffer:
:'<,'>w !tmux load-buffer -
# to pipe the content of a register (e.g. from a previous selection) into the buffer:
# #" being the unnamed register, #0 - #9 the numbered registers, and so on
:call system('tmux load-buffer -', #")
Using tmux paste-buffer you can even trigger the pasting into the correct pane at the same time:
# assuming the python pane is at :0.0
:call system('tmux load-buffer -; tmux paste-buffer -t :0.0', #")
You can now also easily map the last line to a key to send the visually selected text to the python pane.

Related

Select text within terminal output using VIM

Let's say I have the following output in my terminal window now:
1. vsud#vsud:~$ pwd
2. /home/vsud
3. vsud#vsud:~$ echo "Hello, world!"
4. Hello, world!
I want to copy to clipboard the 1st and 2nd lines.
vsud#vsud:~$ pwd
/home/vsud
With mouse - I can do that by selecting those 2 lines and pressing Ctrl + Shift + C.
But can I enter into VIM mode somehow and work with that whole output in my terminal as with pure text - so to copy 2 lines I will need to type just "H2yy" ?
Do I need to install some specific Terminal Emulator for that?
Now I'm using terminal emulator which is built in XFCE.
There are two ways to do that: (1) Using tmux, and using (2) ':term' inside vim
tmux
If you are using your terminal from within tmux, you can enter copy mode and select and copy lines from your terminal. Assuming tmux Prefix is Ctrl-b, the precise steps to do this are:
Enter copy mode: Ctrl-b [
Start selecting: Space
Extend selection: use arrow keys
Store selection in buffer: Enter
Transfer selection to system clipboard: Ctrl-b : run-shell "tmux save buffer - | xsel -i -b"
Paste the selection into a vim buffer(+ register): "+p
Additional Notes:
a. tmux offers a command called "capture-pane", with which you can copy an entire pane(terminal).
b. You need xsel to be installed for step 5 above to work. Step 5 can be shortened by using a keybinding. For that have the following command in your .tmux.conf (or, entering this command on your tmux command line Ctrl-b :)
bind C-c run-shell "tmux save-buffer - | xsel -i -b"
Now step 5 is equivalent to pressing Ctrl-c.
c. You can use vim keys for navigation in copy mode: Ctrl-b :setw -g mode-keys vi
:term
If you fire terminal from inside vim using the ex command ':term', you can enter "normal" mode using: Ctrl-W Shift-n. Then you can navigate (and copy in) the terminal with vim keys as if it were a regular vim buffer.
You can pipe the output to vim using view -. For example:
ls -la | view -
This opens a vim instance in read-only mode which can read from stdin

If I have vim open in two terminals, how can I share the copy/paste buffer?

I want to yy a line from one instance of vim running in one terminal emulator, and p that line into another instance of vim running in another terminal emulator. Possible? Is there a special setting to use?
There is a system clipboard which you can use to share data not only between multiple terminals, but also using shift + insert in GUI application.
First of all, check if system clipboard is enabled for your vim installation
vim --version | grep clipboard
if you see +clipboard you are good to continue, otherwise, follow this page to enable the system clipboard.
Once the system clipboard is enabled, you can copy line via "+yy into + buffer which is a system clipboard, and paste it in another terminal with vim with "+p command.
You can instead:
Esc for canceling all modes
:r! cat file.txt all content of file.txt pasts in your file, but if you want paste for example from 5 to 11 lines of file then
:r! sed -n 5,11p file.txt

How to copy text from Vim command line

How to paste into vims command line was asked here here.
But how can I copy from the vim command line?
For example:
:python import sys; print(sys.executable)
Now I want to copy that line to the clipboard, for pasting it into an other editor.
Yes, it is possible.
If you executed a command :foo, the command text line will be stored in register :, you can see it by :echo #:.
To have those value in clipboard, you just let + register have the same value.
So :let #+=#: or call setreg('+',#:) should help you.
You can assign to a register the content of another register:
:let #+ = #:
Will assign in the system clipboard (#+) the last executed : command (#:).
However, this will become your last executed command. If you want to keep your python command as the last one, you can do the following:
First open the command history:
q:
Go on the line of the command you want to copy, then yank it in the system register (visually selects the whole line and yanks it):
"+yy
This will copy the whole line with the new line character at the end, if you just want the command without the new line, you can do:
v$h"+y
Finally, close the command history:
:q
q: open command-line window, it's filled with command-line history, you can copy via "+yy.

stdin stdout copy/paste doesn't work

I using bash to search files on my PC))
so, I type
ls | vim -
It's working.
But I want to copy string from vim and paste it to bash back. And it doesn't work.
From saved file I can copy/paste using
"*Y
but, if file is unsave it not working.
Simply yank a selected text (y key) and then in terminal paste it with Shift+Insert; you'll need this setting in ~/.vimrc to make yank use clipboard:
set clipboard=unnamed " use clipboard reg '*' for all yank & like operations
Works for both vim & gvim.

Double Quote not working in Vim in command mode

I want to copy text from vim buffer to the system clipboard by using the command "*y but when I press the double quote sign in command mode, it doesn't seem to get fired at all as I don't see anything in the status bar (nothing gets copied also even if I blindly continue with the command). I can see all other commands in the status bar in vim. I'm using Ubuntu 12.04 and I have clipboard support enabled in my vim version.
You should press "+y in NORMAL (or VISUAL) mode.
If you love to do it in command line, do this:
:y +
(same for "*y)

Resources