0

I've got a file open in Neovim like so:

neovim test.rb

And a split open like so:

:split ruby test.rb

How can I rerun the split command each time I write a save in the original window?

Update

I've updated the question to make it clearer, sorry about that. I want to edit in one buffer then run the changes in the terminal split.

  • As is, im not sure what the splitting does (other than appear to open a new file with the name cat test.txt)—do you want to open several splits, close the old one and open a new one, ...? You’re question is a bit unclear. – D. Ben Knoble Oct 2 at 3:19
  • 3
    (Also, welcome to Vi and Vim :) – D. Ben Knoble Oct 2 at 3:20
  • Thanks! The cat was just an example, any shell command would suffice. – okay56k Oct 2 at 3:57
  • 2
    Please explain what you want to achieve. – Christian Brabandt Oct 2 at 6:32
  • 1
    Still a bit unclear: the given command (:split ruby file.rb) doesn't deal with a terminal split. So, you want to, on save, run a given command? Does it have to be in a terminal, or do you just care about the results? – D. Ben Knoble Oct 3 at 1:41
1

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:

augroup RunCommandOnWrite
  autocmd!
  autocmd BufWritePost test.rb call RunCommand()
augroup END

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>")
endfunction

It works by setting up an autocommand to run every time that you save a file named test.rb.

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 :terminal (the :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:

augroup RunCommandOnWrite
  autocmd!
  autocmd BufWritePost test.rb !ruby test.rb
augroup END

Instead of running a function, this directly invokes the command ruby test.rb by using :!. See :help :!cmd for more details.

  • I find the Clam plugin helpful for slightly less interactive versions of this – D. Ben Knoble Oct 3 at 12:22
  • This is amazingly helpful, thanks so much! – okay56k Oct 3 at 21:32

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