How to execute an external command asynchronously in gVim 8.1 without starting a new cmd every time and append the output to an existing buffer

classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view
|

How to execute an external command asynchronously in gVim 8.1 without starting a new cmd every time and append the output to an existing buffer

joaobzzr
I want to execute a batch file and display the output on a new buffer. I know I can use !start for this, but I can't figure out how to make it reuse the same command-line instead of starting a new one every time.

I managed to get close by using :terminal instead with send_keys(), checking if the terminal buffer still exists and only starting a new one when it doesn't. The problem with this is that what I get is an interactive shell, so I have to switch to Terminal-Normal mode to be able to navigate the contents of the buffer otherwise cmd steals focus from vim and doesn't let me move around. Also the prompt itself is part of the output, which is annoying.

Here's the code:

fun! MatchAnyInList(list, value)
    return index(map(a:list, 'v:val =~# "' . a:value . '"'), 1) >= 0
endfun

" I have no idea what I'm doing    
fun! RunBuildBatchFile()
    if !exists("g:terminal_bufnum") || MatchAnyInList(['finished', ''], term_getstatus(g:terminal_bufnum))
        let g:terminal_bufnum = term_start("cmd", {'hidden': 1})
        call term_sendkeys(g:terminal_bufnum, "vcvarsall x64\<CR>")
    endif

    if bufwinnr(g:terminal_bufnum) < 0
        execute "botright sb " . g:terminal_bufnum
        call term_setsize(g:terminal_bufnum, 4, 0)
    endif

    call term_sendkeys(g:terminal_bufnum, "cls\<CR>")
    call term_sendkeys(g:terminal_bufnum, "C:\\Path\To\Executable\Command.exe")

    if term_getstatus(g:terminal_bufnum)=~# "normal"
        call feedkeys("i")
    endif
endfun

There's probably a much better way of doing this. I tried looking for information about this stuff online and in the docs but I didn't get anywhere. Any ideas?

--
--
You received this message from the "vim_use" maillist.
Do not top-post! Type your reply below the text you are replying to.
For more information, visit http://www.vim.org/maillist.php

---
You received this message because you are subscribed to the Google Groups "vim_use" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
For more options, visit https://groups.google.com/d/optout.