Nano is a lightweight, intuitive text editor. Learn common operations and shortcut keys for Nano in Cloud Shell.
Install Nano
-
This guide uses Nano 2.9.3. Operations and shortcut keys may differ in other versions.
-
Nano is pre-installed in most Linux distributions. Run
nano --versionto verify the installation. If Nano is not installed, install it manually following the GNU nano editor FAQ. -
Cloud Shell is a web CLI provided by Alibaba Cloud. You can use the pre-installed Nano editor in Cloud Shell. What is Cloud Shell?
Shortcut key notation
-
Shortcut key notation:
-
^<chr>: Holdcontroland press<chr>. -
M-<chr>: Holdmeta,edit, orAltand press<chr>.
-
-
Some Nano shortcuts conflict with browser shortcuts in Cloud Shell (for example,
^W). Use theEsckey as a substitute:-
Double-press
Esc, then enter<chr>. Equivalent to^<chr>. -
Press
Esconce, then enter<chr>. Equivalent toM-<chr>.
-
Common operations
Start and exit Nano
-
Run
nanoin Cloud Shell to open a new empty file. You must specify a filename when saving. -
Press
^Xto exit Nano.
Manage files
-
Open or create a file:
-
From the shell: Run
nanowith a file path. If the file does not exist, Nano creates it. Examples:nano fileName nano /etc/fileName -
Inside Nano: Press
^RthenM-Fto open a file in a new buffer. If the file does not exist, Nano creates it.
-
-
Save a file:
-
Press
^Sto save. If no filename is set, Nano prompts you to specify one. -
If you press
^Xwith unsaved changes, Nano prompts you to save:-
Press
Yto save. -
Press
Nto discard changes. -
Press
^Cto cancel and return to the editor.
-
-
-
Save as: Press
^Oand specify a file path to save the buffer content to a different location.
Cursor movement
-
Move left:
^Bor therightkey. -
Move right:
^For theleftkey. -
Move up:
^Por theupkey. -
Move down:
^Nor thedownkey. -
Go to line: Press
^_, enter the line number, and pressEnter. -
Page up: Press
^Y. The cursor moves to the top of the page. -
Page down: Press
^V. The cursor moves to the top of the page.
Edit text
-
Delete: Press
Backspaceto delete the character left of the cursor, or^Dto delete the character right of the cursor. -
Select: Move the cursor to the start of the selection, press
M-Ato enter selection mode, then move the cursor to highlight text. -
Copy: Press
M-6to copy selected text. If no text is selected, copies the current line. -
Cut: Press
^Kto cut selected text. If no text is selected, cuts the current line. -
Paste: Press
^Uto paste from the clipboard. Consecutively cut lines paste as a single block. -
Search: Press
^W, enter a keyword, and pressEnterto find the first match. -
Replace: Press
^\or^Rto find and replace text. -
Justify: Press
^Jto reformat the current paragraph. -
Undo: Press
M-U. -
Redo: Press
M-E.
Configure Nano
Customize Nano by editing the .nanorc file in your $HOME directory. Common configurations:
-
Display line numbers: Add
set linenumbersto.nanorc, then restart Nano. -
Auto-indent: Add
set autoindentto.nanorc, then restart Nano. -
All available options are documented in the NANORC reference.
Examples
Enable line numbers
Add set linenumbers to the first line of .nanorc:
-
Open the configuration file:
nano ~/.nanorc -
At the top of the file, type
set linenumbers. -
Press
^Sto save. -
Press
^Xto exit. -
Run
nanoto open the Nano editor. Line numbers are displayed on the left.