Nano is an easy-to-use text editor that supports simple and visualized operations. This topic describes some common operations and shortcut keys of Nano.
Installation of Nano
In the following example, Nano 2.9.3 is used. Operations and shortcut keys may vary based on the Nano version.
The Nano editor is pre-installed in most Linux distributions. You can query the Nano version by executing the
nano --version
command on the terminal. If Nano is not installed on your system, manually install it. For more information, see The GNU nano editor FAQ.Cloud Shell is a web command-line interface (CLI) provided by Alibaba Cloud. You can use the pre-installed Nano editor to edit texts in the Cloud Shell. For more information, see What is Cloud Shell?
Precautions
Sample shortcut keys:
^<chr>
: Press thecontrol
key and enter<chr>
.M-<chr>
: Press themeta
,edit
, orAlt
key and enter<chr>
.
The shortcut keys of some Nano editors may conflict with other software or system shortcut keys. For example, the
^W
shortcut key of Nano in Cloud Shell conflicts with a browser shortcut key. To resolve shortcut key conflicts, replace the conflicting shortcut key with theEsc
key.Double-press the
Esc
and enter<chr>
. This operation equals^<chr>
.Press the
Esc
key and enter<chr>
. This operation equalsM-<chr>
.
Common operations
Start and exit Nano
To start Nano, run the
nano
command in Cloud Shell. Then, Nano automatically creates an empty text file. To save the file, you must specify a filename and a directory.To exit Nano, press the shortcut key
^X
.
Manage files
Open or create a file:
Outside Nano: Run the
nano
command and specify a file directory to open a file in Nano. If the file does not exist, Emacs automatically creates the file in the specified directory. Sample commands:nano fileName nano /etc/fileName
In Nano: Press the
^R
andM-F
shortcut keys to create a buffer in Nano and open a file in the buffer. If the file does not exist, Emacs automatically creates the file in the specified directory.
Save a file:
Press the
^S
shortcut key to save the content in the buffer to the file. If no file is specified for the buffer, you must specify a filename and a file directory.If you press the
^X
shortcut key to exit Nano but the file in the buffer is not saved, Nano asks you whether you want to save the file.To save the file, press the
Y
key.To exit Nano without saving the file, press the
N
key.To cancel the exit operation, press the
^C
shortcut key.
Save as a file: To save the content in the buffer to a file in a specified directory, press the
^O
shortcut key and specify a file path.
Move the cursor
Move left: Press the
^B
shortcut key and press theright
key to move the cursor one character to the left.Move right: Press the
^F
shortcut key and press theleft
key to move the cursor one character to the right.Move up: Press the
^P
shortcut key and press theup
key to move the cursor one character to the top.Move down: Press the
^N
shortcut key and press thedown
key to move the cursor one character to the bottom.Jump lines: Press the
^_
shortcut key, enter the number of the destination line, and then press theEnter
key to jump to the destination line.Page forward: Press the
^Y
shortcut key to go to the previous page. The cursor is at the top of the page.Page backward: Press the
^V
shortcut key to go to the next page. The cursor is at the top of the page.
Edit texts
Delete a text: Press the
Backspace
key to delete a character to the left of the cursor. Press the^D
shortcut key to delete a character to the right of the cursor.Select a text: Move the cursor to the beginning of the text that you want to select, press the
M-A
shortcut key to enter selection mode, and then move the cursor to select the text. The selected text is highlighted.Copy a text: Press the
M-6
shortcut key to copy a selected text. If no text is selected, the text in the line of the cursor is copied to the clipboard.Cut a text: Press the
^K
shortcut key to cut a selected text. If no text is selected, the text in the line of the cursor is cut and pasted to the clipboard.Paste a text: Press the
^U
shortcut key to paste a text from the clipboard to the location of the cursor. If the content is consecutively cut from the same text, you only need to press the^U
shortcut key once to paste all content.Search for content: Press the
^W
shortcut key, enter a keyword, and then press theEnter
key to search for content in a text. Then, Nano locates the first match.Replace content: Press the
^\
or^R
shortcut key to replace all matching content.Align content: Press the
^J
shortcut key to format content.Undo: Press the
M-U
shortcut key to undo the previous operation.Redo: Press the
M-E
shortcut key to redo the undone operation.
Modify configurations
You can modify the .nanorc
file to customize Nano configurations. The file is typically in the $HOME
directory. You can configure Nano to display line numbers and automatically indents lines by modifying Nano configurations.
Display line numbers: Insert
set linenumbers
to the.nanorc
file, save the file, and then restart Nano.Automatically indent lines: Insert
set autoindent
to the.nanorc
file, save the file, and then restart Nano.For more information about Nano configurations, see NANORC.
Examples
Modify the Nano configuration file
To configure Nano to display line numbers, use Nano to modify the .nanorc
file. Insert set linenumbers
to the first line of the file. Procedure:
Run the following command to create or open the configuration file:
nano ~/.nanorc
Move the cursor to the beginning of the file and enter
set linenumbers
.Press the
^S
shortcut key to save the file.Press the
^X
shortcut key to exit edit mode.Run the
nano
command to open Nano. The expected result is that line numbers are displayed on the left side of the editor, as shown in the following figure.