1

How can I create a keymap to send the visual selection/current line to a vim terminal window?

I opened a normal vim window (containing only the line echo test) and a vim terminal window and tried recording a macro and figuring it out from there, but ran into problems:

  1. I ran qqY<C-W>w<C-Shift-V><C-W>wq but the resulting macro simple contained echo test as opposed to a paste command (I got Y^Ww<80>PSecho test^M<80>PE^Ww)

  2. I tried to paste in the terminal window normal mode via qqY<C-W>w<C-\><C-N>p<C-W>wq but the paste didn't work and I got the error message Cannot make changes, 'modifiable' is off. Trying set modifiable I got Cannot make a terminal with running job modifiable: modifiable

UPDATE:

Following the answer by Christian Brabandt below, I added the lines

nnoremap <F9> Y<C-W>w<C-W>"0<C-W>w
xnoremap <F9> y<C-W>w<C-W>"0<C-W>w

to my .vimrc. This works almost as desired but has problems with whitespace and multiple lines. For example, if I select

if False:
    pass
print('Done')

in VISUAL LINE mode, and press F9 to execute it in a terminal window running ipython, the ipython terminal shows

In [1]: if False: 
   ...:         pass 
   ...:         print('Done') 
   ...:     

This is due to ipython's autoindentation, which can be turned off via %autoindent or by starting ipython with the option --no-autoindent. Alternatively, ipython's %paste command could be used. I would be nonetheless interested in a vim hack that behaves more like <C-Shift-V>.

2

If you want to paste something into the terminal window, you can use the command CtrlWReg (if you are in terminal job mode).

For your macro, that would be something along: qqY<C-W>w<C-W>"0<C-W>wq (provided that the cursor is at the start of the line in the other window.

The error message Cannot make changes, 'modifiable' is off happens because the job inside the terminal is still running and you simply cannot paste anything into the buffer in Terminal Normal mode. You can do it, once the job finished (but that might close the terminal window) and you issue the :set modifiable command. But since the job (shell) is not running anymore, it cannot process the pasted content.

  • <C-W>"0 does not work properly with whitespace. If I run ipython in the terminal and try to send if False:\n<4spaces>pass\nprint('Done')\n, the terminal gets if False:\n<4spaces>pass\n<4spaces>print('Done')\n (i.e. even after an additional manual hit of Enter I don't get any output). This problem doesn't happen with <C-Shift-V> – Bananach Nov 27 '18 at 14:21
  • I added the problem description to the question, so it is more easily readable – Bananach Nov 27 '18 at 14:29
  • I just realized this is ipython's fault though, so I accepted your solution – Bananach Nov 27 '18 at 14:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.