1

For using the terminal while working with Rust. I've added this config (NVIM) (TTerm one).

if has('nvim')
    command Ter vsplit term://zsh
    command Tter tabe term://zsh
else
    command Terminal vert term
    command TerminalTab tab ter
endif

It opens the terminal in a different tab as expected. Normally with other tabs, we can use the <C-p> to navigate to previous tabs. But I cannot use the <C-p> or :tabprevious as it types it in the terminal. Any options?

Screenshot

4 Answers 4

6

(This answer pertains to neovim specifically. If you find anything conflicting with vim, let me know and I'll edit the answer.)

:h term operates in a separate mode: the terminal model. It's somewhat like the insert mode in that you have to ESC out of it before you can go into command-line mode with : key. Only to get out of the terminal mode, instead of pressing ESC, you press <C-\><C-N>. Go through :h vim-modes for a complete understanding.

2
  • 2
    All of this is true for vim also
    – D. Ben Knoble
    Commented Aug 21, 2022 at 11:51
  • I use this binding to be able to press esc like normal vim: tnoremap <Esc> <C-\><C-n> nvim: vim.cmd("tnoremap <Esc> <C-\\><C-n>") Commented Jul 13 at 8:58
2
  1. Control-P by default moves cursor a line up. If it switches tabs instead, then it is due to custom mapping.

  2. Vim is all about modes. An "ordinary" buffer may be in Normal or Insert. While in Insert mode, no Normal mappings/keys apply. The same is for a Terminal buffer, except it may be in Normal or Terminal mode. Hence, you need either switch to Normal manually (ctrl-backslash ctrl-n) and then use Normal keys, or create special Terminal mode mapping(s), see help for tmap command and such.

3
  • I beat you by 5 seconds lol
    – 3N4N
    Commented Aug 21, 2022 at 8:39
  • @kadekai No, it was me faster by 5 sec than you :)
    – Matt
    Commented Aug 21, 2022 at 10:16
  • 1
    Thanks for the help. Yeah, actually for the <C-p> it was wrong; what I meant was ctrl w+p. Thanks for the help though, <C-\><C-N> works! Commented Aug 21, 2022 at 15:35
1

Using <C-/><C-N><C-W>r to get to the split on the right is, IMO, very frustrating. The neovim manual suggests adding "Alt+direction" mappings that would then work from any mode:

:tnoremap <A-h> <C-\><C-N><C-w>h
:tnoremap <A-j> <C-\><C-N><C-w>j
:tnoremap <A-k> <C-\><C-N><C-w>k
:tnoremap <A-l> <C-\><C-N><C-w>l
:inoremap <A-h> <C-\><C-N><C-w>h
:inoremap <A-j> <C-\><C-N><C-w>j
:inoremap <A-k> <C-\><C-N><C-w>k
:inoremap <A-l> <C-\><C-N><C-w>l
:nnoremap <A-h> <C-w>h
:nnoremap <A-j> <C-w>j
:nnoremap <A-k> <C-w>k
:nnoremap <A-l> <C-w>l

This is what I use personally (except for the insert mode mappings) and I would strongly recommend them. Of course, you would lose any terminal functionality (if any) that was using these mappings.

3
  • Welcome to Vim :-). I recommend not to refer to 'Other Answer' since you don't know if there will be not more or if it still exist in the future. Try to make your answer complete and self consistent. Commented Aug 22, 2022 at 8:36
  • Yeah this looks good! Thanks Commented Aug 23, 2022 at 11:28
  • In Vim, C-w h/j/k/l is all that you need to move left/down/up/right (no C-\ C-n needed).
    – D. Ben Knoble
    Commented Aug 23, 2022 at 13:34
0

In nvim, I personally use Ctrl-PageUp and Ctrl-PageDown to switch between tabs, so prefixing Ctrl-\ Ctrl-N to Ctrl-PageUp and Ctrl-PageDown in remap works well:

tnoremap <C-PageUp>   <C-\><C-N><C-PageUp>
tnoremap <C-PageDown> <C-\><C-N><C-PageDown>

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.