Nano Toolbox 0/12

Backend engineeringThe GNU nano editor12 modules, keys at hand

Nano
Toolbox

Every shortcut you need to edit config files and scripts on any server without leaving the terminal: save and quit, move fast, cut and paste, search and replace, then make nano your own with .nanorc. Press Run on any session to replay it.

GNU nano 7.2BashUbuntu 24.04
01

Open, save, quit

The three things to learn before anything else. Once you can get out of nano with your work saved, the rest is speed.

nano file^O^XFirst minute

nano shows its own key notation: ^ is Ctrl and M- is Alt. The two lines at the bottom of the screen list the most common shortcuts, so you are never stuck guessing.

Ctrl^O = Ctrl+OHold Ctrl and press the letter.
AltM-U = Alt+UOr press Esc, let go, then the letter.
open-save.keysNANO
nano notes.txt            # open, or create if missing
nano +42 app.js           # open at line 42
nano -v /etc/hosts        # view only, no edits
^O                        # write out: Enter keeps the name
^S                        # save straight away, no prompt
^X                        # exit; asks to save if changed
^G                        # help: every key, ^X closes it

^X with unsaved changes asks first. Press Y to save, N to throw the changes away, or ^C to go back to editing.

nano 7.2Ubuntu 24.04
# a recorded session, replayed when you press Run
nano notes.txt
# type two lines, then save
^O
File Name to Write: notes.txt
Enter
[ Wrote 2 lines ]
^X
cat notes.txt
rotate db password on friday
bump node to 22 on staging

Worth remembering

KeysDoes
^OSave, asking for the file name
^SSave without a prompt
^XExit, offering to save first
^GShow the help screen
^C at a promptCancel the prompt
02

Move around

Arrow keys work, but long config files need bigger jumps: by word, by page, to a line number, or to either end of the file.

^A^E^_M-\First minute

Go to line is the jump you will use most, because error messages name a line. Press ^_ (or ^/), type the number, and press Enter. Add a comma and a column to land on the exact character.

^A / ^E

Start and end of the line.

See the example
Line
^Y / ^V

One screen up or down.

See the example
Page
^_ 42

Go to line 42, or 42,7 for a column.

See the example
Jump
M-\ / M-/

First and last line of the file.

See the example
Ends
move.keysNANO
^A / ^E                   # start / end of the line
Ctrl+Left / Ctrl+Right   # word by word
^Y / ^V                   # page up / page down
M-\ / M-/                 # top / bottom of the file
^_  42                    # go to line 42 (also ^/)
M-]                       # matching bracket
^C                        # where am I?

Open at the error directly. nano +42 app.js starts on line 42, so you can skip the jump entirely.

nano 7.2Ubuntu 24.04
# a recorded session, replayed when you press Run
^_
Enter line number, column number: 42
# the cursor lands on line 42
^C
[ line 42/180 (23%), col 1/31 (3%), char 1022/5410 (18%) ]
M-/
# now on the last line of the file

Worth remembering

KeysMoves to
^A / ^EStart / end of line
Ctrl+Left / RightPrevious / next word
^Y / ^VPrevious / next page
M-\ / M-/First / last line
^_ or ^/A line and column you type
M-]The bracket that matches
03

Cut, copy, paste

nano cuts whole lines by default, which suits config files. Mark a region when you need less than a line.

^KM-6^UM-ADaily

Each ^K cuts the line the cursor is on. Press it several times in a row and the lines stack into one clipboard, so ^K ^K ^K then ^U moves a three line block.

^K

Cut the current line, or the marked text.

See the example
Cut
M-6

Copy the line or marked text without removing it.

See the example
Copy
^U

Paste what you cut or copied.

See the example
Paste
M-A

Start a mark, then move to select.

See the example
Select
cut-paste.keysNANO
^K                  # cut this line
^K ^K ^K            # cut three lines into one clipboard
^U                  # paste at the cursor
M-6                 # copy this line, keep it in place
M-A  then arrows     # mark a region; ^K or M-6 acts on it
M-T                 # cut from here to the end of the file

Moving the cursor starts a fresh clipboard. Cuts only stack while you press ^K back to back. Move away and the next cut replaces the clipboard.

nano 7.2Ubuntu 24.04
# a recorded session, replayed when you press Run
# cursor on the line: PORT=3000
^K ^K
# PORT and HOST are cut together
M-\
# jump to the top of the file
^U
# both lines are pasted above the first line

Worth remembering

