How to Use the Nano Text Editor in Linux

Updated on February 21, 2025
How to Use the Nano Text Editor in Linux header image

Nano is a command-line text editor that allows you to open and manipulate files in Linux. It includes a user-friendly interface with a function-based navigation menu with keyboard shortcuts to modify files. Nano provides essential editing capabilities, making it ideal for editing all types of files on Linux.

This article explains how to use the Nano text editor in Linux and use it to create, open, and modify files.

Prerequisites

Before you begin, you need to:

Install Nano

Nano is pre-installed and available on most Linux distributions by default. Follow the steps below to install it if it's unavailable on your workstation.

Ubuntu and Debian

Follow the steps below to install Nano on Debian-based distributions.

  1. Update the apt package index.

    console
    $ sudo apt update
    
  2. Install Nano.

    console
    $ sudo apt install nano -y
    

Open the Nano Text Editor

Follow the steps below to open the Nano text editor in Linux.

  • Enter nano in your terminal session to open the Nano text editor.

    console
    $ nano
    

    The Nano Text editor should open in your session similar to the following graphic.

    Nano Editor Buffer

    The top bar displays the Nano version and the filename. If it shows New Buffer, it means no file is open while the bottom section lists commonly used shortcut keys.

Nano Keyboard Shortcuts

  • Nano uses the special key notation standard for keyboard shortcuts.

    • ^ represents the Ctrl key.
    • M represents the Alt (Meta) key.

    For example:

    • ^G means Ctrl + G (opens the help menu).
    • M-A means Alt + A.

    Press Ctrl + G to access the Nano help menu.

    Nano Help Page.png

    You can scroll through the help menu to view all available options. This menu provides a list of commands to navigate, edit, search, and customize the Nano CLI editor. Use the Up and Down arrow keys to explore more commands. Press Ctrl + X to exit the help menu and return to the editor.

Create, Edit, and Save Files Using Nano

Follow the steps below to create, edit, and save files using the Nano text editor on Linux.

  1. Enter the nano command followed by the filename to create or edit the file.

    console
    $ nano test.text
    

    Nano Editor Home

    If the file does not exist, Nano creates it and opens it for editing. You can start typing directly in the editor.

  2. To save a file in Nano, press Ctrl + O and confirm the filename when prompted.

    Nano Saving file

    Press Enter to confirm. You can continue editing and press Ctrl + O again to save changes.

  3. To open system files using Nano, use the sudo command with the following syntax.

    console
    $ sudo nano /<systempath>/filename.txt
    
    • For example, run the following command to open the /etc/hosts file.

      console
      $ sudo nano /etc/hosts
      

      Nano-Editor-with-Sudo.png

Search and Replace Text Using Nano

Follow the sections below to search or replace text using the Nano text editor.

Search for Text

  1. Press Ctrl + W and enter a search term when prompted.

    Nano-Search-Lorem.png

  2. Press Enter and verify that the cursor moves to the beginning of the first match.

    Nano-Search-Lorem-Found.png

  3. Press Ctrl + W to find the next match and press Enter.

    Find-Next-Occurrence.png

Replace Text

  1. Press Alt + R to define the text to replace.

  2. Enter a search term to replace.

    Nano-Replace-Lorem.png

  3. Press Enter to define the replacement text.

  4. Enter the replacement text and press Enter.

    Nano-Replace-With-Text.png

  5. Verify that the Nano text editor highlights the first instance of the text and choose any of the following options to apply.

    • Y - Yes (Replace this instance)
    • N - No (Skip this instance)
    • A - All (Replace all instances)
    • ^C - Cancel ( ^ refers to the Ctrl key)

    Nano-Replacement-Instance.png

Cut, Copy, and Paste Text Using Nano

Follow the steps below to cut, copy, and paste text using the Nano text editor in Linux.

  1. To copy text, press Ctrl + 6 to set a mark on a target line to copy.

  2. Use the Left and Right arrow keys to highlight the text to copy.

    Nano-Mark-Set.png

  3. Press Alt + 6 to copy the selected text.

  4. To cut text, press Ctrl + 6 to mark the target line with text to cut.

  5. Use the Left and Right arrow keys to select the text to cut.

    Nano-Mark-Set.png

  6. Press Ctrl + K to cut the text.

  7. To paste text, move your cursor to your desired location to paste the text.

    Nano-Paste.png

  8. Press Ctrl + U to paste the text.

Delete Text Using Nano

Follow the steps below to delete characters, words, and entire lines in a file using Nano.

  • Press Ctrl + H to delete a character before the cursor.
  • Press Ctrl + D to delete a character after the cursor.
  • Press Alt + Backspace to delete a word to the left.
  • Press Ctrl + Delete to delete a word to the right.
  • Press Alt + Delete to delete an entire line.
  • To delete multiple lines, select them using Ctrl + 6, then press Alt + Delete.

Undo and Redo Changes Using Nano

Follow the steps below to undo and redo changes in Nano.

  • Press Alt + U to undo the last action.
  • Press Alt + E to redo the last undone action.

Customize the Nano Text Editor

In this section, you will learn how to configure Nano to enhance your editing experience. Nano reads its configuration parameters from the system-wide file /etc/nanorc and from user-specific files ~/.config/nano/nanorc and ~/.nanorc. Options specified in the user files take precedence over the global options.

Configure Nano Locally

  1. Create the .nanorc file in your home folder.

    console
    $ nano ~/.nanorc
    
  2. Add the following lines to enable auto-indentation and line numbers.

    ini
    set autoindent
    set linenumbers
    

    Save and close the file.

  3. Verify that the text and line numbers are indented when you open Nano.

    console
    $ nano
    

Configure Nano Globally

  1. Open the /etc/nanorc file to apply system-wide changes.

    console
    $ sudo nano /etc/nanorc
    
  2. Uncomment directives in the file by removing the # symbol to enable a feature.

    Nanorc-configuration-file.png

    Save and close the file.

    All changes to the global nanorc configuration apply to all users on your system. Open the Nano text editor and verify that the new features are available.

Exit Nano

Follow the steps below when exiting the Nano text editor.

  • Press Ctrl + X to exit the Nano text editor. If there are unsaved changes, you will receive a Save modified buffer prompt to verify the changes. Then:
    • Press Y to save.
    • Press N to discard changes.
    • Press Enter to apply the file changes.

Conclusion

You have used the Nano text editor in Linux to create, edit, search, replace, cut, copy, delete, and configure files. Nano allows you to open and manipulate files in Linux, allowing you to modify configurations, files, and scripts on your workstation. For more information and configuration options, visit the Nano Documentaiton