akinsho / toggleterm.nvim

A neovim lua plugin to help easily manage multiple terminal windows
GNU General Public License v3.0
4.33k stars 172 forks source link

How to send enter to executing command (`:TermExec`) #259

Closed saccarosium closed 2 years ago

saccarosium commented 2 years ago

Hi, Every time I try to execute a command with TermExec the program is not executed. I don't know if it is intentional or not, but if you happen to know how to send enter with the command, would be very appreciated.

akinsho commented 2 years ago

@saccarosium it sounds like something is configured incorrectly on your side or there is an issue with how you are using TermExec I just tested TermExec cmd=ls and this works fine. A user should not have to manually add an enter character.

saccarosium commented 2 years ago

I'm using it in a command

akinsho commented 2 years ago

@saccarosium can you please provide an exact example of what is not working, I can't help based on just a description. It would be best see actual non-functional code

saccarosium commented 2 years ago

@saccarosium can you please provide an exact example of what is not working, I can't help based on just a description. It would be best see actual non-functional code

Sorry, I wasn't at my computer.

Basically, I want to create a command, called Repl, that if it is called without any arguments gives me a terminal in a horizontal split, else it opens a split and try to run the argument.

the function

function M.repl(cmd)
    if vim.fn.empty(cmd) == 1 then
        vim.api.nvim_command("ToggleTerm size=20 direction=horizontal")
    else
        vim.api.nvim_command("TermExec size=20 cmd='" .. cmd .. "' direction=horizontal")
    end
end

the command

command! -nargs=* Repl lua require("sacca.toggleterm").repl(<f-args>)

https://user-images.githubusercontent.com/96259932/177552985-25616568-5e46-410b-94ed-377ab0b105ad.mov

akinsho commented 2 years ago

@saccarosium you should use the Lua API instead to programmatically trigger term exec i.e. require('toggleterm').exec(cmd) if you read the README or docs this explained there

saccarosium commented 2 years ago

@akinsho I have tried what you suggest, and gives me the same problem. I also tried to run it manually from vim's command line and with this example command that you gave in the docs: command! -count=1 TermGitPushF lua require'toggleterm'.exec("git push -f", <count>, 12).

I don't really know what the issue is… I have also tried in other terminals, but doesn't seem to help.

Some info about my setup:

akinsho commented 2 years ago

@saccarosium command! -count=1 TermGitPushF lua require'toggleterm'.exec("git push -f", <count>, 12) defines a command, it doesn't execute one. I genuinely don't know what you are specifically doing that isn't working. This works perfectly fine for me locally. Can you please make sure there are no issues in your shell

saccarosium commented 2 years ago

I think is a problem with my setup.

I close the issue.

Thanks for you help👍.

athanase commented 1 year ago

Hello @saccarosium,

I currently encounter the same issue, how did you manage to make it work ? What was messing with the plugin in your setup ?

saccarosium commented 1 year ago

Nope, I didn't really found a solution. I simply stopped to use the plugin.

athanase commented 1 year ago

Ok, I found the issue for me. If you are interested:

This is not an issue related to this plugin.

VictorLemosR commented 3 months ago

Hello @saccarosium,

I currently encounter the same issue, how did you manage to make it work ? What was messing with the plugin in your setup ?

I guess the problem has something to do with windows. At first, I made the enter key work, but it had to stay on terminal window, because when I used vim.api.nvim_set_current_win(main_win) the enter didn't work. There is a hack I copied from iron.nvim that solved my problem which is to use vim.defer_fn. Here is an example in my code:

local IPYTHON_TERMINAL_WINDOW = 3
toggleterm.send_lines_to_terminal("visual_lines", false, { args = IPYTHON_TERMINAL_WINDOW })
ipython_terminal = require("toggleterm.terminal").get(IPYTHON_TERMINAL_WINDOW)
ipython_terminal:focus()

local job_id = ipython_terminal.job_id
local enter_in_string = string.char(13)
vim.defer_fn(function()
    vim.fn.chansend(job_id, enter_in_string)
end, 150)