Linux nano Command


The nano command in Linux opens the Nano text editor, a straightforward and user-friendly command-line editor for creating and editing files directly in the terminal. Nano is intuitive for new users, with on-screen shortcuts for common operations.

Basic Syntax

nano [options] filename

Key Features of Nano

  • Easy Navigation: Move the cursor around the file with arrow keys.
  • Editing: Type, delete, cut, and paste text.
  • On-screen Shortcuts: Commands like saving, exiting, searching, and more are displayed at the bottom.

Example with Output

Example 1: Creating a New File or Editing an Existing File

If the file does not exist, nano will create it when you save changes.

Command
nano file.txt
Output (On-Screen in Nano)
GNU nano 5.4 file.txt This is a new file. You can start typing here. ^G Get Help ^O Write Out ^W Where Is ^K Cut Text ^X Exit

Explanation:

  • nano file.txt opens file.txt. If the file exists, its content is displayed; if it doesn’t, Nano opens a blank file for editing.
  • The bottom of the screen shows commands, like ^G for help, ^O for saving, and ^X for exiting.

Example 2: Saving a File

To save changes, use the ^O (Control + O) command.

Steps in Nano:
  1. Press Ctrl + O to write out (save).
  2. Confirm the filename (or edit it if you want to change the name), then press Enter.
On-Screen Output
File Name to Write: file.txt

Explanation:

  • Ctrl + O prompts you to confirm the filename. Pressing Enter saves your changes.

Example 3: Exiting Nano

To exit Nano, use Ctrl + X. If there are unsaved changes, Nano will prompt you to save.

Command
^X
Output
Save modified buffer (ANSWERING "No" WILL DESTROY CHANGES) ? Y Yes N No ^C Cancel

Explanation:

  • Ctrl + X prompts you to save if there are unsaved changes. Press Y to save, N to discard changes, or Ctrl + C to cancel and return to editing.

Example 4: Searching for Text

You can search for specific text in Nano using Ctrl + W.

Steps in Nano:
  1. Press Ctrl + W to initiate a search.
  2. Type the search term and press Enter.
On-Screen Output
Search: term

Explanation:

  • Ctrl + W brings up a search prompt. Enter your search term, and Nano highlights the first occurrence. Press Ctrl + W again to continue searching for the next occurrence.

Example 5: Cutting and Pasting Text

Use Ctrl + K to cut and Ctrl + U to paste text.

Steps in Nano:
  1. Move the cursor to the start of the text you want to cut.
  2. Press Ctrl + K to cut the line.
  3. Move the cursor to the desired paste location and press Ctrl + U to paste.
On-Screen Output
Text cut from file.txt and pasted here.

Explanation:

  • Ctrl + K cuts text from the current cursor position to the end of the line.
  • Ctrl + U pastes it at the cursor position.

Common Nano Shortcuts

CommandDescription
Ctrl + OSave (write out) the current file.
Ctrl + XExit Nano.
Ctrl + WSearch for text in the file.
Ctrl + KCut the current line of text.
Ctrl + UPaste the last cut text.
Ctrl + GOpen the help menu.
Ctrl + AMove to the beginning of the line.
Ctrl + EMove to the end of the line.
Ctrl + CShow the cursor position (line/column).

Summary

The nano command is an accessible text editor for quickly creating and editing files within the terminal. With intuitive shortcuts and commands shown on-screen, it provides essential editing features while being easy for new users to pick up.