Skip to main content

File Editing Commands

Nano Editor Basic Commands

Get a quick reference to using the Nano editor for editing files in the Linux terminal.

The nano text editor is my favorite text editor as it has almost everything that every basic to moderate user would ever require.

While the nano text editor is easy to navigate, it still has some crazy features upon its sleeve that you may not be aware of.

So in this tutorial, I will walk you through all the basics you'd ever need to get started with the nano editor.

Common keyboard shortcuts for the Nano editor

When you work with any command line utility, you have to rely on the shortcuts for most tasks and that is why it is important to get started with the shortcuts.

So here's a list of all the basic shortcuts of the nano text editor:

Shortcut Description
Ctrl+G Display the help menu and list of shortcuts.
Ctrl+X Exit the nano editor.
Ctrl+O Save the current file (WriteOut).
Ctrl+R Open a file for reading into the buffer (Read).
Ctrl+C Show the position of the cursor (line, column, character).
Ctrl+V Move the cursor to the next screen.
Ctrl+Y Move the cursor to the previous screen.
Ctrl+K Cut the current line and store it in the cutbuffer.
Ctrl+U Paste the contents of the cutbuffer at the current cursor position.
Ctrl+A Move the cursor to the beginning of the current line.
Ctrl+E Move the cursor to the end of the current line.
Ctrl+W Search for a string or regular expression.
Ctrl+\ Replace a string or regular expression.
Ctrl+T Move to the preceding word in the current line.
Ctrl+F Move to the next word in the current line.
Alt+A Select text starting from the current cursor position.
Alt+6 Copy the selected text to the cutbuffer.
Alt+U Undo the last operation.
Alt+E Redo the last undone operation.

Now, let's take a look at how you use the nano text editor.

Getting started with the nano editor

In this section, I will walk you through all the basics you'd ever require to get started with the nano editor. Remember, this is not a tips and tricks tutorial but a basic guide.

1. Know the interface

When you open the nano editor by executing the nano command in the terminal, you'd met with the following output:

When you open the nano editor with or without the file, it shows the important shortcuts at the bottom of the editor. But there's a catch.

Unlike GUI editors that show the shortcut directly, the nano editor uses two symbols that are associated with every shortcut:

  • ^: The carrot symbol indicates the CTRL key.
  • M: Known as a meta key which represents the Alt key.

So if you want to use the ^G shortcut, you are required to press Ctrl + G whereas the M-A refers to pressing Alt + A.

2. Open or create files using the nano editor

To open a file with the nano editor, all you have to do is append the filename to the nano command as shown here:

nano <Filename>

For example, if I want to open mylogs.log file, then I'll be using the following:

nano mylogs.log

Alternatively, if the file does not exist, you can create a new file and write to it by appending the filename (that does not exist) to the nano editor and it will open an empty file where you make changes accordingly:

nano <NewFile>

For example, here, I created a new file named unknown.txt, wrote some lines and saved the changes:

Furthermore, if you don't want to decide the filename in the beginning, then you can start the nano editor by executing the nano command which will open an empty prompt where you can write lines.

Once done, when you save changes, it will ask you to enter the filename and there you can give it the filename.

For example, here, I wrote the greeting message in the nano editor and then saved the file by naming it Hello.txt:

3. Navigate through the file

This is the part where the nano editor shines. Unlike advanced text editors like Vim, you can simply use your arrow keys to navigate through the file.

So here, I will introduce some quick tips to navigate through the file faster ⚑.

  • Press Alt + \ to go to the beginning of the file
  • Press Alt + / to jump to the end of the file

You can also jump to a specific line. For that purpose, press Ctrl + _ and enter the number of the line:

4. Cut, copy, and paste in the nano editor

If you're using a desktop environment, you can select the text using the mouse and then press Ctrl + Shift + C to copy the selected text and paste it using Ctrl + Shift + V.

But what if you don't have access to GUI or you want to cut the text?

