5175

I am stuck and cannot escape. It says:

type :quit<Enter> to quit VIM

But when I type that it simply appears in the object body.

unrealapex
  • 578
  • 9
  • 23
jclancy
  • 49,598
  • 5
  • 30
  • 34
  • 205
    Are you just trying to quit VIM ? If this is the case, press "escape" and then type ':q' – Pop Aug 06 '12 at 12:28
  • 74
    Don't forget the **colon**! You should type `:quit` and then hit the `[ENTER]` key. – Farahmand Mar 04 '14 at 18:33
  • 153
    It's really easy to learn the basics of vim, and it's built right into your system. In terminal type "vimtutor". 25 minutes later you will be going faster than your usual text editor! – Mark Robson Jan 26 '15 at 12:11
  • 14
    Check [here](http://www.fprintf.net/vimCheatSheet.html) more commands. – Toni Aug 07 '15 at 15:36
  • 137
    To prevent `git commit` sending you to vim in the future: `git config --global core.editor="nano"` – Tom Kelly May 24 '17 at 03:19
  • 3
    Not mentioned yet; but if you are working in a GUI, you can use gvim and close the window. (If there are unsaved changes it will prompt Save/Discard/Cancel). – M.M May 24 '17 at 04:02
  • 2
    @Toni Why not use a printable single page cheat sheet? [Regular qwerty version](http://www.viemu.com/vi-vim-cheat-sheet.gif) and [dvorak](http://www.viemu.com/vi-vim-dvorak-cheat-sheet.gif). – nilon Nov 29 '18 at 13:15
  • 3
    From the command line you can quit vim as soon as you entered it: `vim --cmd q` . This can be used along with `-V` option: `vim -V20vim.log --cmd exe +q`. The newly created vim.log can be useful for diagnostic/debugging purposes. – builder-7000 Feb 21 '19 at 01:11
  • 9
    @TomKelly Thanks a lot it helped me but it's actually `git config --global core.editor "nano"` with a space, not a `=` sign – Jérôme MEVEL Apr 17 '19 at 07:29
  • You can search in :help like in every other window by typing / which searches for occurences of – Ahmad Dec 29 '20 at 00:13
  • 1
    This question and thread are very helpful (and answered my question), but it's a shame that Vim is so hard to figure out, that people have to resort to using a search engine, and a 3rd party website, to figure out how to get Vim to do something as simple as quitting without saving. – adam-asdf Sep 04 '21 at 06:15
  • I couldn't figure out how to open a file for editing. So, I used the -y argument because the --help said that it would make VIM easy. Now, ESC does nothing. ^C does nothing. : just types a colon. ESC ESC ESC : just types a colon. All I could do was kill the terminal. – Philip Young Sep 09 '21 at 16:14
  • 1
    @adam-asdf It's not a shame. Vim is a powerful tool and it takes time learn how to use powerful tools correctly. The initial cost is more than offset over time. Not everything can be simple and obvious to users of all levels of experience. – user229044 Sep 30 '21 at 23:56

13 Answers13

6209

Hit the Esc key to enter "Normal mode". Then you can type : to enter "Command-line mode". A colon (:) will appear at the bottom of the screen and you can type in one of the following commands. To execute a command, press the Enter key.

  • :q to quit (short for :quit)
  • :q! to quit without saving (short for :quit!)
  • :wq to write and quit
  • :wq! to write and quit, attempting to force the write if the file lacks write permission
  • :x to write and quit; like :wq but writes only if modified (short for :exit)
  • :qa to quit all (short for :quitall)
  • :cq to quit, without saving, with a nonzero exit code to indicate failure (short for :cquit)

You can also quit Vim directly from "Normal mode" by typing ZZ to save and quit (same as :x) or ZQ to just quit (same as :q!). (Note that case is important here. ZZ and zz do not mean the same thing.)

Vim has extensive help - that you can access with the :help command - where you can find answers to all your questions and a tutorial for beginners.

Kevin Bourrillion
  • 40,336
  • 12
  • 74
  • 87
dirvine
  • 57,399
  • 2
  • 18
  • 19
  • 24
    Unless you have remapped esc or have a weird mapping in your .vimrc then it definitely should. If on linux type xev and make sure escape is the keytype you get when you hit escape. – dirvine Jun 11 '14 at 23:49
  • 30
    Remember you can use ctrl+c if you can't use Esc (like me because my shell is in TotalTerminal). http://vim.wikia.com/wiki/Avoid_the_escape_key – dotnetCarpenter Jan 27 '15 at 15:12
  • 33
    `:x` == `ZZ` but `:x` != `:wq`. `:x` write file iff file has changed, `:wq` write file always (matter i.e. when using `inotify`). – Hauleth Feb 05 '15 at 00:27
  • 57
    To be honest, I have a harder time using vim's help system than using vim itself, and mostly rely on quick ref cards and online documentation. – bgvaughan Jul 08 '15 at 06:40
  • 3
    @bgvaughan I don't find vim's help any use at all to discover and learn about new features. I use it purely as a reference. Fortunately there are books (practical vim) and sites (learn vimscript the hard way) which help here, plus there's a stack exchange site devoted to vim http://vi.stackexchange.com/) –  Sep 24 '15 at 10:20
  • 4
    The first statement ("Hit the Esc key; that goes into command mode.") is based on a common misunderstanding of Vi(m). The Esc (if needed) is the final part of the *current* command, and when you finish it this way you can type the next command. – Olaf Seibert Oct 28 '15 at 16:23
  • 1
    `Esc` only goes to command mode if you're still in input mode. If you're in Ex mode, it does not: `E492: Not an editor command: ^[:q` – aij Jul 21 '16 at 18:07
  • 1
    `:x!` does as it mentioned but add confirmation as well. I always use this instead of `:wq!` – Mark Jan 05 '17 at 19:29
  • 27
    if you don't have permissions on the file but have sudo permissions `:w ! sudo tee %` – tvlooy May 23 '17 at 18:53
  • 1
    What's the difference between "Save and quit" and "Write and quit"? – Stevoisiak May 25 '17 at 03:20
  • 2
    If you have only read only permissions to the file, `:wq!` won't write it. If that happened, it would be a security flaw in the OS. – sid-m May 25 '17 at 05:48
  • 1
    @StevenVascellaro There is no difference; writing to a file and saving the file mean the exact same thing. – victor Jul 19 '17 at 22:17
  • 1
    What if there is no escape key?? – Elliot Cameron Oct 28 '17 at 18:17
  • 2
    The use `ctrl [` to do the same. – dirvine Oct 29 '17 at 12:30
  • 1
    Sometimes, `Esc` is not enough. You may need to type `Esc` multiple times to be sure. (E.g. After pressing `^V` or `^O` in Insert mode.) (Of course, [Heikki](https://stackoverflow.com/a/31028197/712526) covered this in depth.) – jpaugh Feb 12 '18 at 23:23
  • 1
    You can search in `:help` like in every other window by typing `/` which searches for occurences of `` – WorldSEnder Jun 17 '18 at 21:07
  • 1
    @sid-m Yes and no. That depends on if you're say root. But generally speaking you're right. It would be more correct to say that it works if vim is in read only mode. Then assuming you have permission to write to the file itself then you can force a write (it will turn off read only mode). – Pryftan Nov 10 '19 at 19:42
444

Pictures are worth a thousand Unix commands and options:

Enter image description here

I draw this to my students each semester and they seem to grasp vi afterwards.

Vi is a finite state machine with three main states.

It starts in COMMAND mode, where you perform editor functions using very short keystroke sequences, blindly. You know what you are doing; this isn't for amateurs.

When you want to actually edit text, you should go to INSERT mode with some keystroke; common ones include:

  • i: insert just before the cursor
  • I: move to beginning of line and (i)nsert
  • a: append just after the cursor
  • A: move to end of line and (a)ppend
  • o: open a new line just below the current line
  • O: open a new line just above the current line
  • R: enter REPLACE mode (similar to INSERT mode)

Now, answering the question: exiting.

You can exit vi from EX mode:

  • q - if you haven't made any modifications, or saved them beforehand
  • q! - ignores any modifications and quit
  • wq - save and quit
  • x - this is similar to wq

w and x accept a file name parameter. If vi already knows the filename to use (e.g. it was started with vi file), you need not give it here again.

At last, the most important: how can you reach EX mode?

EX mode is for long commands that you can see typing at the bottom line of the screen. From COMMAND mode, you press colon, :, and a colon will appear at the bottom line, where you can type the above commands.

From INSERT mode, you need to push ESC, i.e. the Escape button, going to COMMAND mode, and then: to go to EX mode.

If you are unsure, push ESC and that will bring you to command mode.

The robust method is ESC-:-x-Enter which saves your file and quits.

Kevin Bourrillion
  • 40,336
  • 12
  • 74
  • 87
Gergely
  • 6,879
  • 6
  • 25
  • 35
  • 19
    Thank you, the image is very helpful. However, for me `w` doesn't change from Ex to Command mode, but `Esc` does. What am I doing wrong? – Nick Volynkin May 28 '17 at 05:24
  • 4
    If you write w-Enter that saves your file and goes back to COMMAND mode. I wrote it to have a full picture of save & quit commands. – Gergely May 28 '17 at 05:35
  • 6
    oh, so you mean `:w`. Then it makes perfect sense. By the way, is there a command to reload from disk (that is, to revert changes but not close the file)? If so, it could be next to `w` in the diagram. – Nick Volynkin May 28 '17 at 09:50
  • 37
    What you've labeled *command* mode is actually *normal* mode. What you've labeled *ex* mode is actually *command* mode. **Ex** mode is a different beast altogether! – jpaugh Feb 12 '18 at 23:35
  • @jpaugh do you have a reference for this? – Gergely Feb 13 '18 at 08:44
  • 3
    @Gergely Well, I finally found the vim documentation: `:help vim-modes`. – jpaugh Feb 13 '18 at 14:58
  • 1
    About *Ex* mode: "Like Command-line mode, but after entering a command, you remain in Ex mode." The only way to get out of ex mode (that I know) is to use `:vi` to go back to normal mode. Regular command mode "releases" you to normal mode as soon as you enter one command. – jpaugh Feb 13 '18 at 15:00
  • (FWIW, I learned about ex mode the hard way: I entered it accidentally (several times), and was trapped until I used `^C` or learned about it. I never forgot the lesson! ;-) – jpaugh Feb 13 '18 at 15:01
  • 2
    @Gergely I just figured out the confusion! From the docs, normal mode is also called *command mode*! So, command mode != command-line mode. Wow! – jpaugh Feb 13 '18 at 15:04
  • 2
    @NickVolynkin the command to reload from disk is `:e` (or more likely `:e!` since it will warn you about unsaved changes without the `!`) – JDS Mar 26 '18 at 13:15
  • I dunno about O.G. `vi` (the visual mode for ex), but in vim, the modes are `normal`, `insert`, `Ex`, `command`, and `visual`. Normal is the default mode where pressing `:q` will exit vim. Ex mode emulates ex and is accessed via `Q`. Command mode is accessed via `:` from normal and visual mode, and visual/visual-line/visual-block mode is a neat mode lets you highlight text and perform special operations (Try Ctrl-v and go down until you have the right-most column selected for a whole block, then press `A` to go into insert mode and type. The text will be appended on every line you selected) – Braden Best Apr 01 '18 at 22:38
  • Isn't there a longer name for *"EX"*? *"Extended"*? – Peter Mortensen Aug 21 '19 at 09:58
  • 2
    @PeterMortensen `ed` is the original text editor. From there we got `em` which added video input, and `ex` (short for EXtended) is basically a less processor-demanding `em`. `vi` is built upon `ex`, and `vim` is built upon `vi`. `ed` => `em` => `ex` => `vi` => `vim` (=> `gvim`?) – mazunki May 16 '20 at 15:12
  • In original `vi` there is "open mode" as well, which is kind of a one-line normal mode with its own variant on insert mode. This is useful if you ever take a time machine back to the 1980s and have to work on a 1200 baud modem. You get there by typing :o – gpvos Jul 01 '20 at 10:18
  • 1
    says something about the learning curve of vi, that you should know about state machines before attempting to use it – Nikolai R Kristiansen Apr 26 '21 at 09:21
  • Unix is created by programmers for programmers – Gergely Apr 26 '21 at 10:32
  • So :x is actually shorthand for :wq?? Thank you! Haven't known this for years of occasionally using vim ^^ – Jan Z. Feb 04 '22 at 11:54
205

Before you enter a command, hit the Esc key. After you enter it, hit the Return to confirm.

Esc finishes the current command and switches Vim to normal mode. Now if you press :, the : will appear at the bottom of the screen. This confirms that you're actually typing a command and not editing the file.

Most commands have abbreviations, with optional part enclosed in brackets: c[ommand].

Commands marked with '*' are Vim-only (not implemented in Vi).

Safe-quit (fails if there are unsaved changes):

  • :q[uit] Quit the current window. Quit Vim if this is the last window. This fails when changes have been made in current buffer.
  • :qa[ll]* Quit all windows and Vim, unless there are some buffers which have been changed.

Prompt-quit (prompts if there are unsaved changes)

  • :conf[irm] q[uit]* Quit, but give prompt when there are some buffers which have been changed.
  • :conf[irm] xa[ll]* Write all changed buffers and exit Vim. Bring up a prompt when some buffers cannot be written.

Write (save) changes and quit:

  • :wq Write the current file (even if it was not changed) and quit. Writing fails when the file is read-only or the buffer does not have a name. :wqa[ll]* for all windows.
  • :wq! The same, but writes even read-only files. :wqa[ll]!* for all windows.
  • :x[it], ZZ(with details). Write the file only if it was changed and quit, :xa[ll]* for all windows.

Discard changes and quit:

  • :q[uit]! ZQ* Quit without writing, also when visible buffers have changes. Does not exit when there are changed hidden buffers.
  • :qa[ll]!*, :quita[ll][!]* Quit Vim, all changes to the buffers (including hidden) are lost.

Press Return to confirm the command.

This answer doesn't reference all Vim write and quit commands and arguments. Indeed, they are referenced in the Vim documentation.

Vim has extensive built-in help, type Esc:helpReturn to open it.

This answer was inspired by the other one, originally authored by @dirvine and edited by other SO users. I've included more information from Vim reference, SO comments and some other sources. Differences for Vi and Vim are reflected too.

L3viathan
  • 26,748
  • 2
  • 58
  • 81
Nick Volynkin
  • 14,023
  • 6
  • 43
  • 67
104

If you want to quit without saving in Vim and have Vim return a non-zero exit code, you can use :cq.

I use this all the time because I can't be bothered to pinky shift for !. I often pipe things to Vim which don't need to be saved in a file. We also have an odd SVN wrapper at work which must be exited with a non-zero value in order to abort a checkin.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Sue Mynott
  • 1,287
  • 1
  • 9
  • 14
97

This is the ultimate, no-nonsense, hardcore exit command for the worst-case scenarios of exiting Vim if you want out, have no idea what you've done and you don't care what will happen to the files you opened.

Ctrl-cEnterCtrl-\Ctrl-nEnter:qa!Enter

This will get you out most of the time. Most.

You might think, naively, that you can just bang Esc a few times and then do :qa!. Oh, how you would be mistaken.

See these interesting cases where hitting Esc is not enough:

  • iCtrl-ovg (you enter insert mode, then visual mode and then operator pending mode)

  • QappendEnter

  • iCtrl-ogQCtrl-r=Ctrl-k (thanks to porges for this case)

  • iCtrl-ogQCtrl-r=inputdialog('foo','bar')Enter

  • :set insertmode (this is a case when Ctrl-\Ctrl-n returns you to normal mode)

Heikki Naski
  • 2,610
  • 1
  • 21
  • 13
  • 12
    @cavalcade This is an extremely general method of ensuring the editor is in normal mode then safely quitting. In normal usage all you need is `:q` or `:wq` – Dan Passaro May 23 '17 at 19:55
  • 9
    Not general enough! What about if I (a beginner) typed `igQ=`? – porges May 24 '17 at 05:33
  • 2
    @porges thanks for the test case! There are also some other test cases which prove that the original method was woeful. I edited the answer to be more general now. – Heikki Naski May 24 '17 at 06:07
  • @HeikkiNaski at least for me I need Enter after Ctrl-C as well (to escape the expression register) – porges May 24 '17 at 07:26
  • Somehow I am still stuck in it after executing these steps. – 6infinity8 Feb 16 '23 at 10:36
52

In case you need to exit Vim in easy mode (while using -y option) you can enter normal Vim mode by hitting Ctrl + L and then any of the normal exiting options will work.

K.Dᴀᴠɪs
  • 9,945
  • 11
  • 33
  • 43
wsams
  • 2,499
  • 7
  • 40
  • 51
  • 9
    Yet another option: you can use `Ctrl+O` to leave INSERT mode temporarily then enter `:q`. Trick with this combination is useful in normal vim as well to execute single command and return back to INSERT mode. – Andrey Starodubtsev Sep 17 '15 at 12:08
43

Vim has three modes of operation: Input mode, Command mode & Ex mode.

Input mode - everything that you type, all keystrokes are echoed on the screen.

Command mode or Escape mode - everything that you type in this mode is interpreted as a command.

Ex mode - this is another editor, ex. It is a line editor. It works per line or based on a range of lines. In this mode, a : appears at the bottom of the screen. This is the ex editor.

In order to exit Vim, you can exit while you are in either the ex mode or in the command mode. You cannot exit Vim when you are in input mode.

Exiting from ex mode

  1. You need to be sure that you are in the Command mode. To do that, simply press the Esc key.

  2. Go to the ex mode by pressing the : key

  3. Use any of the following combinations in ex mode to exit:

    :q - quit :q! - quit without saving :wq - save & quit or write & quit :wq! - same as wq, but force write in case file permissions are readonly :x - write & quit :qa - quit all. useful when multiple files are opened like: vim abc.txt xyz.txt

Exiting from command mode

  1. Press the escape key. You probably have done this already if you are in command mode.

  2. Press capital ZZ (shift zz) - save & exit

  3. Press capital ZQ (shift zq) - exit without saving.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
tovishalck
  • 990
  • 8
  • 18
  • 5
    What you're calling "ex mode" is actually called [*command-line mode*](http://vimdoc.sourceforge.net/htmldoc/intro.html#vim-modes-intro). It allows you to enter ex commands, but with some [important differences](http://vimdoc.sourceforge.net/htmldoc/intro.html#Ex-mode). – jpaugh Feb 13 '18 at 15:07
  • 1
    Hmm no. You're forgetting visual mode. – Pryftan Nov 10 '19 at 19:47
  • Save(?)+Exit from insert mode: `Alt Z` `Z` That is, press: `alt-shift-z` then `shift-z` Fun fact: From insert mode, you can exit to command mode and do a command by holding Alt with the command character. – David Lotts Dec 30 '21 at 07:02
  • I think what you refer to with "ex mode" is the normal, or "command" mode. To enter the ex-mode you have to press `Q`. In that mode you can enter one command after another without pressing `:` every time. To exit ex-mode you can type `:vi` or `:visual` – void Feb 18 '22 at 08:38
36

After hitting ESC (or cmd + C on my computer) you must hit : for the command prompt to appear. Then, you may enter quit.

You may find that the machine will not allow you to quit because your information hasn't been saved. If you'd like to quit anyway, enter ! directly after the quit (i.e. :quit!).

mtb
  • 1,350
  • 16
  • 32
deleteMe
  • 383
  • 3
  • 7
30

I got Vim by installing a Git client on Windows. :q wouldn't exit Vim for me. :exit did however...

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Bjørn van Dommelen
  • 1,057
  • 7
  • 13
  • 1
    Similarly for vim doing git on a macintosh this worked. – Joel Apr 29 '15 at 09:55
  • 1
    @Joel just checked this on my mac, both commands are legit (`vim -version` 7.3). – Nick Volynkin Jun 08 '15 at 13:47
  • 2
    For `Git Bash` on windows, in `Vim` sometimes `ESC` not working. use `CTRL` + `[` instead. – Val Sep 08 '17 at 06:53
  • 1
    @Val That looks strangely familiar. Iirc that's because the two do the same thing. As in `^[` is the same as `ESC`. That's interesting that Windows makes it more complicated. And amusing. – Pryftan Nov 10 '19 at 19:49
17

The q command with a number closes the given split in that position.

:q<split position> or :<split position>q will close the split in that position.

Let's say your Vim window layout is as follows:

-------------------------------------------------
|               |               |               |
-------------------------------------------------
|               |               |               |
|               |               |               |
|    Split 1    |    Split 2    |     Split 3   |
|               |               |               |
-------------------------------------------------

If you run the q1 command, it will close the first split. q2 will close the second split and vice versa.

The order of split position in the quit command does not matter. :2q or :q2 will close the second split.

If the split position you pass to the command is greater than the number of current splits, it will simply close the last split.

For example, if you run the q100 on the above window setup where there are only three splits, it will close the last split (Split 3).

The question has been asked here.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Subash
  • 7,098
  • 7
  • 44
  • 70
9

One guaranteed way is to kill the port that runs vim

! kill - 9 $(ps | grep vim | cut -d " " -f 1)
Zoe
  • 27,060
  • 21
  • 118
  • 148
Tilak Madichetti
  • 4,110
  • 5
  • 34
  • 52
6

From any of four modes: insert, visual, command or command-line (ex) mode, press this to save if changed then exit vim:

Alt-Z Z

That is, press: alt + shift + z then shift + z

Why this works: From insert mode and apparently other modes, you can exit to command mode and do a command by the combination of holding Alt with any command character. The second Alt+Shift+Z is ignored, so you must let go of the Alt but keep the Shift. Work that muscle memory! :-)

David Lotts
  • 550
  • 5
  • 10
4

Q-first vs. Colon-first

Many people know about the colon-q exit strategy. But for extra strategery and posterity, here is the q-colon-i-q method:

In Normal mode you can type:

q:iq<enter>

If you look closely and squint, you can almost read that command aloud as "quick," since this is the slow way to exit.

(Note: my attempt at humor notwithstanding, this does work!)