nano Editor in Linux: How to Open, Edit, and Save a File

Featured banner showing an editor buffer with a cursor caret flowing through a write arrow into a saved file symbol

Nano opens from the terminal, and the two rows of caret keys along its bottom are the only manual it ships with. The caret means Ctrl, the M- prefix means Alt, and the rest of what you need stays on screen while you type.

I ran each keystroke here in nano 7.2, the build Ubuntu 24.04 LTS installs, and captured the status lines rather than retyping them. The bindings have shifted between releases, which is the trap when a walkthrough from an older year does not match the editor in front of you.

What nano is, and how its screen is laid out

Every file you open in nano gets the same four regions, and each one answers a question you will ask at least once. Nothing sits behind a menu.

The title line names the version and the file, with a star that appears once the buffer differs from what is on disk.

RegionWhat it showsWhat it changes for you
Title lineVersion, filename, and a star for unsaved changesThe star is your warning that the edit is not written yet
BufferThe file contents, one screenful at a timeWhere you type, with no mode to enter
Status linePrompts, confirmations, and failuresIt reports a write count or names a write failure
Bottom two rowsThe shortcut list for this buildThe caret keys you will actually press

That bar is sized to your window, so a narrow terminal prints five keys per row and a wide one prints a dozen.

nano 7.2 open on a new file with the shortcut bar along the bottom two rows
A new buffer in nano 7.2. The bottom two rows are the shortcut list, not decoration.

I opened a new buffer to capture that state, so the status line reports an empty file rather than a line count.

Nano is one of several editors that work without leaving the shell, and editing files from the terminal covers the field. If you are weighing it against the alternatives, nano against vim puts the two side by side, and the vim tutorial covers the other one in depth.

Install nano, or find out which version you already have

Nano ships with the base install on nearly every distribution, so the first useful command reports the build instead of changing it.

nano --version
Terminal output of nano --version showing GNU nano version 7.2
The version line tells you which shortcut bar you are looking at.

Debian and Ubuntu carry it as the package nano, and installing over an existing copy reports that the newest version is already in place.

sudo apt install nano

The number in that first line decides which keys work. Replace sits on Ctrl+backslash in 7.2 and on Alt+R in the cheatsheet for the current release, and the spell checker moved from F12 to a Ctrl then T sequence.

The keystrokes and status lines in this reference match that build. A rolling distribution or a source build can be a major version ahead, and the version line is the fastest way to find out.

Package names and installers differ across families, and package management on Linux collects the apt, dnf, and pacman forms.

Open a file, change a line, save it, and get back to the shell

The sequence is three keystrokes long, and the one in the middle is a confirmation that new users skip. Passing a filename opens that file, and leaving the filename out starts an empty buffer you can name at the first save.

Open or create the file

nano notes.txt

A line number on the command opens the same file with the cursor already parked there.

nano +3 notes.txt

The title line shows what you opened, which is how you confirm at a glance that the edit is landing in the intended file.

Write the change, then leave

Ctrl+O writes the buffer. Nano answers by asking where to write and prefilling the name you opened with, so the next key is Enter.

nano showing the File Name to Write prompt after Ctrl+O
Ctrl+O asks where to write, and the name already filled in is the one to accept.

Skipping that Enter is where edits are lost. Ctrl+O followed straight by Ctrl+X leaves the buffer unwritten, and the exit prompt asks about saving all over again.

Ctrl+X closes the buffer, and when changes are pending it asks first, so N discards them and returns you to the shell.

nano asking Save modified buffer with the choices Y Yes, N No, and Ctrl+C Cancel
The exit prompt names all three answers, so there is no guessing after Ctrl+X.

Ctrl+S writes with no prompt at all, and the status line reports the line count when the write finishes.

nano status line reporting Wrote 4 lines after Ctrl+S
Ctrl+S writes without asking, and the status line confirms how many lines went to disk.

Reading the file back is the plainest confirmation that the write landed, and cat prints it without an editor.

cat notes.txt
cat printing the saved notes.txt file back in the terminal
cat reads the file back, which is the cheapest way to confirm the write landed.

The cat and more commands cover the reading side, including the pager for files longer than a screen.

