0

I am trying to implement a vimscript function which appends to a terminal buffer. It works when I write this as a single mapping which uses the normal mode 'a' command to append:

nnoremap <Leader>glq :tabnew<CR>:terminal<CR>:set ft=instaflog<CR>als<CR>

But I want this in a function to make the code clearer. However, using append() or insert() I get the error stating that the terminal buffer is not modifiable, which I guess is strictly true, but every keypress is passed on to the terminal. So far, I have:

function! TerminalSomething() abort
  tabnew
  terminal
  set ft=instaflog
  startinsert
  execute "normal! Ayyy" 
endfunction

But the 'yyy' text doesn't appear; that execute line apparently does nothing for terminal buffers, even though it works for all other buffer types.

2
  • First thing I try when text is seemingly being ignored is to send it with feedkeys().
    – B Layer
    Commented May 12, 2021 at 10:00
  • Brilliant; ` call feedkeys("Als\<CR>")` works. Convert to answer so I can give you points? Commented May 12, 2021 at 10:05

1 Answer 1

1

This sounds like a job for feedkeys(). What makes me think that* is the fact that you got the response you wanted when the command was included in a mapping. Looking at the first sentence of the documentation for the function:

Characters in {string} are queued for processing as if they come from a mapping or were typed by the user.

(Emphasis is mine.)

So instead of

execute "normal! als" 

...try...

call feedkeys("als\<CR>")

Be sure to read the docs as there are some important things to be aware of there such as the need to escape special keys/keycodes as is done here with <CR>.

*Besides the fact that whenever output seems to be ignored I usually try feedkeys() as a first step.

2
  • instead of 'whatever', I'd suggest ls\<CR> since that shows how to escape the enter you will need at the end to issue a terminal command, and it can be pasted in and tested directly. Commented May 12, 2021 at 10:21
  • 1
    There's also term_sendkeys(), not sure how that's different or why you'd use it over feedkeys(), but probably worth having a look.
    – filbranden
    Commented May 12, 2021 at 12:11

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.