Your Nano Tutorial: Create, Edit and Save Files

Many Linux distributions include the nano text editor due to its simplicity and power. Learn to create, edit, save and close files using nano.
Your Nano Tutorial Create_ Edit_ and Save Files

The nano text editor is an essential part of many Linux distributions. It's easy and effective, especially for those folks just starting with Linux.

Nano is a kinder and gentler introduction to command-line text editing than a tool such as Vim. Vim is certainly more powerful and feature-rich, but that's only useful if you need those extra features. If you're jumping into a configuration file to change a setting from yes to no, then all that extra effort learning Vim tricks may be wasted.

Linux command-line environments (mainly Bash) do not rely on pull-down menus or allow for mouse-based interactions. You must accomplish everything via the keyboard. Administrators often deploy Linux servers without a graphical user interface (GUI). Because Linux stores configurations in text files, for administrators to make changes, they must edit these files. Nano can be an excellent solution.

Text-editing is an assumed skill for roles and certifications involving Linux, including:

If you're pursuing Cisco, Red Hat Linux or other vendor certifications, you'll need to be comfortable editing configuration files with tools such as nano.

This article guides you through four necessary tasks for managing files with nano: Create/open, edit, save and exit. I included a few extra tips and tricks at the end.

Install Nano

Ensure nano is installed on your system. You may use Linux or macOS and, with somewhat more difficulty, Windows. Installation steps vary between versions and operating systems, so be ready to read the documentation for your preferred platform.

Nano on Linux

Your Linux distribution may already have nano installed. If it does not, follow these steps:

If you use a distribution such as CentOS, Fedora or Red Hat Enterprise Linux (RHEL), type:

$ sudo yum install nano

If you use a distribution such as Debian or Ubuntu, type:

$ sudo apt install nano

You will see a message that nano is already installed, or the installation process will begin.

Nano on MacOS

I suggest using the Brew package manager on your Mac to make installation easier. With Brew installed, type:

$ brew install nano

MacOS no longer includes nano as it once did. If you don't have nano installed but issue the nano command, the system runs the pico editor instead.

Nano on Windows

Various nano projects to install the editor on Windows exist on GitHub. You may need to change PowerShell execution policies or set environment variables to get nano functioning. The process will vary by Windows edition and nano version, so check for specific documentation for your use case.

Once you install nano, follow the steps below to get started.

4 Basic Nano Tasks

Managing configuration files encompasses four basic steps:

  1. Create a new file or open an existing one
  2. Edit the text
  3. Save your changes
  4. Exit the text editor

Any additional tricks you learn about nano will make you more efficient, but these four tasks are essential.

Create or Open a File

Open a Terminal in your chosen operating system. Browse to a directory you can use to create test files; this is probably just your home directory.

To open nano and create a new file named demo.txt simultaneously, type:

$ nano demo.txt

You've now created a new empty file. That's the first step!

Nano Command or Key

Result

nano demo.txt

Create or open the demo.txt file in nano


If you're opening a file that already exists, type the nano command and the path to the file. For example, to edit the SSH server configuration file on Fedora, type:

$ nano /etc/ssh/sshd_config

The file opens for editing.

Edit a File

Now that you have a file—either a new blank file or a populated existing file—it's time to begin inputting or editing the text. Nano does not use modes the way Vim does. Instead, nano relies on meta keys—often the Ctrl key.

Assuming you created the empty demo.txt file above, enter some text. For example, you might type:

This is my new nano file.

The common commands are displayed below the text.

Navigate within the file using the arrow keys to move up, down, left, and right.

Your file now has contents—good job! Now it's time to save your new text. Look at the menu at the bottom of the window for some clues.

nano-menu2

The nano menu. Note the Write Out and Exit commands.

Save a File

Like Vim, nano uses the term "write out" to mean "save my changes to disk." To save your file, select Ctrl+O. Nano prompts you to accept or modify the file name. Accept the existing file name by pressing Enter.

Nano Command or Key

Result

Ctrl+O

Save or write changes to the file

 




You successfully saved your edits. But wait, you're still in nano. How can you exit the program and return to your shell?

Exit Nano

At this point, you've created a file, added content and saved your changes. It's time to quit nano.

Check the menu at the bottom of the window again. There's an exit option. Use Ctrl+X to close nano. By the way, if you forgot to save your changes, nano prompts you before closing.

Nano Command or Key

 

Result

Ctrl+X

Exit nano


using-nano

Use nano to create a file and enter text.

What's Next?

Nano includes many user-friendly and practical features. Once you master the basics, you may wonder what else nano can do. It's worth noting that some key combinations vary between nano versions.

Many tricks exist; the following are a few of my favorites.

Display Line Numbers

Many configuration files and scripts are huge, spanning hundreds of lines. Displaying line numbers is essential; it's usually one of the first modifications I make to whatever editor I'm using.

To display line numbers in the current nano session, type:

Alt + Shift + 3

File Navigation

You can navigate among the line numbers using the arrow keys. This is fine in a small file, but what about a file with hundreds or thousands of lines? You might receive instructions that say, "edit line 42" and want to jump to that location specifically.

Nano has you covered, type:

Ctrl+_

Nano prompts you to enter the line number you want to jump to. Note that the command is the Ctrl key plus the underscore character, so technically, you type Ctrl+Shift+- and nano prompts you for a line number.

To jump to the top of the file, type:

alt+\

To jump to the bottom of the file, type:

alt+/

Alternatively, if your keyboard has Home and End keys, use Ctrl+Home or Ctrl+End to get to the beginning or end of your file.

Search a File

Jumping to specific line numbers is useful if you know what line a particular value is on. In many cases, a keyword search might be more helpful. To search for a specific string, type:

Ctrl+W {searchterm}

For example, to search for the string "admin" type:

Ctrl+W admin

Press Enter to see the results.

Nano includes powerful features to help with text editing tasks. Managing content via line numbers is a useful task in almost any file, as is searching for specific terms. The following table summarizes the commands for this section.

Nano Command or Key

Result

Alt+Shift+3

Display line numbers

Ctrl+_

Prompt for a line number to jump to

Alt+\

Jump to the top of the file

Alt+/

Jump to the bottom of the file

Ctrl+W {searchterm}

Search for the given searchterm

Use Nano

Nano can be a great editor for introducing new Linux users to the challenge of managing text files from the command line. Vim used to be the de facto standard, but today, many distributions include nano without providing Vim. Nano is very customizable and feature-rich. Once you're comfortable with the standard editing tasks, explore options such as syntax highlighting and spell-checking.

Install nano today and begin learning how to use this common and handy text editor, then apply your knowledge to Linux configuration files throughout the CompTIA certification exams.

Learn the skills you need with CompTIA CertMaster Learn. Sign up today for a free trial today!

Email us at [email protected] for inquiries related to contributed articles, link building and other web content needs.

Read More from the CompTIA Blog

Leave a Comment