A Beginner's Guide to Using the GNU nano Text Editor on Linux

A Beginner's Guide to Using the GNU nano Text Editor on Linux

As a Linux beginner, one of the first things you'll need to learn is how to create and edit text files from the command line. While Linux offers several powerful text editors, GNU nano is one of the simplest to get started with.

In this beginner's guide, we'll cover how to install nano, open and exit the editor, work with existing files, search and replace text, copy/paste, and save file changes. Follow along to get up and running with this easy-to-use Linux text editor.

An Introduction to GNU nano

The GNU nano text editor is designed to be simple and beginner-friendly, unlike more complex editors like Vim and Emacs. It includes handy features like:

  • Syntax highlighting for easy code reading.

  • Search and replace functionality.

  • Auto-indentation when writing code.

  • Support for undo/redo of edits.

  • Mouse support for easy navigation.

  • Handles UTF-8 encoding for international characters.

These capabilities make nano a great starting point for Linux beginners who just need a basic text editor to modify config files or write simple scripts.

While not as powerful as Vim or Emacs, nano has an easy learning curve that won't intimidate new Linux users. Let's go over how to install and start using it.

Step 1 — Installing GNU nano on Linux

Most Linux distributions come with nano pre-installed. To check if you already have it, open your terminal and run:

nano --version

If it displays a version number, nano is already installed. If not, we'll need to install it using the package manager:

On Debian/Ubuntu systems:

sudo apt update
sudo apt install nano

On CentOS/RHEL systems:

sudo yum install nano

On Fedora:

sudo dnf install nano

On Arch Linux:

sudo pacman -S nano

This will install the latest GNU nano version. Now let's go over the basics of using it.

Step 2 — Opening and Exiting GNU nano

To open the nano text editor, simply type nano in your terminal. This will launch nano and open a blank document.

To exit nano, press Ctrl + X on your keyboard. If you've made changes to the file, it will ask if you want to save the changes. Press Y to save or N to exit without saving.

Step 3 — Opening and Editing Existing Files

To open an existing text file in nano, provide the file name as an argument like:

nano example.txt

This will open the editor with the contents of example.txt loaded.

To edit the file, you can simply type normally. Here are some useful shortcuts:

  • Ctrl + O - Save changes to file.

  • Ctrl + X - Close the editor.

  • Ctrl + K - Cut current line.

  • Ctrl + U - Paste cut line.

  • Ctrl + - Search/replace text.

The bottom of the editor shows all available keyboard shortcuts.

Step 4 — Navigating within Files

Nano makes it easy to navigate through open files:

  • Ctrl + Y - Page up

  • Ctrl + V - Page down

  • Ctrl + C - Display current cursor position.

  • Ctrl + W - Search for text in file.

  • Alt + A - Start text selection.

  • Ctrl + A - Jump to start of line.

  • Ctrl + E - Jump to end of line.

Use these shortcuts to swiftly move around within larger files opened in nano.

Step 5 — Searching and Replacing Text

To find text within the open file, press Ctrl + W and enter the search term.

Press Ctrl + to bring up the search and replace prompt. Enter the text to find, hit enter, then type in the replacement text. You can then replace all occurrences or just the first match.

Using search and replace is an easy way to quickly modify multiple instances of text throughout a file in nano.

Step 6 — Copying and Pasting Text

To copy text, place your cursor at the start of the section you want to copy and press Alt + A to begin selection. Use your arrow keys to highlight the text, then press Ctrl + K to copy.

Position your cursor where you want to paste, and press Ctrl + U to paste the copied text.

Highlighting text with Alt + A before copying is the easiest way to copy specific sections in nano.

Step 7 — Saving Changes and Exiting nano

When you are done editing, press Ctrl + X to exit nano. It will prompt you to save the changes to the file.

Press Y to save the changes, then hit Enter to confirm the file name.

If you want to save the file with a different name, instead type the new name and press Enter.

That covers the basic workflow for editing text files with GNU nano! The integrated shortcuts make common operations quick and easy.

Recap of Basic GNU nano Usage:

  • Install nano using your system's package manager.

  • Launch with nano or open files like nano file.txt.

  • Use Ctrl + O to save and Ctrl + X to exit.

  • Edit text by typing normally anywhere in the file.

  • Ctrl + C shows current cursor position.

  • Ctrl + K cuts and Ctrl + U pastes lines of text.

  • Find text with Ctrl + W and replace with Ctrl + .

  • Ctrl + Y and Ctrl + V scroll the file.

  • Alt + A highlights text to copy.

  • Save files with Ctrl + O and exit editor with Ctrl + X.

Learning these basic nano shortcuts will have you editing files comfortably in no time! It may lack some advanced features, but GNU nano remains a simple and approachable text editor ideal for Linux beginners.