nano Save and Exit: Save Files and Quit the Editor

By 

Updated on

6 min read

Saving and exiting a file in the Nano text editor

When you edit a config file in nano, the most common point of confusion is leaving the editor without losing your changes. Nano uses keyboard shortcuts instead of menus, so saving and exiting depends on a few key combinations.

This guide shows how to save a file in nano, exit nano, save and quit in one flow, and discard changes when you do not want to keep them.

For a broader walkthrough of editing, navigation, and search, see how to use the Nano text editor .

Quick Reference

ActionShortcut
Open a filenano filename
Save changes (stay in editor)Ctrl+O, then Enter
Save without a promptCtrl+S
Save and exitCtrl+X, then Y, then Enter
Exit without savingCtrl+X, then N
Save under a new nameCtrl+O, edit filename, then Enter
Cancel exit promptCtrl+X, then Ctrl+C
Show all commandsCtrl+G

For a printable quick reference, see the nano cheatsheet .

Nano’s Command Notation

At the bottom of the screen, Nano shows two helplines with commonly used commands. All commands in Nano are prefixed with either ^ or M characters.

  • The caret symbol (^) represents the Ctrl key. For example, ^X means press Ctrl and X at the same time.
  • The letter M represents the Alt key.

To see the complete list of commands, press Ctrl+G.

Most Linux systems use nano’s default shortcuts, where Ctrl+O writes the file and Ctrl+X exits. Since nano 2.9.0, the default set also binds Ctrl+S to saving the current file, so a plain save does not have to go through the write prompt at all.

Nano 8.0 added an optional set of modern bindings, enabled by starting the editor with nano -/ or nano --modernbindings. Those bindings follow the conventions of graphical editors: Ctrl+S saves, Ctrl+Q quits, and Ctrl+X cuts the current line rather than exiting. If Ctrl+X starts deleting text instead of closing the editor, that is the reason.

Opening a File

To open an existing file or create a new one, type nano followed by the file name:

Terminal
nano filename

A new editor window will open and you can start editing the file.

Saving Changes in Nano

When you finish making changes, press Ctrl+O (the letter O, not zero) to save your work.

Nano will display the current filename at the bottom and prompt you to confirm.

  • Press Enter to save the file under the same name.
  • Or type a new name and press Enter to save it as a different file.
Nano filename prompt shown after pressing Ctrl+O to save a file

The wording of that prompt depends on the nano version. Nano 8.3 and older label it File Name to Write:, as in the screenshot above, while nano 8.4 and newer label it Write to File:. The keys behave the same way in both.

The help lines under the prompt list the options you can apply before the file is written: M-D switches to DOS line endings, M-A appends the buffer to the target file, M-P prepends it, M-B keeps a backup copy of the previous contents, and ^T opens a file browser. Nano 9.0 and older also show M-M for old Mac line endings, but nano 9.1 removed that format. Press Ctrl+C to leave the prompt without writing anything.

Warning
To save a file, you must have write permission to that file. If you are creating a new file , you need write permission to the directory where the file will be saved.

Saving with Ctrl+S

When the file already has a name, Ctrl+S is the shorter route. It writes the buffer straight to disk without asking anything and reports the result on the status bar:

output
[ Wrote 24 lines ]

Nano has bound Ctrl+S to this direct save since version 2.9.0, so it is available on current distributions without any configuration. The one exception is a buffer that does not have a filename yet, such as the one you get from a bare nano, where Ctrl+S opens the same write prompt that Ctrl+O uses.

Exiting Nano

When you are done and want to exit Nano, press Ctrl+X.

If you have not modified the file, Nano will exit immediately and return you to the terminal.

If there are unsaved changes, Nano will ask: “Save modified buffer?”

Nano save modified buffer prompt shown before exiting with unsaved changes
  • Y: Save changes. Nano will show the filename and prompt you to confirm. Press Enter to save and exit.
  • N: Discard changes and return to the terminal.
  • Ctrl+C: Cancel the exit prompt and stay in the editor.

Troubleshooting

“Permission denied” when saving
Nano reports the failure on the status bar, for example Error writing /etc/hosts: Permission denied. You do not have write permission to the file or its directory. Use ls -l to check permissions. If you need root access, reopen the file with sudo nano filename.

File opened as read-only
Nano shows File '/etc/hosts' is unwritable on the status bar as soon as the buffer loads. You can still type in it, but the write will fail, so exit with Ctrl+X and reopen the file with sudo nano filename. View mode is a separate case: a file opened with nano -v shows View in the title bar, and nano refuses the edits themselves.

Accidentally pressed Ctrl+X
If Nano shows the save prompt, press Ctrl+C to cancel and return to the editor without losing your changes.

Saving overwrites the wrong file
When Nano prompts for the filename, clear the current name and type a new one before pressing Enter to save as a different file.

Ctrl+S freezes the terminal
That happens outside nano, where Ctrl+S is the XOFF character that pauses terminal output. Press Ctrl+Q to resume. Inside the editor the key saves the file, unless nano was started with --preserve, which hands Ctrl+S and Ctrl+Q back to the terminal.

Ctrl+X cuts a line instead of exiting
Nano is running with modern bindings, started with nano -/ or nano --modernbindings. Press Ctrl+Q to quit, or restart the editor without that option.

FAQ

What is the difference between Ctrl+O and Ctrl+X in Nano?
Ctrl+O saves the file and keeps the editor open so you can continue editing. Ctrl+X exits the editor, prompting you to save if there are unsaved changes.

How do I save a file in Nano without exiting?
Press Ctrl+O, then press Enter to confirm the filename. Nano saves the file and you remain in the editor.

How do I exit Nano without saving changes?
Press Ctrl+X, then press N at the “Save modified buffer?” prompt. Nano discards all unsaved changes and returns you to the terminal.

How do I save a file under a different name in Nano?
Press Ctrl+O. When Nano shows the filename prompt, clear the current name, type the new filename, and press Enter.

What does “Save modified buffer?” mean in Nano?
It means Nano has detected unsaved changes. Press Y to save, N to discard changes and exit, or Ctrl+C to cancel and return to editing.

Conclusion

In default nano keybindings, use Ctrl+O or Ctrl+S to save without closing and Ctrl+X to leave the editor. If nano asks whether to save the modified buffer, choose Y to keep the changes or N to discard them.

Tags

Linuxize Weekly Newsletter

A quick weekly roundup of new tutorials, news, and tips.

About the authors

Dejan Panovski

Dejan Panovski

Dejan Panovski is the founder of Linuxize, an RHCSA-certified Linux system administrator and DevOps engineer based in Skopje, Macedonia. Author of 1000+ Linux tutorials with 20+ years of experience turning complex Linux tasks into clear, reliable guides.

View author page