Skip to content
Français
Sync not enabled: your progress stays in this browser My learning
Select theme
Français

Nano: edit, save and exit a file

By updated

Improve this page

nano is a command-line text editor, simple and fast. It is already installed on Debian and Ubuntu, but missing from a minimal AlmaLinux 10 installation. It lets you edit a file with no configuration and no steep learning curve: the shortcuts are shown on screen, and you can use it straight away. It is a good tool for a quick edit, locally or remotely over SSH.

Note: for more complex tasks or intensive development environments, learning vi or vim can prove more powerful and productive in the long run thanks to its advanced features.

What you will learn

  • Open, edit and save a file with nano
  • Use the essential keyboard shortcuts (copy, paste, search)
  • Customise nano through the ~/.nanorc file
  • Avoid mistakes when editing system files

How do you open, create, save and exit a file?

One of the great strengths of nano is its simplicity: you can open, create, edit and save a file in a few keystrokes, without knowing any complex commands.

Edit or create a file

To start nano, simply type the command followed by the file name in your terminal:

Terminal window
nano file_name.txt
  • If the file already exists, it opens in the editor.
  • If the file does not exist, nano opens an empty buffer, that is, a text held in memory under that name. Nothing is written to disk until you write it out (Ctrl + O): exiting without saving creates no file.

Tip: use the Tab key to complete the file name.

Save a file

When your changes are done, press:

Ctrl + O

Nano asks you to confirm the file name to write. Press Enter to confirm.

A message appears at the bottom of the screen to confirm that the file was saved.

Exit nano

To leave the editor, use:

Ctrl + X

Three cases can occur:

  • If no change was made: nano exits straight away.
  • If changes were made: nano asks whether you want to save.
    • Type Y (Yes) to save, then Enter
    • Type N (No) to exit without saving
    • Type Ctrl + C to cancel and go back to editing

Reminder: every shortcut is shown at the bottom of the screen while you use nano, so there is no need to learn them all by heart!

Which keyboard shortcuts should you know?

One of the great strengths of nano is that everything is done from the keyboard, with shortcuts that are easy to remember. What is more, the main shortcuts are shown at the bottom of the screen, which makes it easy to find your way, even for a beginner.

Here are the most useful shortcuts to feel at ease in nano day to day:

Basic navigation

These shortcuts save you from hammering the arrow keys on a file of several hundred lines. The most rewarding one day to day is Ctrl + _: when systemctl or a compiler reports an error "line 148", you jump straight to it instead of scrolling. Note that Ctrl + Y and Ctrl + V move by a whole screen, not by a line: the size of the jump therefore depends on the height of your terminal.

ActionShortcut
Start of lineCtrl + A
End of lineCtrl + E
Go to a specific lineCtrl + _ then the number
Previous pageCtrl + Y
Next pageCtrl + V
Move character by characterArrow keys

Editing text

The right-hand column uses the notation nano itself shows. Two traps to remember: Ctrl + K cuts the whole line (it goes into the internal clipboard, called the cutbuffer), and Ctrl + U pastes that cutbuffer, not your graphical system's clipboard. Chaining several Ctrl + K accumulates lines in the cutbuffer, which lets you move a block in one go. Undo is Alt + U, and it can be repeated.

ActionShortcut
Save the fileCtrl + O
Exit nanoCtrl + X
Cut the current lineCtrl + K
Copy the line/selectionAlt + 6
PasteCtrl + U
Justify a paragraphCtrl + J
Undo the last operationAlt + U
RedoAlt + E

Search and replace

Nano's search works through a prompt: Ctrl + W opens an input line at the bottom, Enter confirms, and Alt + W repeats the same search forwards without retyping the pattern. To go back up the file, use Ctrl + Q, which searches upwards. Watch out for replacement with Ctrl + \: nano asks for the text to find, then the replacement text, then confirms each occurrence one by one; answering A (All) applies the replacement everywhere at once, with no way back other than Alt + U.

ActionShortcut
Search for a word (forwards)Ctrl + W
Search backwardsCtrl + Q
Next / previous matchAlt + W / Alt + Q
Search and replaceCtrl + \

Other handy shortcuts

These toggles change the display or the behaviour for the current session only; nothing is written to the file or to your configuration. Tell the two kinds of wrapping apart: Alt + S enables soft wrapping, which folds the display without touching the contents, whereas Alt + L enables hard wrapping, which inserts real line breaks into the file. On a configuration file, Alt + S is the one you want.

ActionShortcut
Toggle soft wrappingAlt + S
Toggle hard wrappingAlt + L
Toggle line numbersAlt + N
Toggle auto-indentationAlt + I
Open the built-in helpCtrl + G
Run a function or an external commandCtrl + T

Tip: Alt is often written M- in nano's help (M = Meta). Example: M-U = Alt + U.

Advice: practise step by step

There is no point in learning everything at once. Start with:

  • Ctrl + O (save)
  • Ctrl + X (exit)
  • Ctrl + W (search)
  • Ctrl + K / U (cut / paste)

Then add the others as you need them.

How do you configure and customise nano?

nano is a minimalist editor by default, but it can be widely customised to make it more comfortable. Whether on the command line or through a dedicated configuration file, nano adapts easily to your preferences.

Customising on the command line

Nano accepts several options at launch, which change its behaviour temporarily for a given session:

Terminal window
nano --linenumbers --softwrap my_file.txt
OptionDescription
--linenumbers (-l)Shows line numbers in the left margin
--softwrap (-S)Visually folds lines that are too long
--mouse (-m)Allows using the mouse to move around
--ignorercfiles (-I)Ignores the .nanorc configuration files
--view (-v)Opens read-only, writing is blocked

