If you have vim8, it's easy to set up a mapping to open a
:terminal, to which you can then send commands (like build and run code).
My workflow is something like
- Edit python code
- Try some code in the python interpreter (
- Copy or write in the code
- Either tmux split,
:term, Ctrl-Z, or
:quit to run my script (with sessions, I can then get a semblance of where I was with
I have seen several answers on this site dedicated to opening a
:term and sending commands to it. There are also tmux plugins for vim that might help you do this. My alternative (below) is to let the shell do its thing (run code), but provide a way to do that in vim. It also lets me play with interpreters in vim with a minimum of fuss.
At the end, I do provide a possibility of something more like "run code" then "run an interpreter." It is untested, however.
I have these mappings:
" Terms and interpreters
" set b:interpreter for filetypes to affect the term
nnoremap <Leader>t :call terminal#run('++close')<CR>
nnoremap <Leader>T :call terminal#run()<CR>
which use this code (I should just make this a plugin at this point):
" Functions for dealing with the terminal
" Global function for calling terminal with the appropriate interpreter
" b:interpreter controls the program run
function! terminal#run(...) abort
let l:interpreter = get(b:, 'interpreter', &shell)
let l:command = 'term'
for l:opt in a:000
let l:command .= printf(' %s', opt)
let l:command .= printf(' %s', l:interpreter)
after/ftplugin/python.vim, I have
let b:interpreter = 'python'
I have similar definitions for lisp (
clisp), ruby (
irb), and more.
You could probably even do something like
let b:interpreter = 'python '.expand('%'), so that the "interpreter" is actually "run this file." Then you would prefer
<Leader>T, which, in my mappings, does not close the terminal when it finishes.
I'm not hating on tmux. I actually use it anytime I'm in a terminal. But for running code, it can be nice to pop open an integrated terminal, particularly for interpreted languages.
If the program is not interactive, the Clam plugin is quite useful.