0

I am trying to create autocommand to run current python file in a terminal, when it is saved. It is working with the following autocommand:

vim.api.nvim_create_autocmd("BufWritePost", {
  group = augroup("PythonAutorun", {clear = true}),
  desc = "Start autorunning python file on save",
  pattern = {"*.py"},
  callback = function (ev)
    vim.cmd(':split | terminal python %')
    print(vim.inspect(ev))
  end
  }
)

The problem is, contrary to just running :term directly, focus stays in python file window. Therefore, I need to manually switch to terminal window and enter terminal mode and quit.

What I want to achieve, is that when autocommand is triggered - focus switches to the newly created terminal window and terminal mode is activated.

I tried to use TermOpen event, but none of the Term* events trigger, if it is spawned like shown above.

vim.api.nvim_create_autocmd("TermOpen", {
  group = augroup("FocusTerm", {clear = true}),
  desc = "When terminal is run, shift focus to it",
  callback = function (ev)
    print(vim.inspect(ev))
  end
})

What would be the shortest way to achieve the functionality?

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.