Guide to the nano Text Editor

In this guide, we will explain some of the best ways to use the nano text editor.

nano text editor guide

nano is a command-line text editor that is often included with Unix-like computing systems such as Raspberry Pi OS or Ubuntu.

It is our preferred text-editor when working within the command-line. We use nano within all of our Raspberry Pi projects and Linux guides.

When getting used to using a Linux system, it can often be daunting when trying to use more advanced text-editors such as vim or emacs.

This is where nano comes in to play. We found nano to be incredibly straightforward to use, even for the most beginner Linux user.

Within this guide, we will be showing you how to use the nano text editor and its various shortcuts.

Creating or Editing a File in nano

In this section, we will explain the various ways of using nano to either create or edit a file.

As you will soon see, creating and editing files using the nano text editor is a straightforward process.

Using nano Without Specifying a Filename

The most straightforward use of nano is to use the command without specifying a filename.

When you use nano without specifying a file, you will start the editing session with a clean buffer.

This buffer will be your empty text file without a filename attached to it.

nano

When you save the buffer, you will be asked to specify the filename for it.

Specifying a Filename to Create or Edit

You can also specify a filename alongside the nano command.

nano will use this filename to either create a new file or edit an existing one.

nano FILENAME

For example, if we have a file called “raspberrypi.mylifeup” we can begin editing it by using the command below.

nano raspberrypi.mylifeup

If this file doesn’t exist, you will see an empty buffer that you can start writing in.

When you save, the contents will be written to the filename that you specify.

Editing Text with nano

Editing text using the nano editor is a straightforward process, which is one of nano’s most significant benefits.

To edit the text using nano, all you need to do is type using your keyboard.

nano editing text

You can move the cursor (the point you are typing) by using the ARROW keys.

The ENTER key is used to add a new line.

Listing the nano Shortcuts

In the next couple of sections, we are going to show you some shortcuts you can use to make modifying and navigating a file in nano easier.

If you wanted to explore these shortcuts yourself, you can find these by pressing CTRL + G

nano main help and shortcuts

Once you have finished exploring these shortcuts, you can return to the editor screen by pressing CTRL + X.

Searching for Text with nano

One of the first things you will want to learn is how to search for text within a file.

Knowing how to search for text makes it significantly easier to navigate large files when you are looking for a particular phrase.

To search for text with nano, press CTRL + W.

Type in the text you are searching for, then press the ENTER key.

nano search for text

If you wanted to repeat the search, you can press ALT + W or press CTRL + W.

The search function of nano will look in a forward direction until it wraps back to the top of the file.

If you wanted to search backward instead of forward, you can use the Q key instead of W. So, you would press, CTRL + Q to search backward, and ALT + Q to repeat the search backward.

Searching and Replacing Text With nano

We can also use nano to search and replace text.

To begin searching for the text you want to replace, press ALT + R.

First, you will be prompted to type in the text you want to replace.

nano search text to replace

Then you will be asked to type in the text you want to replace it with.

nano replacement text

Finally, you will be prompted for every instance whether you want to replace it.

nano replace found text instance

Press Y to replace the selected instance or N to move to the next one.

Alternatively, if you want to replace all instances within the text, you can press the A key.

You can also cancel the search and replace by pressing CTRL + C.

Copying and Pasting Text in nano

In this section, we will be showing you how to copy text within nano.

Text that is copied within the editor will be stored within a special buffer called the “cutbuffer”.

To copy the currently selected line, press ALT + 6.

It is also possible to copy a selection of text, use CTRL + 6 to mark the start of the selection. Once you have made the selection, you can use ALT + 6 to copy it.

You can paste all your copied data within the nano text editor by pressing CTRL + U.

You can paste the contents of the buffer continually till you make a new copy.

Cutting and Pasting Text in nano

In this section, we will be showing you how to cut and paste text within the nano text editor.

To cut an entire line of text, you can use CTRL + K.

You can also highlight a selection of text to cut as well. To start the selection press CTRL + 6, then use the ARROW keys to mark your selection.

Once you have made a selection, you can use the CTRL + K to cut it.

All the text that you cut will be stored in a buffer called the “cutbuffer“. This buffer will be continually added to till you perform a paste.

To paste the contents of the cut buffer, press CTRL + U. You can paste the same contents multiple times until a new line is cut or copied.

Saving and Quitting in nano

To save a file within the nano text editor, press CTRL + O.

