21

I have a vim's buffer and a terminal in the same vim's window but each time switch to terminal I can only exit it to go back to vim's buffer,

Is there a way to keep terminal session but be able to switch to vim's buffer?

I used gvim 1-10802 on windows 10.

Many thanks.

2
  • 1
    does the normal way not work? (see :h terminal-use)
    – B.G.
    May 9, 2018 at 13:43
  • 1
    Thanks, that works. :h terminal-typing May 9, 2018 at 14:55

5 Answers 5

25

A better way which I use (and love these days) to jump between terminal (interactive mode with all alias and path set) and vim is using CTRL+Z in normal mode. Work on terminal, and when done type fg to return back to vim right where I left.

CTRL+Z Suspend Vim, like :stop. Works in Normal and in Visual mode.

1. Suspend and resume

Like most Unix programs Vim can be suspended by pressing CTRL-Z. This stops Vim and takes you back to the shell it was started in. You can then do any other commands until you are bored with them. Then bring back Vim with the "fg" command.

CTRL-Z
{any sequence of shell commands}
fg

You are right back where you left Vim, nothing has changed. In case pressing CTRL-Z doesn't work, you can also use ":suspend". Don't forget to bring Vim back to the foreground, you would lose any changes that you made!

Only Unix has support for this. On other systems Vim will start a shell for you. This also has the functionality of being able to execute shell commands. But it's a new shell, not the one that you started Vim from. When you are running the GUI you can't go back to the shell where Vim was started. CTRL-Z will minimize the Vim window instead.

2. Starting new shell

You can start a new shell this way:

:term[inal]          //  for nvim users
:shell                // (depricated in nvim)

This is similar to using CTRL-Z to suspend Vim. The difference is that a newnshell is started. Exit back to vim hitting CTRL+D

2
  • Thanks, but this is for windows' terminal (cmd) and native vim's terminal. May 12, 2018 at 1:26
  • 1
    heads up :shell not an editor command in neovim 0.5 . I guess it's understandable since :term exists Dec 25, 2020 at 11:51
22

From :h terminal-typing

CTRL-W can be used to navigate between windows and other CTRL-W commands, e.g.:

CTRL-W CTRL-W   move focus to the next window
CTRL-W :    enter an Ex command
12

After opening a terminal window within vim with :te[rminal] and switching to insert mode (needed to type in the terminal), all keys are sent to the terminal, so esc doesn't switch back to normal mode (as it would if from insert mode in a file).
To switch back to normal mode, use <C-\><C-N>. See :h terminal-input for more info.

1
  • This was the only set of commands that worked for me, the above did not. I am on v0.5 of neovim using zsh.
    – alex
    Jul 22, 2021 at 12:22
0

In my workflow, I find it convenient to:

  • Create a terminal in a new buffer using :term(check if your version of vim supports this -- I use neovim).
  • After using the terminal, switch back to normal mode using <C-\><C-N>(like others pointed out) and open some other buffer to work on using :buffer other_file.txt -- doing this will not kill your terminal buffer.
  • Switch back to the previously opened terminal buffer using :b term(opens buffer starting with prefix term). A convenient shortcut for this can be configured as follows:
" Switch back to the opened terminal buffer using Ctrl-t
nmap <c-t> :b term<CR>
0

It's not so easy to switch between a regular buffer and a terminal buffer, using CTRL-6 to switch between alternate files doesn't work in the terminal buffer. What you can do is CTRL-6 and then CTRL-W :b#.

Instead, consider splitting the regular buffer. You can open the terminal in a vertical split :vert ter and then use CTRL-W P to switch back and forth.

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.