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:
- Press
Esc(maybe twice). - 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
dddeletes a line. - Insert mode: keys type text, like a normal editor. Press
ito 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:
- Press
ito start typing. - Type your message on the first line.
- 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"
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.ito start typing,Escto stop.- Set
git config --global core.editor nanoif 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.
Keep reading
What Is n8n? Workflow Automation (and AI Agents) Explained Simply
n8n is a workflow automation tool: you connect apps and APIs with visual nodes so that something happening in one place triggers actions in others. What n8n is used for, how workflows and nodes work, its AI agent features, the fair-code licence, and cloud vs self-hosted.
What Is OpenAI Codex? The Coding Agent Explained
Codex is OpenAI's coding agent: a desktop app, a command-line tool, IDE extensions and cloud tasks that read your code, make changes and run commands. What it does, how it differs from the old 2021 Codex model, what it costs with ChatGPT plans, and how it compares with Claude Code.