First off, you might like to look into any of the many plugins that incorporate a REPL into Vim (vim-slime is the one I use). They don't do exactly what you're asking, but they do something very similar and are far more flexible.
If you want your exact behaviour, though, then here's a proof of concept that shows how you can implement it:
autocmd BufWritePost test.rb call RunCommand()
function! RunCommand() abort
" Find terminal
windo if &buftype == "terminal" | let s:terminal = bufnr('%') | endif
" Type in command
call term_sendkeys(s:terminal, "ruby test.rb\<CR>")
It works by setting up an
autocommand to run every time that you save a file named
The autocommand calls a function that finds a visible terminal buffer and then sends the keystrokes
ruby test.rb<Enter> to them to run your command.
There is a lot you can/should do to make this more robust, if you're intending to use it. Feel free to ask if you need any tips on how to do so.
In order to use it, you need to open your terminal by typing
:split command you included in your question does not run a command or open a terminal) and then return to your ruby file by typing Ctrl-WCtrl-W.
It's actually unclear from your question whether you need a terminal or a split window at all. If your goal is literally just to run a shell command whenever you save a file, all you need is the following:
autocmd BufWritePost test.rb !ruby test.rb
Instead of running a function, this directly invokes the command
ruby test.rb by using
:help :!cmd for more details.