Nano is a lightweight, terminal-based text editor available on almost every Linux distribution by default. It's simple, predictable, and perfect for quick edits over SSH.

Nano uses a specific notation to represent keyboard combinations:

  • ^ denotes the Ctrl key
  • M stands for the Alt (Meta) key

Examples:

  • ^G equals Ctrl + G, which opens the help menu
  • M-A equals Alt + A

Press Ctrl + G to view Nano's help documentation. Use the arrow keys to navigate through the list of commands. Press Ctrl + X to close the help screen and return to the editor.


Creating, Editing, and Saving Files in Nano

To create or modify a file, use the nano command followed by the file name.

nano test.text

If the specified file doesn't exist, Nano will create it and open it for editing. You can immediately begin typing content.

To save changes, press Ctrl + O, then confirm the filename.

After confirming, hit Enter to save. You may continue editing and press Ctrl + O again to save additional changes.


Opening System Files

To edit system-level files, prepend the command with sudo using the structure below:

sudo nano /<systempath>/filename.txt

For instance, to open the /etc/hosts file:

sudo nano /etc/hosts

Searching and Replacing Text in Nano

- Searching

Press Ctrl + W, then type your search query when prompted. Press Enter to locate the first match.

To find additional matches, repeat Ctrl + W and press Enter again.

- Replacing

Initiate replacement by pressing Alt + R.

Enter the text you wish to find and press Enter.

Then enter the replacement text and confirm by pressing Enter again.

Once Nano highlights the first instance, you can choose:

  • Y: Replace current match
  • N: Skip this occurrence
  • A: Replace all matches
  • ^C: Cancel (where ^ means Ctrl)

Cutting, Copying, and Pasting Text in Nano

Use the steps below to perform cut, copy, and paste operations in the Nano text editor on Linux.

To copy content, press Alt + A to set a marker at the desired starting line.

Use the Left and Right arrow keys to select the text you want to copy.

Press Alt + 6 to copy the marked section.

To cut content, press Alt + A at the start of the line you wish to cut.

Again, use the Left and Right arrow keys to select the portion you want to cut.

Then press Ctrl + K to cut the highlighted text.

To paste, navigate to the desired position using the cursor.

Press Ctrl + U to paste the copied or cut text at the cursor location.


Undoing and Redoing Changes in Nano

To reverse or reapply actions in Nano, use the commands below:

  • Alt + U: Undo the most recent change
  • Alt + E: Redo the previously undone action

Customizing the Nano Text Editor

Nano reads settings from /etc/nanorc for global configuration, and from ~/.config/nano/nanorc or ~/.nanorc for user-specific settings. User-level settings override global ones.

- Local Configuration

Create a .nanorc file in your home directory:

nano ~/.nanorc

Add the lines below to enable automatic indentation and display line numbers:

set autoindent
set linenumbers

Save and exit the file. When you launch Nano, you should see indentation and line numbering activated.

- Global Configuration

To set Nano options system-wide, open the /etc/nanorc configuration file:

sudo nano /etc/nanorc

In this file, you can enable options by uncommenting lines (remove the # at the beginning). Save and close the file. Any changes you make here apply to all users on the system.


Exiting Nano

When you're finished editing, press Ctrl + X to leave Nano. If changes haven't been saved, you'll see a prompt asking if you'd like to save the modified buffer.

  • Press Y to save the changes
  • Press N to discard changes
  • Press Enter to confirm your selection

Source: Orkhan Alishov's notes