These options are very useful for one-off use, without changing the global configuration.

Configuration file: ~/.nanorc

To customise nano permanently, you can create or edit the ~/.nanorc file. It lets you define your personal preferences, which are loaded every time nano starts.

Example configuration

Each line of the file takes the form set <option>, where <option> has the same name as the long command-line option, without the two dashes. An unknown option name is not silently ignored: nano shows [ Mistakes in '/home/you/.nanorc' ] at startup, which gives an immediate signal in case of a typo. Here is a sound base for editing system configuration files:

Terminal window
set linenumbers
set softwrap
set tabsize 4
set autoindent
set mouse
DirectiveEffect
set linenumbersShows line numbers
set softwrapWraps long lines automatically
set tabsize 4Sets the tab size to 4 spaces
set autoindentKeeps the indentation from one line to the next
set mouseEnables the mouse (click to move the cursor)

To apply the changes, save the .nanorc file and restart nano.

Loading global .nanorc files

Some systems have a global configuration file, usually located in /etc/nanorc. It lets system administrators define a default configuration for every user. You can read this file for inspiration.

Good practices

A ~/.nanorc that is too loaded backfires the day you have to troubleshoot a server: your reflexes no longer work, because the remote machine does not have your configuration. So keep a portable base and memorise the keyboard toggles (Alt + N, Alt + S) to get the same comfort everywhere, without depending on a missing file.

  • Keep a backup copy of your .nanorc in case of conflict.
  • Test your changes on a temporary file before generalising them.
  • Avoid overloading your configuration: stick to the essentials to keep the editor fast and clear.

Troubleshooting

A block with nano almost never comes from the editor itself: it comes from the permissions on the file or on its parent directory. The typical case is a system file opened without sudo. Read the status bar first, at the bottom of the screen: nano usually writes the reason for the refusal there, when you open the file or when you save it. The table below starts from that message and works back to the cause.

SymptomLikely causeFix
A File '...' is unwritable message on opening, or Error writing ...: Permission denied on saving, on a system file (/etc/hosts)You opened a file owned by root without sudoExit with Ctrl + X then N, and reopen with sudo nano /etc/hosts (or sudoedit /etc/hosts)
Ctrl + O does nothingThe file is open read-only (-v): nano blocks writingRead the status bar message, then relaunch with nano file without -v, or with sudo for a system file
Nano shows [ Directory '...' is not writable ] on opening (a message only issued when --locking is enabled), or Error writing ...: Permission denied when creating a new fileYour account cannot write to the file's parent directoryCheck with ls -ld /path/to/folder/, then relaunch with sudo nano or sudoedit. Do not change the permissions of a system directory with chmod to get around the refusal
Pasted text is indented like a staircaseAuto-indentation applies to pasted lines, on a terminal without bracketed pasteDisable indentation during the paste with Alt + I, then enable it again
Impossible to exit nanoThe file was changed and nano is waiting for a confirmationPress Ctrl + X, then N to exit without saving
Syntax highlighting does not workThe highlighting .nanorc files are not loadedAdd include "/usr/share/nano/*.nanorc" to ~/.nanorc

sudo nano or sudoedit?

Two commands fit to edit a system file. The command sudo nano /etc/hosts runs the whole of nano with root rights. The command sudoedit /etc/hosts works differently: it copies the file to a temporary file, opens that copy with your own account, then rewrites the original as administrator when you exit. The editor therefore never has root rights, which limits the damage of a wrong move.

The editor opened by sudoedit is the one named by the SUDO_EDITOR, VISUAL or EDITOR variables, or else the one that the sudo configuration names (the editor option of sudoers). To be sure to get nano, type SUDO_EDITOR=nano sudoedit /etc/hosts. In both cases, never make a system directory writable by everyone to avoid sudo.

Where does nano come from?

The nano editor was born out of a simple need: to offer a free, easy to use and more intuitive alternative to complex editors such as vi or emacs, while respecting the standards of free software.

Nano was first born in 1999 under the name TIP (TIP Isn't Pico), a play on words to say that it was not pico, but very similar.

  • pico was the text editor included in the Pine mail client, developed at the University of Washington.
  • The problem: pico was not free and could not be redistributed freely, which was an issue in the open source world.

To work around this problem, Chris Allegretta decided to create a free editor, under the GPL licence, offering the same simplicity as pico, with improvements.

The project was then renamed nano in 2000, to respect a UNIX rule: avoid two programs having the same name (tip already existed).

Today, nano is actively maintained by the GNU community and keeps receiving regular updates.

Key points

  • nano is installed by default on Debian 13 (important priority) and on Ubuntu (standard priority on Ubuntu 24.04 and Ubuntu 26.04), but not on a minimal AlmaLinux 10, whose core group only brings vim-minimal: install it with sudo dnf install nano, or troubleshoot with vi, the only editor guaranteed everywhere.
  • The essential shortcuts are Ctrl + O (save), Ctrl + X (exit), Ctrl + W (search) and Ctrl + K / Ctrl + U (cut/paste).
  • The ~/.nanorc file customises nano permanently (line numbers, indentation, tab size).
  • For a system file, open it with sudo nano /path/file or sudoedit /path/file. Never change the permissions of a system directory with chmod to avoid sudo.
  • nano is ideal for quick changes; for intensive development, move on to vi or vim.

Knowledge check

Check that the essentials of this guide stuck. The questions cover only what has just been explained here.

Knowledge check

Check what you have learned with this interactive quiz

6 questions
6 min
70% to pass

How it works

  • The timer starts when you click Start
  • Multiple choice, true/false and short answer questions
  • You can move between questions
  • Detailed results are shown at the end

Starts the quiz and the timer