0

Using a terminal emulator (with :term), I would like to insert the value of a variable at the terminal cursor.

In a normal text buffer, I could use put =g:my_variable or exec ("normal! i" . g:my_variable). Neither of these seems to work with the terminal emulator, however. How can I send the contents of the variable g:my_variable to the job that is running in the terminal emulator?

1
  • put =g:my_variable does work with the terminal emulator in neovim, but not with Vim8 (we get E21: Cannot make changes, 'modifiable' is off).
    – Jasha
    Commented Apr 8, 2019 at 13:34

1 Answer 1

1

In the :terminal, you can put any register using (<c-w> = ctrl w):

<c-w>"{register}

In particular, to insert the contents of a variable you can use the expression register:

<c-w>"= g:variable <cr>

If you are writing a script, in vim's terminal you may use the function term_sendkeys to send the contents of a variable to the terminal.

call term_sendkeys(bufnr('%'), g:contents)

If contents contains a "\n" newline character, the command will additionally be executed in the shell.

3
  • Hi @Mass, thank you for your answer. Do you know how to do this using an ex-mode command? I am writing a vim script...
    – Jasha
    Commented Apr 8, 2019 at 13:39
  • updated the answer. note that neovim and vim differ but I'm not familiar with the former
    – Mass
    Commented Apr 8, 2019 at 13:45
  • For reference: the term_sendkeys(...) function works for vim, and the put =... method works for neovim. Neovim does not have a term_sendkeys function (as of this writing).
    – Jasha
    Commented Apr 8, 2019 at 13:48

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.