Nano

In the last section you have created empty files using the command touch. Now, let’s learn how to put content into files using the popular text editor nano.

### Creates a directory to practice piping and redirection
username@bash:~$ mkdir nano_practice  
### Moves to the recently created directory
username@bash:~$ cd nano_practice  
### Creates a new file called nano_file1.txt and opens it with nano  
username@bash:~$ nano nano_file1.txt

When you create a file using nano, you will see the following home page:

  • The first line shows the version of the nano editor you are using (in this example, version 2.2.6) as well as the name of the file that is being edited.
  • The second line is where your file content begins. You can easily change its content by directly writing or deleting content. Notice that in this example nano_file1.txt is empty since we have just created it.
  • Finally, the last line shows some shortcuts that you will probably want to use while editing your file.

Editing the file

The process of putting data into the file is pretty straightforward. You just need to type the text you want to include. The Enter(↵) key will skip to the next line. And to delete text, just do like in any other text editor, using either Backspace(←){: .btn } or delete(DEL) keys. To navigate across the file, you can use the arrow keys ( ).

You can play around and add any text you want to:

Saving the changes

To save the changes that you have made to the file, use the shortcut Ctrl+O (always lower case!). Nano will ask the name you want to give to the modified version of the file. If you want to keep the original file name, just press Enter.

After saving the modified version, Nano will return to the first page. If you want to leave Nano, you need to use the shortcut Ctrl+X. If any modifications have been done between the last save action and the moment you try to leave Nano, it will ask you if you want to save the modifications into a new file:

There are some shortcuts that can significantly speed up the process of navigating through the lines of the file. Play around to better understand their effects:

Shortcut Effect
Ctrl+A Moves to the begin of current line
Ctrl+E Moves to the end of current line
Ctrl+Y Moves to the begin of file
Ctrl+V Moves to the end of file

Copying and pasting

In Nano, the first step before copying a text is to enter the Mark mode, which is the mode where you will highlight the text to be copied. Use the shortcut Ctrl+^ to turn on/off the Mark mode, which will be informed by Nano with a “Mark Set”/”Mark Unset” message:

Once Mark mode is on, you can highlight the text to be copied. Notice that the last character of the selection, which is highlighted in green, will not be copied:

Then, to copy the marked text, use the shortcut Meta+^ or Meta+6, where Meta usually is the Alt key on most keyboards. If otherwise you want to cut the marked text, use the shortcut Ctrl+K

Finally, use the shortcut Ctrl+U to paste the text:

Searching and replacing

Use the shortcut Ctrl+W to search for a string. It will open a Search box where you can type the string and then hit the Enter key to search for it across the file:

If you want to replace a string by another, use the command Ctrl+\. It will first open a box where you need to type in the text that will be replaced:

Then you will type the string that will replace:

And then once you hit Enter, Nano will ask if you want to replace the string that has been found. It will ask you this for every occurrence of the string, unless you choose to replace all occurrences of the string by answering A to the question Replace this instance?