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 --versioncommand 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 thecontrolkey and enter<chr>.M-<chr>: Press themeta,edit, orAltkey and enter<chr>.
The shortcut keys of some Nano editors may conflict with other software or system shortcut keys. For example, the
^Wshortcut key of Nano in Cloud Shell conflicts with a browser shortcut key. To resolve shortcut key conflicts, replace the conflicting shortcut key with theEsckey.Double-press the
Escand enter<chr>. This operation equals^<chr>.Press the
Esckey and enter<chr>. This operation equalsM-<chr>.
Common operations
Start and exit Nano
To start Nano, run the
nanocommand 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
nanocommand 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/fileNameIn Nano: Press the
^RandM-Fshortcut 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
^Sshortcut 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
^Xshortcut 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
Ykey.To exit Nano without saving the file, press the
Nkey.To cancel the exit operation, press the
^Cshortcut key.
Save as a file: To save the content in the buffer to a file in a specified directory, press the
^Oshortcut key and specify a file path.
Move the cursor
Move left: Press the
^Bshortcut key and press therightkey to move the cursor one character to the left.Move right: Press the
^Fshortcut key and press theleftkey to move the cursor one character to the right.Move up: Press the
^Pshortcut key and press theupkey to move the cursor one character to the top.Move down: Press the
^Nshortcut key and press thedownkey 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 theEnterkey to jump to the destination line.Page forward: Press the
^Yshortcut key to go to the previous page. The cursor is at the top of the page.Page backward: Press the
^Vshortcut key to go to the next page. The cursor is at the top of the page.
Edit texts
Delete a text: Press the
Backspacekey to delete a character to the left of the cursor. Press the^Dshortcut 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-Ashortcut 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-6shortcut 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
^Kshortcut 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
^Ushortcut 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^Ushortcut key once to paste all content.Search for content: Press the
^Wshortcut key, enter a keyword, and then press theEnterkey to search for content in a text. Then, Nano locates the first match.Replace content: Press the
^\or^Rshortcut key to replace all matching content.Align content: Press the
^Jshortcut key to format content.Undo: Press the
M-Ushortcut key to undo the previous operation.Redo: Press the
M-Eshortcut 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 linenumbersto the.nanorcfile, save the file, and then restart Nano.Automatically indent lines: Insert
set autoindentto the.nanorcfile, 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 ~/.nanorcMove the cursor to the beginning of the file and enter
set linenumbers.Press the
^Sshortcut key to save the file.Press the
^Xshortcut key to exit edit mode.Run the
nanocommand 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.