Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Yank/copy selected text to clipboard in IPython Vi mode

I can't figure out how to copy lines that I selected/highlighted in IPython in Vim mode to the normal clipboard (to be pasted outside of the IPython shell). Normally, in vim I can yank text using "+y and paste it somewhere else, but hitting those keys in IPython Vim mode doesn't seem to work. So I end up having to highlight the desired text using my mouse and copying it with Command-C.

This is an annoyance because if I have multiple lines in IPython there will be many junk characters that I have to filter out as seen below:

In [8]: import numpy as np 
   ...: import math 
   ...:  
   ...: print("hi") 
   ...: while(True): 
   ...:     break 
   ...:  
   ...: x = 3 
   ...: y = 4 
   ...:  
   ...:   

Here I would have to filter out the In [8] and the ...: on each line. But selecting using v or V appropriately ignores these junk characters.

This answer doesn't say how to do it in Vi mode and also doesn't mention anything about yanking to the system's clipboard.

like image 953
WalksB Avatar asked Dec 05 '25 20:12

WalksB


1 Answers

if you are running ipython inside vim terminal, you can type the following in ipython: %history -l 10

This will print last 10 commands without the leading dots. Which can be easily copied.

You need to open the ipython in vim terminal. And then, after typing the %history command (above), you will need to go to normal mode with key-combination Ctrl-W Shift_N. Then, copy multiple lines using V (block visual model) into + register with "+y command. You can then copy it into another vim buffer using "+p or another application such as gedit using 'right click, then paste.'

like image 172
CyclicUniverse Avatar answered Dec 08 '25 10:12

CyclicUniverse



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!