What you wantKeysWhat you see
Write and keep editingCtrl+O, then EnterThe status line reports the lines written
Write with no promptCtrl+SThe same confirmation, with no filename question
Leave the editorCtrl+XThe shell prompt
Leave and discard the editCtrl+X, then NThe shell prompt, file untouched
Start at a known linenano +12 fileThe cursor sitting on line 12

The shortcuts worth memorising, and the bar that lists them

In practice the bar names the keys that fit your window, which is why it reads as noise until the notation is decoded. Control-key sequences carry a caret, and the M- prefix means Alt, or Esc on a keyboard without one.

Press Ctrl+G and nano prints the full keystroke list for the build you are running, including everything the bar had no room for. The keys worth keeping in your head are grouped by job below.

nano help screen listing the keystrokes of the installed build
Ctrl+G prints the keystroke list for the build in front of you.
JobKeysWhat happens
Write the bufferCtrl+O, then EnterPrompts for a name, prefilled with the open file
Write with no promptCtrl+SThe status line reports the line count
Leave the editorCtrl+XAsks about unsaved changes first
Read the keystroke listCtrl+GFull list for the running build
Cut the current lineCtrl+KThe line goes into the cutbuffer
Paste the cutbufferCtrl+UInserts at the cursor
Copy instead of cuttingAlt+6Keeps the line where it is
Mark a regionAlt+A, then moveThe marked text is highlighted
Undo the last actionAlt+USteps back one edit
Redo the last undone actionAlt+ESteps forward again
Find textCtrl+WPrompt opens on the status line
Replace textCtrl+backslashAsks about each match, Alt+R on newer builds
Go to a lineCtrl+/ or Alt+GAccepts a line, and a column after a comma
Show the cursor positionCtrl+CLine, column, and percentage through the file
Count lines and wordsAlt+DCounts the buffer or the marked region
Toggle line numbersAlt+NNumbers appear in the left margin
Toggle soft wrapAlt+SLong lines stay on one row
Insert another fileCtrl+RAsks for a path and inserts at the cursor
Run a shell commandCtrl+TThe command output lands in the buffer
Spell check the bufferF12Needs hunspell or spell installed

A second cut adds to the cutbuffer instead of replacing what is already there, so the text survives until the next cut or copy.

nano buffer after cutting the first line and pasting it at the end with Ctrl+K and Ctrl+U
Ctrl+K cut the first line, and Ctrl+U pasted it at the end of the buffer.

Alt+U steps back one action and Alt+E steps forward again, which undoes an accidental cut without closing the file.

Search, replace, and jump to a line

Finding text and replacing it use different keys, and both prompts appear on the status line rather than in a window. The search prompt takes a term, and the replacing session takes a term and its replacement.

Find a string

Ctrl+W opens the search prompt, and Enter accepts the term.

nano search prompt with the term config typed after Ctrl+W
Ctrl+W opens the search prompt on the status line.

I searched the same file for config, and Ctrl+C reported line 3 and column 15. That position line is the fastest way to confirm a match.

nano status line reporting the cursor position after Ctrl+W found the search term on line 3
Ctrl+C after a search reports the line and column the match sits on.

Replace one instance or all of them

Ctrl+backslash starts a replacing session: the term, the replacement, then a prompt for each match. Recent releases list Alt+R for the same job.

That prompt takes Y for the current match, N to skip it, and A to replace the rest of the file.

nano buffer after replacing every occurrence of Week with Step, status line reading Wrote 4 lines
Replace-all rewrote every line, and the write to disk is reported on the status line.

Jump to a line

A line number on the command opens a file at that row, and a search tool finds the number when you do not know it.

grep -n config notes.txt

The number in front of the match is the one nano +3 expects, so the two commands work as a pair.

Make nano behave: line numbers, soft wrap, colors, and the spell checker

The options you toggle by hand live in a configuration file you write once, and the toggles keep working for a single session.

A nanorc with the settings you keep reaching for

Your own file sits at ~/.nanorc, and /etc/nanorc supplies the defaults for every user on the machine. Each line enables one option.

