0

As title. Assume my cursor is already at the terminal buffer, but it's still in normal mode. I'm trying to use TermEnter to enter terminal mode when ever I switch to a terminal buffer.

1 Answer 1

0

It turns out to be ridiculously hard:

Solution 1: no blink on switch terms, can't change bg-color on TermEnter: (simple, so preferred)

vim.cmd("au BufEnter,TermOpen term://* if &buflisted | set winhl=Normal:CoolBlack | startinsert | endif")
vim.cmd("au BufLeave term://* if &buflisted | set winhl= | stopinsert | endif")

Solution 2: blink on switch terms, can change bg-color on TermEnter: (best UX if blink could be fixed)

  vim.cmd("au TermEnter term://* if &buflisted | set winhl=Normal:CoolBlack | endif")
  vim.cmd('au TermLeave,BufLeave term://* set winhl=')
  vim.cmd("au TermOpen term://* if &buflisted | set winhl=Normal:CoolBlack | startinsert | endif")
  vim.cmd("au BufEnter term://* if &buflisted | startinsert | endif")
  -- vim.cmd("au BufEnter,TermOpen term://* if &buflisted | set winhl=Normal:CoolBlack | startinsert | endif")
  vim.cmd("au BufLeave term://* if &buflisted | set winhl= | stopinsert | endif")

vim.keymap.set({'n', 't'}, '<M-}>', function ()
  vim.cmd('BufferLineCycleNext')
  vim.cmd('normal M')
end, NOREF_NOERR_TRUNC)

Some notes:

  • Use the first argument of keymap.set to set shortcuts for terminal mode. Don't detect terminal mode during the execution of the shortcut.
  • There are two situations that you need to reset the color.
  • TermOpen is needed since BufEnter won't be triggered after calling :term.

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.