Edit Files in Terminal With nano on Mac
Sooner or later a guide tells you to edit a config file in Terminal, and you need an editor that won't trap you. That editor is nano: your arrow keys work, the important shortcuts are printed at the bottom of the screen, and quitting is one keystroke — not a puzzle. Here is everything you need to edit, save, and get out.
Open, edit, save, exit
- Run
nano filename— for examplenano ~/.zshrc. If the file doesn't exist, nano opens a new buffer and creates it when you save. - Type normally. Arrow keys move the cursor; there are no modes to switch, unlike vim.
- Press Ctrl+O (WriteOut) to save, then Return to confirm the filename.
- Press Ctrl+X to exit. If you have unsaved changes, nano asks Save modified buffer? — press Y then Return, or N to discard.
That last prompt is also your undo for a botched editing session: exit with N and the file on disk is untouched.
The ^ symbol in the shortcut bar means the Control key, not Command — ^O is Ctrl+O. One footnote for the curious: since macOS 12.3, the nano command has actually launched pico, a nearly identical editor Apple ships for licensing reasons — so the nano on Ventura, Sonoma, and Sequoia is pico under the hood. Every shortcut in this guide works the same; if you want full GNU nano, brew install nano gets you the real thing.
Search and move around
- Ctrl+W ("Where is") — search. Type the text, press Return, and the cursor jumps to the first match. Press Ctrl+W then Return again to jump to the next one.
- Ctrl+V / Ctrl+Y — page down / page up through long files.
- Ctrl+A / Ctrl+E — jump to the start / end of the current line.
- Ctrl+K — cut the current line. Ctrl+U pastes it back — cut and paste in sequence to move lines, or Ctrl+K repeatedly to delete several.
- Ctrl+C — show the current line and column number.
- Ctrl+G — the full help screen, with Ctrl+X to leave it.
Editing protected files with sudo
System files like /etc/hosts are writable only by an administrator. Prefix the command with sudo and enter your password:
# back up first — this is your undo
sudo cp /etc/hosts /etc/hosts.bak
# edit the protected file
sudo nano /etc/hosts
# if something breaks, restore the backup
sudo cp /etc/hosts.bak /etc/hosts
Two cautions. First, make the backup copy every time — a one-character typo in a system file can be genuinely hard to diagnose later. Second, if you opened a system file without sudo, nano will let you edit but fail at the save with "Permission denied"; exit with Ctrl+X, then N, and rerun the command with sudo.
Small flags that help
nano +25 file— open with the cursor on line 25, handy when an error message names the line.nano -w file— don't wrap long lines, important for config files where a line break changes meaning.
One habit worth forming: make nano your system-wide default editor, so tools like git and crontab open it instead of vim. Add this line to ~/.zshrc:
# make nano the default for git, crontab, and friends
export EDITOR=nano
# undo: delete the line and open a new window
Reload with source ~/.zshrc, and every tool that respects the EDITOR variable — most do — will drop you into the editor you can actually exit.
Config files are for the shell. For everything else, Mainspring turns 90+ hidden macOS settings into labelled toggles — each one reversible with a click, no backup copies required.
Try Mainspring free →Signed & notarized by Apple · 1-day free trial · $29 once
Where you'll use it next
The file most Mac users end up editing first is their shell config — aliases, PATH, prompt. Our .zshrc guide gives you a worthwhile file to practice on.