How to temporarily exit Vim and go back

Vim

Vim Problem Overview


How could I exit Vim, not :q, and then go back to continue editing?

Vim Solutions


Solution 1 - Vim

Assuming terminal Vim on a flavor of *nix:

##To suspend your running Vim

Ctrl + Z

will suspend the process and get back to your shell

fg

will resume (bring to foreground) your suspended Vim.

##To start a new shell

Start a subshell using:

:sh

(as configured by)

:set shell?

##or

:!bash

followed by:

Ctrl+D (or exit, but why type so much?)

to kill the shell and return to Vim.

Solution 2 - Vim

You can use :sh to exit to your default shell then typing $ exit at the shell prompt will return you to Vim.

Solution 3 - Vim

You can switch to shell mode temporarily by:

:! <command>

such as

:! ls

Solution 4 - Vim

If you are on a Unix system, Ctrl + Z will suspend Vim and give you a shell.

Type fg to go back. Note that Vim creates a swap file while editing, and suspending Vim wouldn't delete that file (you aren't exiting Vim after all). On dumb terminals, this method was pretty standard for edit-compile-edit cycles using vi. I just found out that for me, gVim minimizes on typing Z.

Solution 5 - Vim

You can also do that by :sus to fall into shell and back by fg.

If you frequently need to go back and forth between shell and vim, probably what you really want is have only one vim instance in the shell, and use it to open any file in the workspace. If so, check this question. Once you set it up correctly, you can :sus or C-z to return to the shell, then just v or v <newfile> to get back to vim.

And my answer is almost my daily routine.

Solution 6 - Vim

If you're using Neovim, you can do the following:

  • :terminal command to bring up a terminal window.

  • Do your terminal stuff

  • Type exit to kill the terminal process

  • Press any key to return to Neovim

Solution 7 - Vim

Just put in fg and go back to your most recently suspended program.

Solution 8 - Vim

There are several ways to exit Vim and have everything the same when you return. There is very good documentation within Vim itself explaining the various ways this can be done. You can use the following command within vim to access the relevant help page: :help usr_21

To give you a brief summary, here are the different methods of quitting and returning with your session intact:

  1. Suspend and resume - You don't actually quit Vim with this; you simply hide your session in the background until you need it. If you reset your computer or issue a kill command to Vim, you will lose your session. This is good for when you want to switch to another task temporarily, but if this is the case, then you might want to look into using the [GNU Screen utility][1] instead.

  2. Sessions - This is the true way of saving your session between instances of Vim. Even if you truly quit Vim, your session will be there for you when you return. This is probably what you are looking for.

[1]: http://en.wikipedia.org/wiki/GNU_Screen "GNU Screen utility"

Solution 9 - Vim

To extend user Zen's answer, you could add the following line in your ~/.vimrc file to allow quick toggling between Bash and Vim:

noremap <C-d> :sh<cr>

Solution 10 - Vim

If you don't mind using your mouse a little bit:

  • Start your terminal,
  • select a file,
  • select Open Tab.

This creates a new tab on the terminal which you can run Vim on. Now use your mouse to shift to/from the terminal. I prefer this instead of always having to type (:shell and exit).

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionRickyView Question on Stackoverflow
Solution 1 - VimzenView Answer on Stackoverflow
Solution 2 - VimPierre-Antoine LaFayetteView Answer on Stackoverflow
Solution 3 - VimJay ZengView Answer on Stackoverflow
Solution 4 - VimAlok SinghalView Answer on Stackoverflow
Solution 5 - VimsolotimView Answer on Stackoverflow
Solution 6 - VimloeschgView Answer on Stackoverflow
Solution 7 - VimfalcucciView Answer on Stackoverflow
Solution 8 - VimSwissView Answer on Stackoverflow
Solution 9 - VimxaaView Answer on Stackoverflow
Solution 10 - VimPius IKView Answer on Stackoverflow