In this guide, we will explain some of the best ways to use the nano text editor.
nano is a command-line text editor that is often included with Unix-like computing systems such as Raspberry Pi OS or Ubuntu.
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.
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.
For example, if we have a file called “
raspberrypi.mylifeup” we can begin editing it by using the command below.
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.
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
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.
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.
Then you will be asked to type in the text you want to replace it with.
Finally, you will be prompted for every instance whether you want to replace it.
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.
To save the file when quitting out, you will need to type in Y when prompted, then press the ENTER key.
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.
|CTRL + S||Saves the current file|
|CTRL + O||Offer to write file (“Save as”)|
|CTRL + R||Insert a file into the current buffer|
|CTRL + X||Close buffer, exit from nano|
Below is a table of some of the shortcuts you can use to make it easier to edit the file.
|CTRL + K||Cuts the current line and stores it into the cutbuffer|
|ALT + 6||Copies the current line and stores it in the cutbuffer|
|CTRL + U||Paste contents of the cutbuffer into the file|
|ALT + T||Cuts from the cursor’s location to the end of the file|
|CTRL + ]||Completes the current word|
|ALT + 3||Comments or uncomments the selected line or region|
|ALT + U||Undoes the last action|
|ALT + E||Redoes the last undone action|
Search and Replace Shortcuts
This table includes shortcuts to help with searching for and replacing text.
|CTRL + Q||Start searching backwards|
|CTRL + W||Start searching forwards|
|ALT + Q||Find the next occurrence backwards|
|ALT + W||Find the next occurrence forwards|
|ALT + R||Start searching for and replacing text|
Below are some shortcuts that help with deleting characters, words, and lines in a file.
|CTRL + H||Delete the character before the cursor|
|CTRL + D||Deletes the character that is currently selected by the cursor|
|CTRL + SHIFT + D||Deletes the word to the left of the cursor|
|CTRL + DEL||Deletes the word to the right of the cursor|
|ALT + DEL||Deletes 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.
|CTRL + B||Moves the cursor one character backward|
|CTRL + F||Moves the cursor one character forward|
|CTRL + ←||Moves the cursor one word backward|
|CTRL + →||Moves the cursor one word forward|
|CTRL + A||Moves the cursor to the start of the line|
|CTRL + E||Moves the cursor to the end of the line|
|CTRL + P||Moves the cursor one line up|
|CTRL + N||Moves 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 + Y||Moves the cursor up one full page of text|
|CTRL + V||Moves 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.
|ALT + G||Go 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.
|CTRL + T||Runs a spell check on the current file|
|CTRL + J||Justify the currently selected paragraph or region|
|ALT + J||Justify the entire buffer|
|ALT + B||Runs a syntax check on the file|
|ALT + F||Allows you to run a formatter on the file|
|ALT + :||Start / stop recording a macro|
|ALT + ;||Replays your recorded macro|
This table includes shortcuts that allow you to get more information on your file and nano.
|CTRL + C||Reports the current position of the cursor within the file|
|ALT + D||Reports the number of words, lines, and characters within the file or selection|
|CTRL + G||Displays the help text for nano|
Below is a table that includes some of the miscellaneous shortcuts that you can use within nano.
|ALT + A||Sets and unsets the start of a marked area|
|TAB||Indents the currently marked region|
|SHIFT + TAB||Unindents the currently marked region|
|ALT + N||Toggles the line numbers on/off|
|ALT + P||Allows you to enter the next keystroke as verbatim|
|CTRL + L||Refreshes the screen|
|CTRL + Z||Suspends 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.