To cut, copy, and paste, you can use the nano editor's shortcuts but for that, you first have to select the text.

To select the text, first, use the navigation keys to the starting point from where you want to select the text, and then press Alt + A to set the marker.

Now, you can use the navigation keys to select the area that you want to copy or cut.

Looks complex? Here's how you do it:

Once you are done with the selection, you can have two choices:

  • Press Alt + 6 to copy the selected text
  • Press Ctrl + K to cut the text

After copying or cutting the text, press Ctrl + U to paste the text.

For example, here, I pasted the copied text at the end of the file:

5. Delete lines in the nano editor

Sadly, there's no dedicated option in the nano to delete the text or lines. But worry not, you can use the following ways to have a similar effect to deleting lines in the nano editor:

  1. Use backspace multiple times to remove words or lines.
  2. Cut the lines by pressing Ctrl + K.

To me, cutting lines seems the best option as I don't paste text very often and if you fall in the same category, it is the most efficient way to remove unwanted text.

But I have a hack to take things to the next level πŸš€.

You can select multiple lines or words and then cut them to get rid of them. This works so well when you want to delete multiple lines.

Sounds confusing? Here's how I removed multiple lines by following these steps:

  1. Went to the point where I wanted to remove lines using arrow keys.
  2. Pressed Alt + A to set the mark.
  3. Pressed Alt + / to go to the end of the file.
  4. Pressed Ctrl + K to cut the selected text.

6. Undo and redo

Undo refers to reverting your most recent activity whereas redo is undoing the last undo.

To undo and redo in the nano editor, you have to use the following shortcuts:

  • Alt + U: Undo the last action
  • Alt + E: Redo the last undo

For reference, in the below example, I removed a line from the file, did undo, and then redo:

7. Search and replace

To search and replace in the nano text editor, all you have to do is follow 3 simple steps:

  1. Press Ctrl + R and enter the term you want to search and then press Enter.
  2. Enter the term to replace with.
  3. It will show matching terms one by one asking which terms to replace and which to not. To replace all the matched results, press A.

For example, here, I replaced two instances of quick with lazy:

8. Save and exit from the nano editor

Saving and exiting from the nano editor is quite easy and can be done in two easy steps:

  1. Press Ctrl + O to save changes.
  2. Press Ctrl + X to exit from the editor.
πŸ“‹
You can also exit from the editor without saving changes by directly pressing Ctrl + X and then pressing N.

When you press Ctrl + O where it will ask you if you want to save changes with the given filename or if you want to use a different filename.

Once done, press the enter key and it will save the changes.

Another good thing about the nano editor is the way it notifies you of changes that are yet to be saved. Let me explain βœ‹.

Whenever you make changes to a file that is yet to be saved, it will show asterisk (*) next to the filename:

Once done, press Ctrl + X and it will close the nano editor.

But what if you want to close the nano editor without saving changes? In that case, follow the given steps:

  1. Press Ctrl + X
  2. Press N to exit the nano editor without saving the changes

9. Here's how to get help from the nano editor

Getting help from the nano editor can be helpful in tough times.

All you have to do is press Ctrl + G and it will bring a list of every shortcut available in the nano editor with a short description:

I use it to learn new shortcuts πŸ˜‰.

Cool tips and tricks of the nano editor

Did you know that you can enable the scrollbar in the nano editor? Or how about enabling mouse support?

Sounds cool, right? Well, we wrote a detailed guide covering 10 such tips and tricks of the nano editor to elevate the overall experience:

10 Tips to Get More Out of Nano Editor
Learn and use these tips and tricks to utilize lesser known Nano editor features.

You can also refer to our Nano tutorial series to learn the basics with more details:

Start Using Nano
Nano is perhaps one of the simplest terminal based text editor. While it is still keyboard shortcut based, the shortcuts are simpler here when compared to Vim and Emacs. Nano also displays appropriate shortcuts on the bottom all the time, making it even easier to use. You don’t need to

I hope you will find this guide helpful.

Sagar Sharma