set linenumbers
set softwrap
set tabsize 4
set autoindent
set mouse
set backup
SettingWhat it changes
set linenumbersNumbers every row in the left margin
set softwrapWraps long lines instead of scrolling sideways
set tabsize 4Sets the indent width for the Tab key
set autoindentStarts a new line at the current indent
set mouseLets a click place the cursor when the terminal reports it
set backupKeeps the previous version of a file on write

Line numbers, soft wrap, and syntax colors also toggle for the current session on Alt+N, Alt+S, and Alt+Y, so you can try a setting before writing it down.

I ran nano with that file on the command line and the margin appeared on the first row.

nano started with a nanorc showing line numbers and soft wrap
The settings in a nanorc turn on line numbers and soft wrap for every file.

A different file can be read instead of your own, which is how a project ships its conventions: nano –rcfile=./my.nanorc notes.txt. The option takes a full path, so that file can live in a repository beside the code it configures.

A spell checker that starts

F12 runs the checker, and nano calls hunspell or spell underneath, which means the key does nothing on a system without either one.

sudo apt install hunspell hunspell-en-us

I installed hunspell and pressed F12 on a line with a misspelled word. Nano highlighted the word and asked for a replacement with a suggestion filled in.

nano spell checker asking for a replacement word
The spell checker only runs when a checker such as hunspell is installed. F12 starts it.

The checker walks the buffer word by word, and every misspelled word gets the same prompt until the pass ends. Cancel the pass with Ctrl+C when you want to stop partway.

Shell configuration follows the same idea, and the bashrc and bash profile files are the place to look when a setting will not stick.

When nano will not save your file

Each failure that costs work announces itself before the text is gone. Opening a root-owned file works because reading is permitted, and the write is where it stops.

A file your account cannot write

nano reporting Error writing locked.txt: Permission denied in the status line
A file the user cannot write fails on the write, and the buffer stays open.

The status line reports Error writing locked.txt: Permission denied and the buffer stays open, so nothing you typed is lost yet.

There are two repairs: write the file with elevated rights, as in sudo nano /etc/hosts, or take ownership of it. The chmod and chown commands cover the first, and user and group permissions covers the model behind both.

The editor died with unsaved changes

When nano is killed by a hangup or a termination signal, it writes the buffer beside the original file with .save appended and prints where it went. In a session I ended with SIGTERM, nano reported the dump and left a save file holding the unwritten line.

Open that save file, then move the text back into place. It sits beside the original with .save appended, so nothing typed before the kill is lost.

A shortcut from an older tutorial does nothing

Keys moved between releases, and a walkthrough written for nano 4.8 can describe a bar your build no longer prints. The build in front of you answers for itself, since Ctrl+G lists its keys and the version line names it.

nano --version

Read the bar once, and the next release cannot surprise you

The two rows at the bottom are printed by the build in front of you, so they already account for every rename and every added key in your installation.

The habit is small: read those rows before reaching for a chart, and press Ctrl+G when a key you expected is missing. What the bar cannot carry between sessions is the shortlist of settings you keep toggling, so write that down once.

printf 'set linenumbers\nset softwrap\n' >> ~/.nanorc

Every file then opens with a numbered margin and wrapped lines, and the save key keeps working the way it did on the first file.

Frequently asked questions

How do you save and exit nano?

Press Ctrl+O, press Enter to accept the filename nano fills in, then press Ctrl+X. Ctrl+O writes the buffer and Ctrl+X closes it.

How do you exit nano without saving?

Press Ctrl+X, then N when nano asks about the modified buffer. The shell comes back and the file on disk keeps its previous contents.

What does the ^ symbol mean in nano?

It stands for the Ctrl key, so ^X means Ctrl+X. The M- prefix in the same bar stands for Alt, or Esc on a keyboard without an Alt key.

Why does a shortcut from a tutorial do nothing in my nano?

Bindings moved between releases. Press Ctrl+G to read the list of the build you are running, and the version line at the top names that build.

Does nano need hunspell for its spell checker?

Yes. The spell checker runs an external program, and nano calls hunspell or spell, so install one of them before pressing F12.

Where are nano’s settings stored?

In ~/.nanorc for your user, and in /etc/nanorc for the whole machine. The –rcfile option reads a different file instead.