You will also be prompted to save when you go to quit out of the file by using CTRL + X.

nano save file

To save the file when quitting out, you will need to type in Y when prompted, then press the ENTER key.

nano shortcuts

Below you can find a table of all the available shortcuts that you can use within nano to make your life easier.

This is the same list of shortcuts that you can access by using CTRL + G within nano.

File Handling Shortcuts

Below is a list of some of the shortcuts related to file handling within nano.

KeysDescription
CTRL + YSaves the current file
CTRL + OOffer to write file (“Save as”)
CTRL + RInsert a file into the current buffer
CTRL + XClose buffer, exit from nano

Editing Shortcuts

Below is a table of some of the shortcuts you can use to make it easier to edit the file.

KeysDescription
CTRL + KCuts the current line and stores it into the cutbuffer
ALT + 6Copies the current line and stores it in the cutbuffer
CTRL + UPaste contents of the cutbuffer into the file
ALT + TCuts from the cursor’s location to the end of the file
CTRL + ]Completes the current word
ALT + 3Comments or uncomments the selected line or region
ALT + UUndoes the last action
ALT + ERedoes the last undone action

Search and Replace Shortcuts

This table includes shortcuts to help with searching for and replacing text.

KeysDescription
CTRL + QStart searching backwards
CTRL + WStart searching forwards
ALT + QFind the next occurrence backwards
ALT + WFind the next occurrence forwards
ALT + RStart searching for and replacing text

Deletion Shortcuts

Below are some shortcuts that help with deleting characters, words, and lines in a file.

KeysDescription
CTRL + HDelete the character before the cursor
CTRL + DDeletes the character that is currently selected by the cursor
CTRL + SHIFT + DDeletes the word to the left of the cursor
CTRL + DELDeletes the word to the right of the cursor
ALT + DELDeletes the currently selected line

Shortcuts to Help Moving Around

This table includes some shortcuts to help move around a file within the nano text editor.

KeysDescription
CTRL + BMoves the cursor one character backward
CTRL + FMoves the cursor one character forward
CTRL + Moves the cursor one word backward
CTRL + Moves the cursor one word forward
CTRL + AMoves the cursor to the start of the line
CTRL + EMoves the cursor to the end of the line
CTRL + PMoves the cursor one line up
CTRL + NMoves the cursor one line down
CTRL + Moves the cursor to the previous block of text
CTRL + Moves the cursor to the next block of text
CTRL + YMoves the cursor up one full page of text
CTRL + VMoves the cursor down one full page of text
ALT + \Moves the cursor to the top of the file
ALT + /Moves the cursor to the bottom of the file

Special Movement Shortcuts

These shortcuts are in addition to the previous one. These are special movement shortcuts that you can use to move around a file in nano.

KeysDescription
ALT + GGo to the specified line
ALT + ]Go to the complementary bracket
ALT + Scroll the viewport up
ALT + Scroll the viewport down
ALT + <Switch to the preceding buffer
ALT + >Switch to the succeeding buffer

Special Operation Shortcuts

These shortcuts allow you to perform special operations within the current buffer. For example, you can make nano run a spell check.

KeysDescription
CTRL + TRuns a spell check on the current file
CTRL + JJustify the currently selected paragraph or region
ALT + JJustify the entire buffer
ALT + BRuns a syntax check on the file
ALT + FAllows you to run a formatter on the file
ALT + :Start / stop recording a macro
ALT + ;Replays your recorded macro

Information Shortcuts

This table includes shortcuts that allow you to get more information on your file and nano.

KeysDescription
CTRL + CReports the current position of the cursor within the file
ALT + DReports the number of words, lines, and characters within the file or selection
CTRL + GDisplays the help text for nano

Miscellaneous Shortcuts

Below is a table that includes some of the miscellaneous shortcuts that you can use within nano.

KeysDescription
ALT + ASets and unsets the start of a marked area
TABIndents the currently marked region
SHIFT + TABUnindents the currently marked region
ALT + NToggles the line numbers on/off
ALT + PAllows you to enter the next keystroke as verbatim
CTRL + LRefreshes the screen
CTRL + ZSuspends nano

Hopefully, at this stage, you will now understand how to use nano on your Linux device.

As mentioned earlier, we are a big fan of nano and use it heavily in our Raspberry Pi projects.

If you feel like we have missed something or are having trouble with nano, feel free to leave a comment below.

Be sure to check out some of our other great Linux guides.

Leave a Reply

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