mle0312
mle0312

Reputation: 425

How to switch from terminal back to editor in vim?

I am new to vim, and I learn that you can interact with the terminal two ways while in vim. The first is to type: ! . The second is :term, when you have a terminal side by side with your source code. As soon as I press :term, my cursor is in the terminal. How do I switch back to the my source code and vice versa? The answer over here does not really help. I tried the key combinations C-W, and then :bn but it only exits terminal, then back to terminal again.

(How do I run a terminal inside of Vim?)

Upvotes: 6

Views: 15830

Answers (4)

Iman Hosseinzadeh
Iman Hosseinzadeh

Reputation: 534

1. Start a new terminal:

:term

2. Switch between terminals and Vim

CTRL-W CTRL-W

3. Close Terminal

exit

Upvotes: 1

Harsh Verma
Harsh Verma

Reputation: 923

As others pointer out, you will enter terminal mode after using the terminal with :terminal. You can use <Ctrl-\><Ctrl-N> to switch back to normal mode and then switch to your buffer with source code.

To switch to an existing terminal from some buffer, consider adding the following shortcut to your config :

" switch to an existing terminal
" by typing `<c-t>` in normal mode.
" It works by simply switching to a buffer that
" starts with the `term` keyword.
nmap <c-t> :b term<CR>i

In NeoVim, you can bind shortcut <Ctrl-N> to <Ctrl-\><Ctrl-N> in terminal mode to make your life easier:

" Escape to normal mode from inside the terminal by typing <C-n>
:tnoremap <C-n> <C-\><C-n>

Even better, in Neovim, you can quickly switch back to the previous buffer from the terminal with <C-t> with the following config:

" Switch back to code buffer from terminal using <C-t>
:tnoremap <C-t> <C-\><C-n> :b#<CR>

Upvotes: 2

user4640261
user4640261

Reputation:

You can swich terminal with :shell or :sh and turn back to Vim with exit. You can also use C-z and fg couple in same matter.

Upvotes: 4

Amadan
Amadan

Reputation: 198334

As :help :terminal says, you can always exit to normal mode using <Ctrl-\><Ctrl-N> (:help CTRL-\_CTRL-N). Use i to return to terminal interaction.

Upvotes: 8

Related Questions