Over the years, Nano has grown to enjoy a devout following. It's incredibly powerful and almost intuitive for novices, making it a fantastic addition to any coder or sysadmins’ arsenal. It is a serious competitor for Vi(m) and Emacs power users too.

Unfortunately, though, there are still a few things about the editor that confuse beginners, and some of those are how to exit Nano and how to delete lines in Nano. This tutorial will show you how save and exit Nano, but will also cover some of the basic functions:

Sign up for BitLaunch and spin up an anonymous VPS to use in this exit Nano tutorial.

Nano save and exit


There are two ways to save and exit nano:

  1. Press Ctrl + X, followed by "Y" when the prompt "Save modified buffer? " appears
  1. Press Ctrl + O, then Enter to save, followed by Ctrl + X to quit.


We prefer method one because it's more foolproof — there's no opportunity for you to press "N" in a moment of insanity and lose all of your progress.

How to save in nano (without exiting)


If you want to save your progress in nano without exiting back to the terminal, the process is simple:

  1. Press Ctrl + O.
  2. Type your desired filename.
  1. Press Enter.

That's it. Nano will even autofill the original filename for you so that you don't have to type it.

How to exit nano without saving


If you want to discard your changes in nano, the easiest way is to:

  1. Press Ctrl + X
  2. Type N when asked if you want to save the modified buffer.
  1. Press Enter.

This will exit nano without keeping any of the changes you made since your last save.

For some explainers of basic Linux tools, consider our guide to permissions on Linux and on finding files via the command line.

Troubleshooting


Most of the time, nano is simple and intuitive to use, but there are a few common issues users run into when saving.

Permission denied when saving


A frequent hiccup in server administration is editing a file with regular nano only to find a "permission denied" error when trying to save it. This is because the file belongs to root, and you must use sudo to save in that location. If you don't want to open the file with sudo and make the changes again, you can follow this process:

  1. Press Ctrl + O and write a different path in the filename. For example,/home/documents/myfile.txt. Press Ctrl + X to exit.
  2. Copy the file you just saved back to the original location with sudo cp /home/document/myfile.txt /path/to/original/myfile.txt. This will replace the existing file with your changed one.

File opened as read-only


Files opening as read-only in nano is usually a permissions issue. Essentially, you have permission to view files in the directory, but not modify them. This can also sometimes occur if the underlying file system is mounted as read-only.

The first and easiest thing to try is opening the file with sudo nano instead. If it's a permissions issue, the read-only warning will probably go away immediately. If that doesn't work, make sure your partition isn't read-only: mount | grep ' ro,'. If it is, you'll have to check your mount options or run a disk repair.

If all else fails, you may be able to move the file to a different directory and edit it there using mv /path/to/your/file.txt /path/to/destination/file.txt

Each line indenting more than the last when pasting


This is due to the auto indent feature. Press Meta/Alt/Windows + I to turn it off, then paste again.

Accidentally pressed Ctrl+X


If you accidentally pressed Ctrl + X when editing in nano, don't panic. You have a two options to keep your progress:

  1. Press Ctrl + C to cancel the command and return to the editor
  2. Press Y to save the progress and simply nano into the file again.

Saving overwrites the wrong file


If saving in nano overwrites the wrong file, simply type the correct file name after pressing Ctrl + O to have it overwrite that one instead.

FAQs


How do I back up a file before editing?


Type cp -p filename filename.bak to create a copy of it with the same contents and permissions.

How can I open a file for editing with nano?


By typing nano /path/to/filename.txt.

How do I use view mode in nano?


You can type nano -v filename or nano --view filenameto open a file in view mode, which will disable editing.

What's the difference between Ctrl + O and Ctrl + X in nano?


Ctrl + O saves the document, while Ctrl + X quits it and asks if you'd like to save if there are unsaved changed. Saving either way is functionally the same, but some prefer to use the Ctrl + X method because it's one less shortcut.