I'm trying to write a function to open a split terminal in Vim and then run the current file in that terminal. A non-working example trying to explain what I'm trying to do is
function! Foo()
:bel term
%
endfunction
This just opens up the terminal. I tried with another command, dir, and it seems it's fails because dir is not a Vim command, so it's still trying to run things in Vim rather than the terminal. I also tried putting the command on the same line e.g. :bel term % but I get an error:
Error detected while processing function Foo:
line 2:
CreateProcess failed
And the terminal doesn't even open.
I also tried
function! Foo()
:bel term
!dir
endfunction
though that had the expected result of essentially running !dir and then returning me to a split window with nothing in the terminal there.
I've also tried to use the feedkeys() function but I can't find a way to automatically pass the file name in.
I can't see anything relevant on :help terminal. I've also found two relevant posts (post 1, post 2) but both seem to use Linux/bash and I'm on Windows, and those solutions aren't working.
How could I achieve this? If it's relevant I'm running Vim on Windows through cmd.
const file_name = expand('%')and thencall term_start(&shell)->term_sendkeys(file_name)! So thank you for that. A couple more questions: (1) how do I send an Enter keypress afterwards? Currently it fills the filename in the terminal but doesn't run it. (2) Is there a way to make the terminal split to be below or alongside the code, rather than above it, similar to:bel termand:vert term? – Alira Jul 28 '21 at 15:35:terminal %:psadly does not work, and gives the same error as the similar attempts mentioned in the question. – Alira Jul 28 '21 at 15:37term_start()switches windows, so the old one is lost. I'll use#here for the alternate file. For an enter keypress, you need a string with"\<cr>"in it probably. Have a look atterm_start()s options and see if there's a splitting option. – D. Ben Knoble Jul 28 '21 at 16:38#before, good to know! Much cleaner. As for theterm_start()options: I'd already found them so I knew there was an option for a vertical split calledvertical, but being new to Vim/Vimscript I couldn't figure out the syntax. After an embarassing amount of time I've finally got it:term_start(&shell, {'vertical' : 1}). Thanks for all the help! – Alira Jul 28 '21 at 17:02:termand all that goes with it. – B Layer Jul 28 '21 at 21:59:terminal+tmux is pretty much my daily “i need to run something” when I’m in vim @BLayer – D. Ben Knoble Jul 28 '21 at 22:21:termand/or is easier to do with:!. – B Layer Jul 29 '21 at 00:18