[vim/vim] Implement TerminalNormal autocommand event (#8360)

15 views
Skip to first unread message

Jeff Pitman

unread,
Jun 11, 2021, 2:17:46 AM6/11/21
to vim/vim, Subscribed

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.

Jeff Pitman

unread,
Jun 11, 2021, 2:31:55 AM6/11/21
to vim/vim, Subscribed

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.

Bram Moolenaar

unread,
Jun 11, 2021, 6:19:54 AM6/11/21
to vim/vim, Subscribed

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.

Jeff Pitman

unread,
Jun 17, 2021, 2:57:07 AM6/17/21
to vim/vim, Subscribed

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.

ben.k...@gmail.com

unread,
Jun 17, 2021, 11:14:43 AM6/17/21
to vim_dev
PS normal without a bang allows mappings; normal! does not. Probably not applicable here, because of the terminal. There is also term_sendkeys()

lacygoill

unread,
Jun 19, 2021, 3:47:16 PM6/19/21
to vim/vim, Subscribed

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.

lacygoill

unread,
Jun 19, 2021, 4:04:17 PM6/19/21
to vim/vim, Subscribed

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:

Jeff Pitman

unread,
Jun 19, 2021, 5:05:01 PM6/19/21
to vim/vim, Subscribed

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.

Jeff Pitman

unread,
Jun 19, 2021, 5:05:31 PM6/19/21
to vim/vim, Subscribed

Closed #8360.

Reply all
Reply to author
Forward
0 new messages