365

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

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Ricky
  • 34,377
  • 39
  • 91
  • 131

10 Answers10

671

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.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
zen
  • 12,613
  • 4
  • 24
  • 16
  • 5
    `ps` to view foreground processes http://unix.stackexchange.com/questions/6115/viewing-foreground-process-using-ps – Vlad Vinnikov Oct 29 '12 at 16:47
  • 1
    What if I want to continue running my server while editing my file. For example, let's say I do `nodemon app.js` (nodemon refreshes the server on file edits for you) and then I want to return to my vim editing. Can I do that without two separate terminal windows? – Costa Michailidis Feb 20 '15 at 11:19
  • 5
    or `jobs` to visit the foreground processes with a (to me) more readable title. Then eg `fg %2` to select the second. – isomorphismes Feb 26 '15 at 23:43
  • If this isn't working for you and `control-z` acts as 'undo' check your vimrc to see if `behave mswin` is present. If it is remove it and `control-z` will work – schuess Jan 20 '16 at 14:43
  • Is there a way to prevent vim from auto-saving the file if you suspended vim or do I have some configuration that's doing this? – linuxgnuru Nov 07 '16 at 09:29
  • Works for Windows users too! `:sh` or `Ctrl+Z` will start (a new) command prompt. – anishpatel May 05 '17 at 04:32
  • 3
    in windows you can get back to vim if you type `exit` – JonnyRaa Mar 20 '18 at 16:22
  • so many plugins, formatters, etc break when you use Ctrl+Z. don't do it. – Erik Aronesty May 08 '20 at 05:36
  • Does this work on windows cause. `ctrl+z `is starting a new shell as per the info provide by the CLI. However `fg` is not bringing anything to foregrund. Rather it is being read as `not recognized as internal or external command operable program or batch file`. – Anagh Basak Nov 18 '20 at 07:01
122

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

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Pierre-Antoine LaFayette
  • 24,222
  • 8
  • 54
  • 58
  • 2
    I wish I could upvote this harder. This is exactly why :shell exists. – Randy Morris Dec 10 '09 at 13:55
  • 9
    To be fair, sometimes you want the same environment, not a sub-shell environment. So, if you're looking for changes to environment variables for when you exit Vim completely, you'll want to CTRL-z/:sus. – Tango Bravo Apr 23 '14 at 22:18
  • Can use `$ ps` to find whether we're in a vim sub-shell before calling `$ exit` to avoid closing the terminal accidentally. Found this [here](https://joshtronic.com/2018/02/12/how-to-tell-if-your-shell-is-a-subshell-of-vim/) (I haven't tried the prompt indicator mentioned on the website). – Daniel May 02 '19 at 10:07
38

You can switch to shell mode temporarily by:

:! <command>

such as

:! ls
Jay Zeng
  • 1,423
  • 10
  • 10
23

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.

solotim
  • 1,826
  • 4
  • 24
  • 41
22

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.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Alok Singhal
  • 93,253
  • 21
  • 125
  • 158
10

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
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
loeschg
  • 29,961
  • 26
  • 97
  • 150
6

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

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
falcucci
  • 319
  • 3
  • 5
5

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 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.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Swiss
  • 5,556
  • 1
  • 28
  • 42
5

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>
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
xaa
  • 818
  • 13
  • 13
  • five year later I just would like to mention that using C-d is probably a bad advice as it overwrite Vim default behavior. ctrl-d scoll window towards in the buffer by default. see http://vimdoc.sourceforge.net/htmldoc/scroll.html#CTRL-D – xaa May 25 '20 at 13:27
0

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).

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Pius IK
  • 1
  • 1