KeysDoes
^KCut line or selection
M-6Copy line or selection
^UPaste
M-A or ^6Start or end a mark
Shift+arrowsSelect as you move
M-TCut to the end of the file
04

Search and replace

Find a setting, jump between matches, and change every occurrence in one pass, with or without regular expressions.

^WM-W^\Daily

While a search or replace prompt is open, toggles appear on the help lines: M-C for case sensitive, M-R for regular expressions and M-B to search backwards.

^W word

Find the next match.

See the example
Find
M-W / M-Q

Next or previous match of the last search.

See the example
Repeat
^\ old, new

Replace, asking at each match.

See the example
Replace
A

In replace: change all the rest at once.

See the example
All
search.keysNANO
^W  timeout          # find
M-W                 # next match
M-Q                 # previous match
^\                  # replace: pattern, then replacement
Y / N / A           # this one, skip, all the rest
M-R  (at a prompt)  # regular expression mode
M-C  (at a prompt)  # match case

Press Y a few times before A. Checking the first matches by hand catches a pattern that is wider than you meant, like 3000 inside 30001.

nano 7.2Ubuntu 24.04
# a recorded session, replayed when you press Run
^W
Search: PORT
# jumps to the first match
M-W
[ This is the only occurrence ]
^\
Search (to replace): 3000
Replace with: 8080
A
[ Replaced 2 occurrences ]

Worth remembering

At a promptToggles
M-CCase sensitive
M-RRegular expression
M-BSearch backwards
Up / DownEarlier searches from history
^CCancel
05

Undo and select

Take back a mistake, bring it back again, and select text the way you would in any other editor.

M-UM-EShiftDaily

Undo in nano is M-U, not Ctrl+Z, which suspends programs in a terminal. If you press Ctrl+Z by habit, type fg at the shell to get nano back.

UndoM-USteps back one edit at a time.
RedoM-ESteps forward again.
undo.keysNANO
M-U                 # undo
M-E                 # redo
Shift+arrows        # select while moving
M-A                 # start a mark, M-A again ends it
M-D                 # count words, lines and characters

Ctrl+Z is not undo here. It pauses nano and drops you at the shell. Your work is safe: fg brings it straight back.

nano 7.2Ubuntu 24.04
# a recorded session, replayed when you press Run
# delete a line by mistake
^K
M-U
# the line is back
^Z
[1]+  Stopped                 nano config.yml
fg
# nano returns with your edits intact
M-D
Words: 214  Lines: 31  Chars: 1388

Worth remembering

KeysDoes
M-UUndo
M-ERedo
Shift+arrowsSelect text
M-A or ^6Toggle a mark
M-DWord, line and character count
06

Indent and comment

Shift blocks left and right, comment them out, and keep indentation steady while you type.

M-}M-{M-3M-IDaily

Indent and comment work on the current line, or on every line of a marked region. M-3 picks the comment style from the file's syntax, so it writes # in YAML and // in JavaScript.

M-}

Indent the line or selection.

See the example
Right
M-{

Unindent it.

See the example
Left
M-3

Comment or uncomment.

See the example
Toggle
M-I

Turn auto indent on or off.

See the example
Typing
indent.keysNANO
M-A  then Down x3    # mark three lines
M-}                 # indent them one step
M-{                 # take it back
M-3                 # comment them out
M-I                 # auto indent on or off
^J                  # justify (rewrap) a paragraph

Turn auto indent off before pasting. Pasted code arrives already indented, so auto indent adds a step to every line and makes a staircase. M-I, paste, M-I.

nano 7.2Ubuntu 24.04
# a recorded session, replayed when you press Run
# three lines of nginx.conf marked with M-A
M-}
# each line moves one tab to the right
M-3
# each line now starts with #
M-3
# and the comments come off again

Worth remembering

