1

I am having trouble crafting a command that would copy the text from the current window and paste this text into a terminal buffer opened in the split below.

Here are a few things I tried:

normal yaw<c-w>j<c-w>"0

:execute "normal yaw\<c-w>j\<c-w>\"0"

:execute 'normal yaw<c-w>j<c-w>"0'

let cmd = "normal yaw\<c-w>j\<c-w>" . '"0'
:execute cmd

But nothing seems to be working. I can get everything to work up until the very last part ("0) which specifies the register to paste from. No escape sequences seem to work.

What am I missing with this?

1

3 Answers 3

2

The problem is that you can't really use :normal in terminal mode, since that mode is special, it doesn't really behave like Normal/Insert mode in a normal vim buffer.

Instead, you can use the term_sendkeys() function to send the buffer to the terminal.

This should work:

:execute "normal yaw\<c-w>j" | call term_sendkeys('', @0)

Or:

:execute "normal yaw" | wincmd j | call term_sendkeys('', @0)

Note you can also pass a buffer name/number as a first argument to term_sendkeys(), in which case you don't need to switch to that window first.

2

A little experiment:

  • Go to your fresh opened terminal, and do an echo:
echo 1
  • Leave it in terminal job mode, go back to your original window, execute:
exec "norm! \<c-w>pggyy\<c-w>pp"

You should see a new line in your current buffer, it's copied from first line of terminal buffer. As ggyy are normal mode commands, it tells us that when you switch to a terminal buffer in :norm, it's always in terminal normal mode during the life time of :normal , even though the terminal is actually in terminal job mode.

  • Execute another simple ex command:
exec "norm! \<c-w>p" | echom 'balabala'

You must see balabala. Note that echom is an ex command, which means your are still not in terminal job mode, you can execute as much ex command as you want.

To communicate with the terminal with ex command:

  • :h term_sendkeys() Send keystrokes to terminal without tmap applied, without leaving current buffer.
  • :h feedkeys() You can choose to apply map or not.
  • :h ch_sendraw() You can get handle from :h term_getjob().
1
  • Accepted the other answer since it was earlier, and solve the problem in the same way. But wanted to say thank you for detailed explanation. Commented May 28, 2019 at 6:17
0

This will work if you are sending the whole current buffer.

This example assumes the terminal is in buffer '2', so change the buffer number to your actual buffer number. To find the terminal buffer number(s) use either :ls or `:echo term_list().

:%y | call term_sendkeys(2, @")

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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