Blog
3 min read

How to Exit Vim (and Save, or Not): The Keys You Need

Stuck in Vim? Press Esc, then type :wq and Enter to save and quit, or :q! to quit without saving. Why Vim behaves this way, what to do when git opens it for a commit message, the handful of commands worth knowing, and how to make git use a friendlier editor.

To get out of Vim:

  1. Press Esc (maybe twice).
  2. Type one of these, then press Enter:
Type What it does
:wq Save and quit
:q! Quit without saving (throw away changes)
:q Quit (only works if nothing changed)
:x Save (if changed) and quit
ZZ Save and quit (no colon, capital Z twice)

That's it. You're free.

Why Vim is confusing

Vim is a text editor that runs inside the terminal. Unlike most editors, it has modes:

  • Normal mode (where it starts): keys are commands, not text. Typing dd deletes a line.
  • Insert mode: keys type text, like a normal editor. Press i to enter it; you'll see -- INSERT -- at the bottom.
  • Command-line mode: press : from Normal mode to type commands like :wq.

Esc always gets you back to Normal mode, which is why it's step one.

"Why am I in Vim? I didn't open it."

Usually git opened it. When you run git commit without -m, or git merge, or git rebase -i, git opens your default editor — which is often Vim — for you to write a message.

For a commit message:

  1. Press i to start typing.
  2. Type your message on the first line.
  3. Press Esc, type :wq, press Enter. The commit is created.

To cancel the commit, press Esc, type :cq, and press Enter. Vim exits with an error status, so Git aborts. :q! only discards unsaved edits and exits successfully: if Git supplied a non-empty message (for a merge or amend, for example), it can still create the commit. (Vim's quit commands, Write Good Commit Messages)

A few more commands worth knowing

Keys (in Normal mode) Action
i Insert before the cursor
u Undo
Ctrl+r Redo
dd Delete the current line
/word then Enter Search for "word" (n for next)
:w Save without quitting
gg / G Go to the top / bottom

Use a friendlier editor for git

If you'd rather never see Vim again, tell git to use something else:

git config --global core.editor "nano"
# or VS Code (waits until you close the tab)
git config --global core.editor "code --wait"

(Nano Editor Basics)

Or avoid the editor entirely for simple commits:

git commit -m "Fix login redirect"

Vim on a server

On servers you'll often need to edit a config file over SSH, and Vim (or vi) is almost always installed. The same rules apply: i to type, Esc then :wq to save. If the file needs admin rights, open it with sudo vim /etc/..., or you'll get "E45: 'readonly' option is set" when saving. (SSH Into a Server)

The summary

  • Esc, then :wq + Enter to save and quit; :q! to quit without saving.
  • i to start typing, Esc to stop.
  • Set git config --global core.editor nano if you'd rather avoid Vim.

EasySpawn lets Claude Code edit config files on your server for you — no Vim required — and shows you the diff before anything changes. See how it works or join the waitlist.

Related: Terminal for Beginners · Basic Linux Commands · Git and GitHub for Beginners · VS Code for Beginners

Keep reading