KeysDoes
M-} / Tab on a markIndent
M-{ / Shift+Tab on a markUnindent
M-3Comment or uncomment
M-IToggle auto indent
^JJustify the paragraph
07

Display toggles

Flip the view to suit the file: line numbers for code, soft wrap for prose, visible whitespace for YAML and Makefiles.

M-NM-PM-SDaily

Toggles last for the session only. When you find a view you always want, make it permanent in .nanorc, which module 11 covers.

M-N

Line numbers in the margin.

See the example
Numbers
M-P

Show tabs and trailing spaces.

See the example
Whitespace
M-S

Soft wrap long lines on screen.

See the example
Wrap
M-X

Hide the help lines for more room.

See the example
Space
display.keysNANO
M-N                 # line numbers
M-P                 # show whitespace
M-S                 # soft wrap long lines
M-X                 # hide or show the help lines
M-C                 # keep the cursor position visible
M-Y                 # syntax colour on or off
M-O                 # type spaces when you press Tab

M-P settles tab arguments. A Makefile needs real tabs and YAML refuses them. Show whitespace and you can see which one you typed.

nano 7.2Ubuntu 24.04
# a recorded session, replayed when you press Run
nano Makefile
M-N
# numbers appear in the left margin
M-P
# tabs show as » and trailing spaces as ·
M-X
# the two help lines disappear

Worth remembering

KeysTogglesnanorc name
M-NLine numbersset linenumbers
M-PWhitespace displayset whitespace
M-SSoft wrappingset softwrap
M-CCursor positionset constantshow
M-OTabs to spacesset tabstospaces
M-XHelp linesset nohelp
08

Several files

Open more than one file at once, switch between them, pull one file into another, or save under a new name.

M->M-<^RDaily

Every file you open sits in its own buffer. The title bar shows which one you are in, and ^X closes only the current buffer, so you leave nano once the last one closes.

nano a b

Opens both, each in a buffer.

See the example
Open
M-> / M-<

Next and previous buffer.

See the example
Switch
^R file

Insert another file at the cursor.

See the example
Insert
^O new-name

Save a copy under another name.

See the example
Save as
buffers.keysNANO
nano server.js config.js   # two buffers
M->                        # next buffer (also M-.)
M-<                        # previous buffer (also M-,)
^R  header.txt             # insert a file here
^R  then ^T                # browse for the file
^O  server.backup.js       # save as a new name

^R then M-F opens a new buffer. Inside the Read File prompt, M-F switches between inserting into this file and opening it on its own.

nano 7.2Ubuntu 24.04
# a recorded session, replayed when you press Run
nano server.js config.js
M->
# the title bar now reads config.js
^R
File to insert [from ./]: header.txt
[ Read 6 lines ]
^X
# config.js closes and server.js is back

Worth remembering

KeysDoes
M-> or M-.Next buffer
M-< or M-,Previous buffer
^RInsert a file
^R then M-FOpen it as a new buffer
^T in a file promptBrowse the folders
09

Run commands

nano can run a shell command and drop its output into the file, or pipe your text through a tool and replace it with the result.

^T|sortspellPower user

Press ^T to open the Execute prompt. A plain command inserts its output at the cursor. Start the command with | and the marked text, or the whole buffer, goes through it instead.

^T date

Insert a command's output.

See the example
Insert
^T |sort

Sort the marked lines in place.

See the example
Pipe
^T ^S

Spell check, if spell or hunspell is installed.

See the example
Spell
^T ^Y

Run the linter set for this syntax.

See the example
Lint
execute.keysNANO
^T  date             # insert today's date
^T  ls *.conf        # insert a file list
M-A  ...  ^T  |sort  # sort the marked lines
^T  |jq .           # pretty print JSON in place
^T  ^S              # spell check
^T  ^Y              # linter

Mark first, then pipe. Without a mark, |sort sorts the whole file. M-U undoes it if that was not the plan.

nano 7.2Ubuntu 24.04
# a recorded session, replayed when you press Run
^T
Command to execute: date
Sat Oct  3 10:22:41 IST 2026
# the date is now in the file at the cursor
# mark four unsorted host names, then
^T
Command to execute: |sort
# the four lines are replaced in sorted order

Worth remembering

At the Execute promptDoes
cmdInsert the output of cmd
|cmdPipe the mark or buffer through cmd
^SSpell check
^YRun the linter
^ORun the formatter
^CCancel
10

Startup flags

Choose how nano behaves for one file, right on the command line: numbers, indents, tab width, mouse, backups.

-l-i-E-TPower user

Flags apply only to that launch. They are perfect for a one off, such as opening a Python file with four space indents on a server where you do not own the .nanorc.

-l

Line numbers.

See the example
View
-i -E -T 4

Auto indent, spaces for tabs, tab width 4.

See the example
Python
-m

Click to place the cursor, scroll with the wheel.

See the example
Mouse
-B

Keep file~ as a backup on save.

See the example
Safety
flags.shNANO
nano --version
nano -l app.js                # line numbers
nano -i -E -T 4 tool.py       # four space indents
nano -m README.md             # mouse support
nano -B nginx.conf            # keep a backup
nano -v /var/log/syslog       # view only
nano +42,7 app.js             # line 42, column 7

-v is the safe way to read config. View mode cannot change the file, so you can open anything under /etc without the risk of a stray keypress.

nano 7.2Ubuntu 24.04
# a recorded session, replayed when you press Run
nano --version | head -1
 GNU nano, version 7.2
nano -l -i -E -T 4 tool.py
# numbers on, Tab types four spaces, new lines keep their indent
nano -B nginx.conf
^S
ls nginx.conf*
nginx.conf  nginx.conf~

Worth remembering

FlagLong formDoes
-l--linenumbersLine numbers
-i--autoindentKeep indent on new lines
-E--tabstospacesSpaces for Tab
-T 4--tabsize=4Tab width
-m--mouseMouse support
-B--backupBackup on save
-v--viewRead only
11

Your .nanorc

Write your preferences once in ~/.nanorc and every nano you open starts the way you like it.

.nanorcsetincludePower user

Each line is set plus an option, the same names as the toggles and flags. Ubuntu already loads syntax colours from /usr/share/nano; the include line makes sure of it on other systems.

setup.shNANO
mkdir -p ~/.cache/nano/backups
nano ~/.nanorc
nano app.py                     # opens with your settings
nano -I app.py                  # ignore .nanorc this once

Keep it in your dotfiles repo. Copy one file to a new server and nano behaves the same everywhere.

nano 7.2Ubuntu 24.04
# a recorded session, replayed when you press Run
mkdir -p ~/.cache/nano/backups
nano ~/.nanorc
# paste the starter config, then ^S and ^X
nano app.py
# line numbers, a scroll indicator and four space tabs, every time

A starter ~/.nanorc

~/.nanorcNANORC
# ~/.nanorc
set linenumbers
set autoindent
set tabsize 4
set tabstospaces
set indicator
set mouse
set constantshow
set backup
set backupdir "~/.cache/nano/backups"
include "/usr/share/nano/*.nanorc"

Back up into one folder. backupdir keeps the ~ files out of your projects, and the folder must exist first.

Worth remembering

OptionDoes
set linenumbersNumber every line
set autoindentNew lines keep the indent
set tabsize 4Tab width
set tabstospacesSpaces instead of tabs
set indicatorA scroll bar on the right
set mouseMouse support
set backupWrite file~ on save
12

Everyday gotchas

Editing files you do not own, making nano the default for git and cron, and getting work back after a crash.

sudoeditEDITOR.saveProduction

Use sudoedit for system files. It copies the file, opens the copy in nano as you, with your own .nanorc, and writes it back as root only when you save.

gotchas.shNANO
sudoedit /etc/hosts                     # edit a root file safely
echo 'export EDITOR=nano' >> ~/.bashrc
git config --global core.editor nano
select-editor                           # Ubuntu: crontab's editor
ls *.save                               # emergency saves

nano saves your work when it is killed. If the terminal dies, nano writes file.save next to the file. Open it, check it, then rename it over the original.

nano 7.2Ubuntu 24.04
# a recorded session, replayed when you press Run
nano /etc/hosts
# edit, then ^S
[ Error writing /etc/hosts: Permission denied ]
# quit with ^X and N, then open it the right way
sudoedit /etc/hosts
git config --global core.editor nano && git config --get core.editor
nano
ls *.save
notes.txt.save

Worth remembering

ProblemFix
Alt keys do nothing on macOSPress Esc, then the key
Permission denied on saveReopen with sudoedit
Pasted code becomes a staircaseM-I off before pasting
Long lines run off screenM-S soft wrap
Pressed Ctrl+ZType fg at the shell
Terminal crashedLook for file.save

Which one do I need?

Find the job, press the keys. Every shortcut here is covered in a module above, and ^G inside nano shows the full list.

I want toRun
Save^S (or ^O to choose the name)
Quit^X
See every shortcut^G
Go to line 42^_ 42
Top / bottom of fileM-\ / M-/
Start / end of line^A / ^E
Cut a line^K
Copy a lineM-6
Paste^U
Select textM-A or Shift+arrows
Find^W, then M-W for next
Find and replace^\
Undo / redoM-U / M-E
Indent / unindentM-} / M-{
Comment outM-3
Line numbersM-N
Show whitespaceM-P
Soft wrapM-S
Next fileM->
Insert a file^R
Insert a command's output^T
Word countM-D
Edit a root filesudoedit /path
Make it permanent~/.nanorc