1

I want to run an async command and when it completes open a terminal window. Since the opening is delayed, I don't want it to steal input from me, so the new window should be in Terminal-Normal mode.

So far, the best I've figured out is opening it in a new tab and returning to the previous tab, but if I gt to that tab, it's still in Terminal-mode and I can't gt out of it without exiting to Terminal-normal.

function! project#run_after_compile(return_code) abort
    if a:return_code == 0
        tab terminal ++norestore C:\bin\run_game.bat
        tabnext #
    endif
endf

(I'm using this with asyncrun to build with makeprg so errors go to quickfix then run in a terminal so its interactive: let g:asyncrun_exit = 'call project#run_after_compile(g:asyncrun_code)')

1 Answer 1

1

Use feedkeys:

function! project#run_after_compile(return_code) abort
    if a:return_code == 0
        terminal ++norestore C:\bin\run_game.bat
        " Replace C-W if you have set 'termwinkey'.
        " Must pass "x" to allow other commands to follow (wincmd).
        call feedkeys("\<C-W>N", "x")
        wincmd p
    endif
endf

" Or put it in a tab:
function! project#run_after_compile(return_code) abort
    if a:return_code == 0
        tab terminal ++norestore C:\bin\run_game.bat
        call feedkeys("\<C-W>N", "x")
        tabnext #
    endif
endf

Note: Trying to be portable with feedkeys(&termwinkey .."N") doesn't work. I believe it's due to how you need to escape < for feedkeys.

3
  • 1
    Theres a term-specific feedkeys, but I forget what it’s called. You might also look at Dispatch
    – D. Ben Knoble
    Jul 27, 2021 at 1:07
  • 1
    @D.BenKnoble It's term_sendkeys(). +1 for vim-dispatch.
    – filbranden
    Aug 25, 2021 at 23:48
  • term_sendkeys() send keys to the underlying terminal and not the buffer containing the terminal. :call term_sendkeys(term_bufnr, &termwinkey ..'N') inserts characters instead of switching to terminal-normal.
    – idbrii
    Aug 26, 2021 at 0:51

Your Answer

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

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