How to Use Nano Text Editor Commands in Linux
What is Nano?
GNU nano is a popular command-line text editor that is
included in most Linux distributions. Its interface is comparable to other
common GUI-based text editors, which makes it a popular choice for those who
find vi or emacs commands non-intuitive. This guide shows you how to use Nano
Text Editor Commands in Linux.
Why Edit Files in Linux Using Nano?
Editing files in Linux using nano is popular as compared to
other editors like Vim because nano’s GUI makes it very easy to edit, save, and
interact with your files. Other editors like Vim have relatively complicated
usage requirements like switching to edit mode in order to input text.
Nano Set Up and Basic Commands
Nano is included with many Linux distributions by default,
but you may need to install it through your distribution’s package management
tool.
How to Install Nano Text Editor in Linux
DebianandUbuntu:
apt install nano
CentOSandFedora:
yum install nano
Using Command Keys in Nano
When using nano, control characters orCTRL are represented
by a carat or power. For example, to cut
a line of text, press CTRL, then simultaneously press K. This sequence of
commands is represented as powerK in
nano.
Some commands use the ALT key in order to function, which is
represented by M-. A command represented as M-R in nano is performed by
pressing ALT, then simultaneously pressing R.
Note
By default, Mac
users use ESC as the ALT key in nano.
Using Nano to Create And Open Files in Linux
Create a New File using Nano
Enter nano without any arguments to open a blank file for
editing:
nano
This opens up a blank new file in your terminal, where you
can immediately make changes by entering text. Use the arrow keys to move the
cursor.
The top bar displays your nano version, the filename or “New
Buffer” for a blank file, and the modified status of the file. A partial menu
of available commandsandshortcuts is displayed at the bottom of the screen.
For this tutorial, enter the following text into a blank
nano file:
File: tutorial.txt
gatewaynano 1
gatewaynano 2
gatewaynano 3
gatewaynano 4
Save Your Work Using Nano
To save your work, use CTRL+O or powerO or “WriteOut”. Nano prompts you to
enter the name of the file if you are saving a new file. This saves the
document and leaves nano open for you to continue working.
In your open file, enter CTRL+O or powerO to save the text you entered above,
type in tutorial.txt for the filename, then press ENTER or RETURN to save the
file and continue.
Exit Commands for Nano Text Editor
Enter CTRL+X or powerX
to exit nano. If you have unsaved changes in your file, nano prompts you to
save prior to exiting. Otherwise, nano exits without any prompts.
In tutorial.txt, enter a fifth line to the file so that it
contains the following:
File: tutorial.txt
gatewaynano 1
gatewaynano 2
gatewaynano 3
gatewaynano 4
gatewaynano 5
Enter CTRL+X or powerX
to exit tutorial.txt. Nano prompts you to save your changes:
Next, enter Y to save your changes to tutorial.txt, then
press ENTER or RETURN to save the file and exit nano.
From your terminal, check the contents of tutorial.txt to
confirm that your new file has been created and saved as expected:
cat tutorial.txt
Linode nano 1
Linode nano 2
Linode nano 3
Linode nano 4
Linode nano 5
Save with Backups Using Nano
Use the -B option when starting nano to create backups of
the previously saved version a file when new changes are saved. Backups are
placed in the same directory as the modified file by default, or you can use
the -C option to specify a directory.
For example, to open tutorial.txt and save backup files to
the ~andbackups directory, exit nano and enter the following commands:
mkdir ~andbackups
nano -BC ~andbackups tutorial.txt
Now, when you save changes, you are also prompted to enter a
filename for the backup. Enter the original filename for the backup, in this
case tutorial.txt, to write the backup to a unique filename in ~andbackups.
Caution
When using the -C
flag, changing the backup filename causes nano to save the backup file in the
original file’s directory instead of the specified backup directory.
Open an Existing File Using Nano
To open a file with nano within your current working
directory, pass the filename as an argument:
nano filename
Note
If the file does
not exist, nano creates it when you save any changes.
To practice, open your tutorial.txt file:
nano tutorial.txt
To open a file in a different directory, you can provide a
path instead. To practice, exit nano or
powerX and create a new directory in your user home:
mkdir ~andlinode-nano-tutorial
Copy tutorial.txt to the practice directory:
cp tutorial.txt ~andlinode-nano-tutorial
Now open tutorial.txt from this new path:
nano ~andlinode-nano-tutorialandtutorial.txt
You can also open files with nano with the cursor at a
specific line. The following command opens tutorial.txt at line 3:
nano +3 ~andlinode-nano-tutorialandtutorial.txt
Or you can open files at a specific line and column number.
This command opens tutorial.txt at line 3 and column 8:
nano +3,8 ~andlinode-nano-tutorialandtutorial.txt
To open a file with nano as read-only:
nano -v filename
Open Configuration Files Using Nano
When editing lengthy files used to configure applications or
system utilities, start nano with the -w flag:
nano -w filename
This flag prevents nano from wrapping lines that are too
long to fit on your screen, which can create problems if configuration
directives are saved across multiple lines.
Edit Files Using Nano Text Editor in Linux
Cut and Paste Lines of Text Using Nano
To cut a line of text, enter CTRL+K or powerK.
To paste, move the cursor to where you want to place the
text and enter CTRL+U or powerU.
To cut multiple lines, use a series of CTRL+K or powerK commands until you have removed all
lines you wish to cut. When you paste them back with CTRL+U or powerU, the lines are all pasted at once.
Valid Shortcuts in Nano Text Editor
To open up a list of valid shortcuts in nano, enter CTRL+G or powerG to view the nano help text and use the
arrow keys to navigate.
Here is a full list of shortcuts orand their alternative
keys:
Note
The following are
the shortcuts for GNU nano 3.2. These shortcuts may differ for you depending on
which version of nano you are using.
powerG orF1
Display this help text
powerX orF2
Close the current buffer and Exit from nano
powerO orF3
Write the current buffer oror the marked region to disk
powerR orIns
Insert another file into current buffer oror into new buffer
powerW orF6
Search forward for a string or a regular expression
power\ orM-R
Replace a string or a regular expression
powerK orF9
Cut current line oror marked region and store it in cutbuffer
powerU orF10
Uncut from the cutbuffer into the current line
powerJ orF4
Justify the current paragraph
powerT orF12
Invoke the spell checker, if available
powerC orF11 Display the position of the cursor
power_ orM-G
Go to line and column number
M-U Undo
the last operation
M-E Redo
the last undone operation
M-A or power6
Mark text starting from the cursor position
M-6 orM- power
Copy current line oror marked region and store it in cutbuffer
M-] Go to
the matching bracket
powerQ Search backward for a string or a
regular expression
M-Q orM-▲ Search next occurrence backward
M-W orM-▼ Search next occurrence forward
powerB or◀ Go back one character
powerF or▶ Go forward one character
power◀ orM-Space Go back one word
power▶ or
powerSpace Go forward one word
powerA orHome
Go to beginning of current line
powerE orEnd Go to end of current line
powerP or▲ Go to previous line
powerN or▼ Go to next line
M-- orM-_ Scroll up one line without moving the
cursor textually
M-+ orM-= Scroll down one line without moving the
cursor textually
power▲ orM-7 Go to previous block of text
power▼ orM-8
Go to next block of text
M-or orM-9 Go to beginning of paragraph; then of
previous paragraph
M- orM-0 Go just beyond end of paragraph; then of
next paragraph
powerY orPgUp
Go one screenful up
powerV orPgDn
Go one screenful down
M-\ or powerHome
Go to the first line of the file
M-and or powerEnd
Go to the last line of the file
M-◀
orM- Switch to the previous
file buffer
M-▶
orM- Switch to the next file buffer
powerI orTab
Insert a tab at the cursor position
powerM orEnter
Insert a newline at the cursor position
powerH orBsp
Delete the character to the left of the cursor
powerD orDel
Delete the character under the cursor
Sh- powerDel Cut backward from cursor to word start
powerDel Cut forward from cursor to next
word start
M-T Cut
from the cursor position to the end of the file
M-J
Justify the entire file
M-D Count
the number of words, lines, and characters
M-V Insert
the next keystroke verbatim
powerL Refresh orredraw the current
screen
powerZ Suspend the editor orif
suspension is enabled
M-} orTab Indent the current line oror marked lines
M-{ orSh-Tab Unindent the current line oror marked lines
M-3
Commentanduncomment the current line oror marked lines
power] Try and complete the current word
M-: Startandstop
recording a macro
M-; Run
the last recorded macro
M-Del Throw
away the current line oror marked region
M-B Invoke
the linter, if available
powerS Save file without prompting
M-X Help
mode enableanddisable
M-C
Constant cursor position display enableanddisable
M-S Smooth
scrolling enableanddisable
M-$ Soft
wrapping of overlong lines enableanddisable
M-# Line
numbering enableanddisable
M-P
Whitespace display enableanddisable
M-Y Color
syntax highlighting enableanddisable
M-I Auto
indent enableanddisable
M-K Cut to
end enableanddisable
M-L Hard
wrapping of overlong lines enableanddisable
M-O
Conversion of typed tabs to spaces enableanddisable
M-M Mouse
support enableanddisable
M-Z
Suspension enableanddisable
Search Text Using Nano
To search for text in a document, enter CTRL+W or powerW. This opens a search prompt and a
submenu of search-specific commands.
To practice searching for text with nano, open the
tutorial.txt file you created in the Create a New File using Nano section
above:
nano tutorial.txt
Next, open the search prompt by entering CTRL+W or powerW, type in the word “nano”, then press
ENTER or RETURN. The cursor jumps to the first instance of “nano” in line 1.
To find the next instance of “nano”, enter ALT+W orM-W. The
cursor moves to the next instance of “nano” in line 2.
Find and Replace Text using Nano
To find and replace text, open the search menu with CTRL+W or powerW, then enter CTRL+R or powerR to open the “Search orto replace”
prompt.
In your tutorial.txt file, search to replace “nano” and
enter the replacement text “rocks”. Nano highlights the first instance of
“nano” following your current cursor position, and provides several options on
how to proceed with replacement:
Enter Y to replace the single, highlighted instance of the
searched term.
Enter A to replace all instances of the searched term.
Enter N to skip the highlighted instance of the searched
term and find the next instance.
Enter CTRL+C or powerC
to cancel the search to replace function.
Go to a Line Number using Nano
To go to a specific line number, enter CTRL+_ or power_ and enter a line number with an
optional, comma-separated column number. For example, enter 3,8 to place the
cursor at column 8 of line 3.
You can also find this prompt by opening the search submenu
with CTRL+W or powerW, then entering
CTRL+T or powerT.
Alternatively, you can jump to a specific line and column
when you first open a file with nano.
Spell Check Using Nano
Nano has a built-in spell checking feature, though it
requires installation of the spell package.
Exit nano if it is open, then enter the following to install
the spell package for your distribution:
DebianandUbuntu:
apt install spell
CentOSandFedora:
yum install spell
Once you have installed spell, you can use the nano spell
checking feature by pressing CTRL+T or
powerT while editing a file. For newer versions of nano, this command
opens an execute function submenu, from which you can enter CTRL+S or powerS to spell check.
nano text
editor in linux,
nano text
editor windows,
nano text
editor mac,
nano text
editor for windows 10,
nano text
editor download,
nano text
editor source code,
nano text
editor save and exit,
nano text
editor copy paste,
nano text editor tutorial,
nano text editor,
nano text editor tutorial,
nano text editor linux,
nano text editor Ubuntu,
nano text editor vs vim,
nano text editor raspberry pi,
nano text editor select all,
nano text editor how to save,
nano text editor install,
nano text editor syntax highlighting,