Can I pass flags to the `:terminal` command?

I am trying to create a function that will open a single interactive command (called doc) in a terminal. I have it basically working with this function:

function()
    vim.cmd.te()
    local keys = vim.api.nvim_replace_termcodes("idoc<cr>", true, false, true)
    vim.api.nvim_feedkeys(keys, "t", false)
end

The problem is that it takes a while to open, because of my .zshrc. And, after I quit the command, there is still a terminal running. This would be fixed if I could pass the flags --no-rcs and --onecmd to Zsh when I first ran :terminal. Is there a way to do this? And if not, is there some other way to fix the problems?

How about using termopen by vim.fn.termopen({"zsh", "--onecmd", "--no-rcs"}) instead of vim.cmd.te?

EDIT: Fixed the syntax of the command line list. Sorry.

Thanks! The only problem is that once the command exits, there is a screen that says

[Process exited 0]

Do you know if there is a way to get rid of this?

1 Like

I haven’t tested, but how about deleting the buffer using the bdelete command (or some equivalent function in Lua) after finishing the zsh?

Hm, I am not sure how I would do that. My code is this:

vim.fn.termopen({
    "zsh",
    "--onecmd",
    "--no-rcs"
})
local keys = vim.api.nvim_replace_termcodes("idoc.zsh<cr>", true, false, true)
vim.api.nvim_feedkeys(keys, "t", false)

The nvim_feedkeys part runs right after opening the terminal, and I don’t know how I would specify to wait until after zsh has stopped.

How about jobwait? The returned value of termopen is same as jobstart, which represents a job ID. We can use the jobwait function to wait for a job exits with the job ID. See :help jobstart() and :jobwait() for details.

Instead of jobwait, just pass an on_exit handler to either termopen or jobstart. To automatically close the terminal and not wait for confirmation on exit code 0, you can have:

vim.fn.termopen({"zsh", "--onecmd", "--no-rcs"}, { on_exit = function() vim.api.nvim_input("<Cr>") end })

Although running that I just get a blank screen, I’ve only ever done it for interactive commands like “python” or some such.

1 Like