July 14, 2020
Article feature image

First steps with the Nano text editor

Nano is a versatile, easy to use, and quick to learn text editor for the Linux terminal. This article presents the first steps with the Nano text editor. The goal is to get you comfortable with editing text files from the terminal. The article covers topics such as managing files, navigating the cursor, copy and paste, search and replace. The article includes brief exercises to try out your newly learned skills.

A general concept of Linux is that everything is a file. Especially when configuring your system and the installed applications, you typically do so by editing configuration text files. When working with Linux servers, you often only have access to the terminal through SSH. Consequently, the only way to edit files is with a terminal text editor. Luckily, there are plenty of terminal text editors available on Linux such as Nano, Vim and Emacs. I selected Nano for this article, because in my opinion it is the quickest one to learn and it gets installed by default on all major Linux distributions.

Let’s get started by creating a file with a few lines of text for testing purposes. Open the terminal in your home directory and run the command:

echo $'1. Debian\n2. Ubuntu\n3. openSUSE\n 4. Fedora\n5. Arch' >distros.txt

File management

Before diving into the actual text editing part, it is important to know how to open, close and save files. To open a file for editing with Nano, simply type nano followed by the name of the file. If the file is not in the current directory, then add the directory to the filename as well. In case your user does not have write access to the file, you can start the command with sudo to open and edit it with super user privileges. Examples:

sudo nano /etc/timezone

nano distros.txt

After opening the file in Nano, you can close it again with the Ctrl + x key combination. If you made changes to the file, Nano asks you if you want to save the changes. Press y to save the changes or n to ignore them, followed by Enter to commit.

Press the Ctrl + o keys to save changes while editing the file. Nano prompts you with the filename to save to. You can optionally change the filename, if desired. Press Enter to commit the save operation.

Exercise: Open and then close the distros.txt file that you created previously.

Shows the contents of the newly created distros.txt file in the Nano text editor.

Cursor navigation

Navigating the cursor is the same as when you edit a file with a regular word processor, such as LibreOffice Writer. This means the arrow keys move the cursor one position in the direction of the arrow. The Home key brings the cursor to the start of the line. Similarly, the End key brings the cursor to the end of the line.

The Page Up and Page Down keys move the cursor several lines up or down, respectively. This is handy for scrolling quickly through the file. To jump directly to the first line in the file, press Alt + \. The other direction is possible too by pressing Alt + /. This jumps straight to the last line in the file.

Exercise: Open the distros.txt file and navigate to the 4. Fedora line. This line starts with a space character and causes the listed Linux distributions to not be properly aligned. Fix this mistake (Backspace or Del), save the changes (Ctrl + o), and close the file (Ctrl + x).

Cut, copy and paste

Pressing the Ctrl + k keys cuts text to the internal buffer (clipboard) and the Alt + 6 key combination copies text to the buffer. To paste the contents of the buffer, starting at the current cursor position, press Ctrl + u.

If no text is marked (selected), Nano cuts or copies the entire line, where the cursor is currently located. Otherwise Nano only cuts or copies the marked text.

The Alt + a key combination enters a mode for marking text. Once entered, you can navigate the cursor to mark text starting at the current cursor location. To undo the text marking, simply exit the mode by pressing the same Alt + a keys again.

An alternative method for quickly marking text is by holding down the Shift key and then navigate the cursor. To me this is more intuitive and therefore my preferred technique for marking text.

Undo and redo

Nano keeps a history of all the operations you perform, making it possible to undo and redo changes. The Alt + u keys undo the last recorded operation. Likewise, the Alt + e keys redo the last operation you undid.

Exercise: Open the distros.txt file and use Nano’s cut and paste functionality to invert the order of the list with Linux distributions. If something goes wrong, you can rely on undo to correct it. Once you are done, save the changes and close the file. Its contents now look like:

Shows how the contents of the distros.txt file should look after completing the exercise where the entries were reordered.

Search and replace

Nano includes a search feature to quickly locate a specific string in a file. After pressing Ctrl + w, you can enter a search term. Hit Enter afterwards to start the search from the cursor position. Nano remembers the last string you searched for. It shows this string between square brackets, when starting a new search. This means that you do not have to retype the string when you want to search for the next occurrence of it.

Replacing one or all occurrences of a specific string is also supported by Nano. The Ctrl + \ key combination enters the search and replace mode. Similar to searching for text, you first enter the string to search for, followed by Enter. Right after this you can enter the string to replace it with. Hit Enter to continue with the replace operation. If Nano finds the string, it asks you if you want to replace this particular instance (y), skip it (n), or replace all instances of the string (a). This procedure is repeated, if the search string appears more than once in the text.

Exercise: Open the distros.txt file and use Nano’s replace functionality to change Arch to Manjaro. Once you are done, save the changes and close the file.

Screenshot of the Nano editor when a search and replace operation is active.

After reading through this article and completing the included exercises, you now know your way around the Nano editor and are comfortable with editing files directly from the terminal. In my experience, this is an essential skill when working with Linux. The last new Nano command to finish this article with is Ctrl + g. This opens a page in Nano with its help screen and serves as a quick reference. Press Ctrl + x to close the help page and return to the file you are editing.

I hope you enjoyed this article. If you would like to receive new articles automatically in your Email in-box, consider subscribing to the blog.


Long term Linux enthusiast, open source software developer and technical writer.

View all posts by PragmaticLinux →

3 thoughts on “First steps with the Nano text editor

Leave a Reply

Your email address will not be published. Required fields are marked *