Is your feature request about something that is currently impossible or hard to do? Please describe the problem.
I'm trying to execute a function when a terminal is finished executing a command. The function is used to mimic quickfix-like behavior where a line can be selected in Terminal-Normal mode and processed.
Describe the solution you'd like
Once :terminal goes from the state Terminal-Job to Terminal-Normal, I would like this event to fire.
Describe alternatives you've considered
Trapping <CR> is currently impossible using nnoremap since for other conditions, I want <CR> to pass through and every time I try to conditionally map <CR> for a buftype of terminal, I cannot get it to send a <CR> without recursively going back through the map. I tried this:
function! QuickfixTerminal(...) if &buftype ==? 'terminal' " && bufname(a:2.bufnr)[0] ==? '!' let l:line = getline('.') return l:line else " for other Normal mode situations, just pass <CR> through. call feedkeys("\<CR>") " normal <CR> endif endfunction nnoremap <silent> <CR> :call QuickfixTerminal()<CR>
Additional context
My ultimate goal is to run Mercurial commands in :terminal. Then I want to map <CR> in Terminal-Normal mode so that when I hit Enter, that line is processed and then customized commands are run. For example, if you run hg log, you get a list of revisions. Once the command is done executing, then you would be able to hit Enter which then runs hg update to a particular revision. After this, the terminal window can then be automatically closed.
—
You are receiving this because you are subscribed to this thread.
Reply to this email directly, view it on GitHub, or unsubscribe.![]()
It looks like I can fix the pass thru problem with the mode param:
call feedkeys("\<CR>", "n")
This accomplishes what I want in the additional context. Though you might want to consider a TerminalNormal autocommand event for other reasons.
Can you use term_getjob() and then job_setoptions() to add an exit callback?
Or, if you use term_start(), add the exit callback there.
My use case is to leave the Terminal-Normal buffer open so the user can navigate with movement keys, pick a line, and hit <CR>. This picks the revision to run the hg update command on. The proposed method to add a callback to terminal start skips this needed navigation step and wouldn't be able to pick the right revision from the presented hg log output.
Try to listen to the TerminalWinOpen event to install a buffer-local mapping (defined with the <buffer> argument):
autocmd TerminalWinOpen * nnoremap <buffer><nowait> <cr> <cmd>call QuickfixTerminal()<cr>
^-------------^ ^------^
This way, no need to pass through anything, because the mapping is only installed where it makes sense; i.e. in a terminal buffer.
Though you might want to consider a TerminalNormal autocommand event for other reasons.
Already asked here:
And acknowledged as valid and useful since a relevant item was added in the todo list:
Add a ModeChanged autocommand that has an argument indicating the old and new
mode, as what's returned from mode(). Also used for switching Terminal mode.
If somehow how I misunderstood the request, those related ones exist too:
Perfect! Feel free to dupe this out and I'll try the tip about installing a buffer-local mapping so that the function doesn't have to be invoked for every kind of buffer.
Closed #8360.