Stuck in vim or nano? The Few Keys for a Quick Config Edit
Quick answer
In Vim, press Esc, then type ZZ to save and exit, or :q! and Enter to throw your changes away. In nano, press Ctrl+O to write the file and Ctrl+X to exit. That's enough to get out of either editor.
This is for the moment when you open a terminal session on a Mac, an iPad, or a remote machine over SSH, land in an editor you did not choose, and just need to change one line of a config file. Below are the few keys that matter in each editor, taken from the Vim user manual and the nano cheatsheet.
Before you edit: know which file you are changing
Check the path before you touch anything. Startup files and settings files often have near-identical names, and editing the wrong one is the usual reason a change seems to do nothing. If you are in a shell file, which zsh startup file runs when tells you where a line belongs. For Git, where Git settings live explains the system, global, and local files.
Then copy the file first. The Vim manual is blunt about why: "Unlike many other editors, Vim does not automatically make a backup file." Once you save, there's no taking it back.
cp config.toml config.toml.bakVim: the few keys that get you in and out
Why it feels stuck: Vim has modes
Vim is modal. The manual puts it this way: "In Normal mode the characters you type are commands. In Insert mode the characters are inserted as text." When you open a file you are in Normal mode, so typing a sentence doesn't type a sentence. It runs commands, which is why a file can seem to jump around or refuse to take text.
Make the edit
Move the cursor to the line you want, then:
- Press
ito start Insert mode. The manual calls it "i for Insert." - Type your change. Press
oinstead if you want a new empty line below the cursor in Insert mode. - Press
Escto stop Insert mode and go back to Normal mode.
If you are ever unsure which mode you are in, press Esc. The manual's "getting out of trouble" advice is: "To get back to Normal mode, no matter what mode you are in, press the Esc key. Sometimes you have to press it twice. If Vim beeps back at you, you already are in Normal mode."
Save and leave, or bail out
From Normal mode:
ZZwrites the file and exits.:q!followed byEnterquits and discards your changes. The colon starts a command,qquits, and the!overrides Vim's reluctance to throw work away.:qon its own works only when there is nothing unsaved. With unsaved edits, Vim refuses and showsE37: No write since last change (use ! to override).
If you made a mess and want to keep editing from the original, :e! reloads the original version of the file.
Search and undo
Two more keys are worth knowing for a config file. Type /string and press Enter to search forward, as in /port. Press n for the next match and N to go the other way, as described in the Vim chapter on moving around. To undo, press u. CTRL-R redoes the undo.
nano: the keys printed at the bottom
nano has no modes. Open the file, move with the arrow keys, and type. The commands use Ctrl combinations, and the cheatsheet lists them:
| Key | What it does | | --- | --- | | Ctrl+O | Offer to write the buffer ("Save as") | | Ctrl+X | Close the buffer and exit from nano | | Ctrl+F | Start a forward search | | Ctrl+K | Cut the current line into the cut buffer | | Ctrl+U | Paste the cut buffer | | Alt+U | Undo the last action | | Alt+G | Go to a specified line | | Ctrl+G | Display help text |
For a quick edit, the routine is: change the text, press Ctrl+O and follow the prompt to write the file, then press Ctrl+X to leave. Only looking? Ctrl+X alone closes it. Alt+N turns line numbers on and off, which helps when an error message points at a line.
On a Mac, the Alt key is usually Option, and some terminals need a setting turned on before it sends Alt combinations. If Alt+U does nothing, check your terminal's keyboard settings first. Ctrl+G shows help inside nano, so you can look up anything not listed here.
If the file will not save
When a save fails, the usual cause is that you do not have write access to the file. File permissions and chmod modes shows how to read the rwx letters so you can see who owns the file and what you are allowed to do. In Vim, leave with :q! rather than forcing anything, then check the permissions before trying again. If you have no right to change a file, ask whoever manages the machine rather than working around it.
Which one to reach for
Use whichever editor you landed in. If you have a choice and are not sure, nano shows its commands on screen and has no modes, which makes it the gentler choice for one small edit. Vim rewards a few more keys but punishes guessing, so learn those first. Either way, the safe sequence is the same: copy the file, make one change, save, and check the result.
Doing this with Carets
Carets is a fast, native notes and code editor for iPhone, iPad, and Mac, built around Markdown, plain text, and code. A terminal editor is the right tool for a one-line fix on a server, but the config snippets, commands, and notes you collect around it are easier to keep in an editor with normal keys.
Three features fit this topic. Code syntax highlighting makes config and shell snippets readable, so a stray character stands out. Markdown and plain-text editing keeps a cheat sheet of Vim and nano keys as a file you own. Files, projects, and tags let you group those notes by machine or project. Carets is native to iOS and Mac rather than a web wrapper, so opening a note mid-task is quick. Get Carets on the App Store for iPhone, iPad, and Mac.
Frequently Asked Questions
How do I quit Vim without saving?
Press Esc, then type :q! and press Enter. The exclamation mark tells Vim to override its reluctance to throw away changes.
How do I save and exit in nano?
Press Ctrl+O to write the buffer and follow the prompt, then press Ctrl+X to exit.
Why does Vim say E37: No write since last change?
You typed :q while the file had unsaved edits, so Vim refused to exit. Add ! to discard the changes (:q!), or use ZZ to write the file and exit.
How do I undo a mistake in each editor?
In Vim, press Esc to reach Normal mode, then u; CTRL-R redoes. In nano, press Alt+U to undo and Alt+E to redo.