nano text editor

Nano is a simple, easy-to-use text editor for Linux systems, including Ubuntu. It's a terminal-based editor, designed to be intuitive and user-friendly, making it ideal for beginners. Here's a detailed guide on using the Nano text editor in Ubuntu.

Installation of Nano

Nano is typically pre-installed on Ubuntu. To check if it is installed, run the following command:

nano --version

If it's not installed, you can install it using:

sudo apt update
sudo apt install nano

Basic Command Structure

To open or create a file using Nano, use the following syntax:

nano filename
  • If the file exists, it opens for editing.

  • If the file doesn’t exist, a new file with the given name will be created.

For example:

nano myfile.txt

Interface Overview

When you open Nano, the interface looks like this:

GNU nano 6.2      myfile.txt                                    
                                                                
This is a test file.                                             
                                                                
^G Help      ^O Write Out  ^W Where Is  ^K Cut Text  ^J Justify   
^X Exit      ^R Read File  ^\ Replace   ^U Paste Text ^T To Spell
  • The File Contents: The main area displays the content of the file you’re editing.

  • Key Commands: At the bottom, you’ll see commonly used commands with the ^ symbol, which means you need to press the Ctrl key followed by the command key (e.g., ^X means Ctrl + X).

Common Nano Commands

Here are some frequently used commands in Nano:

Command
Description

Ctrl + O

Save (write out) the file. Nano will ask for a file name if it's a new file.

Ctrl + X

Exit the editor. If there are unsaved changes, Nano will prompt you to save the file.

Ctrl + K

Cut the current line (acts like a delete line).

Ctrl + U

Paste text (used after cutting).

Ctrl + W

Search for a word or phrase.

Ctrl + G

Display help with all commands.

Ctrl + J

Justify text.

Ctrl + C

Show the current cursor position (line and column).

Ctrl + R

Insert the contents of another file at the cursor position.

Ctrl + T

Open the spell checker (you need to have spell check tools installed).

Ctrl + \

Find and replace text in the document.

Editing a File

  1. Opening a File: To edit an existing file or create a new one:

  2. Typing: Start typing or editing the file right away. Nano automatically updates the content as you type.

  3. Saving the File: When you want to save the file, press Ctrl + O. Nano will prompt you to confirm the filename. Press Enter to save with the same name, or modify the name before pressing Enter.

  4. Exiting: Press Ctrl + X to exit. If you haven’t saved your changes, Nano will prompt you to save them before quitting.

  • Arrow Keys: Use the arrow keys to move the cursor up, down, left, and right.

  • Go to Specific Line: You can jump to a specific line by pressing Ctrl + _ and then entering the line number.

Searching Text

To search for a specific string of text:

  • Press Ctrl + W.

  • Type the string you're looking for and press Enter.

  • Nano will move the cursor to the first occurrence of that string. You can continue searching by pressing Ctrl + W and hitting Enter again.

Cut, Copy, and Paste

  1. Cutting Text: Move the cursor to the line you want to cut and press Ctrl + K. This cuts the entire line.

  2. Pasting Text: To paste the cut line, move the cursor to the desired location and press Ctrl + U.

  3. Copying Text: Unlike typical editors, Nano does not have a direct copy command. However, you can cut text with Ctrl + K and immediately paste it back with Ctrl + U, leaving the original text unchanged (effectively copying it).

File Operations

  1. Open Another File: While editing, you can open another file by pressing Ctrl + R and entering the file name. The contents of the new file will be inserted into the current file at the cursor's position.

  2. Write to Another File: You can also write part of the buffer (file content) to another file. To do this:

    • Mark the text you want to write by pressing Ctrl + ^ (Ctrl + Shift + 6) and moving the cursor to highlight the section.

    • Press Ctrl + O and enter the file name where you want to write the selected text.

Nano Configuration

You can customize Nano's behavior by editing its configuration file: /etc/nanorc or ~/.nanorc.

For example, to enable syntax highlighting, add this line in .nanorc:

Useful Nano Options

You can pass options when opening Nano. Some commonly used options are:

Option
Description

-B

Creates a backup of the file before editing.

-m

Enables mouse support (to position the cursor or highlight text).

-i

Automatically indent new lines to match the indentation of the previous line.

-c

Shows the cursor position (line and column) constantly.

-l

Enables line wrapping at the end of the screen.

-v

Opens the file in view mode (read-only).

For example:

This command will open myfile.txt, show the cursor position constantly, and create a backup of the file.

Example: Editing a Configuration File

Let’s say you want to edit the /etc/hosts file to map a domain name to an IP address. You can use Nano to do that:

  1. Open the file with root privileges:

  2. Navigate to the desired location and add a line:

  3. Save the file with Ctrl + O and exit with Ctrl + X.

Conclusion

Nano is a powerful yet simple text editor that is perfect for beginners and advanced users who want to quickly edit text files in the terminal. Although it lacks the advanced features of editors like Vim or Emacs, it excels in ease of use.

Last updated