3

I am trying to copy a block of text (im in linux debian) from vim to ipython I select the block of text "yank", exit vim with ctrl-Z, enter ipython and shift-insert.

However it always only paste the first line of my originaly copied block of text..

Any idea how to paste multi lines?

(Edit by @maxymoo to add a MCVE)

docker run -t -i ubuntu /bin/bash
apt-get update && apt-get -y upgrade
apt-get install python-pip tmux vim
pip install --upgrade pip
pip install ipython

Now start up a tmux session, C-b % to split pane, start vim and enter the following lines:

print("first line")
print("second line")

Copy it to tmux clipboard with C-b [ C-space <arrow keys to select all> M-w

Switch to the other pane with C-b o and start an ipython session. Paste the text with C-b ]

Output:

In [1]: print("first line")                                                                                                                                    
first line                                                                                                                                                     

In [2]:                                                                                                                                                        

The paste works properly if I force line-continuation mode by using %%time cell magic:

In [3]: %%time
   ...: print("first line")
   ...: print("second line")
   ...:
first line
second line
CPU times: user 0 ns, sys: 0 ns, total: 0 ns
Wall time: 42.2 us
maxymoo
  • 35,286
  • 11
  • 92
  • 119
jim jarnac
  • 4,804
  • 11
  • 51
  • 88

2 Answers2

1

When Typing ctrl+z while editing in vim keep in mind that you are back to the shell terminal so you cannot use any vim commands instead you need to use the shortcuts provided by your terminal of Copy and Paste by checking them in terminal Edition > Preferences > shortcuts or you can use your mouse clicks.

Note: in case you have a problem of copying the text using the mouse in vim you need to turn it off by this command :set mouse=

1

You have a couple of options:

Use your window manager

Highlight the text with your mouse and then middle click to paste. Some versions of Python have this behavior turned off, which is silly.

Use tmux

I have this tmux.conf

So for me, Ctrl+a, [ (you can think of it as [opy) then I just use vim-like keys to move the keyboard, space to start marking, and Enter to copy. Then Ctrl+] pastes.

Use xclip

$ cat yourfile.py | xclip

should copy your entire file into your system clipboard. Then you should be able to paste like normal.

Use the system clipboard

"+y should yank from vim into the system clipboard. "+yy if you haven't visually selected text. Depending on how your environment works this may or may not work. Then your shift+insert option should work.

Use ipython magic functions

%run or %ed(it)

%edit lets you edit a specified file. Or, it will make you a temp file if you don't give it a filename. You can also give it the name of a function that you've defined and it will let you edit the function. On exit, it will execute your file. It uses the EDITOR env var to determine what editor to launch.

%run using the specified filename will execute your file.

This is similar to running at a system prompt python file args, but with the advantage of giving you IPython’s tracebacks, and of loading all variables into your interactive namespace for further use (unless -p is used, see below).

You can find another answer where I talk about my IPython workflow here.

Community
  • 1
  • 1
Wayne Werner
  • 49,299
  • 29
  • 200
  • 290
  • 1
    does the tmux solution really work for you? when i try it, i get exactly same behavior as OP (tested on debian and mac), only the first line is executed, the rest is ignored. somehow the tmux "paste" is different than system clipboard paste, which automatically goes into line continuation mode. (this is important if I'm on a remote server where I don't have system clipboard) – maxymoo Mar 05 '18 at 04:12
  • Are you sure that you've actually copied things using tmux's copy? You'll probably want to do a region select (`v` after you start to copy, at least with my tmux.conf)... – Wayne Werner Mar 05 '18 at 04:52
  • yeah it's definitely copying, i can paste it no problems into another vim for example, i can also paste no problems with cmd-v, just with `C-a ]` the ipython command executes after the first line – maxymoo Mar 05 '18 at 05:28
  • @maxymoo can you make an MCVE? – Wayne Werner Mar 05 '18 at 14:24
  • I've added a MCVE to the question – maxymoo Mar 06 '18 at 04:06
  • i have the same problem (just copies the first line). copying from system clipboard works fine though – WalksB Oct 26 '20 at 01:53