We continue with the entries for Raspberry Pi and Linux. This time we are going to delve into the Nano text editor, an old inseparable friend that will accompany us in most Linux distributions, and with which we must familiarize ourselves.
GNU Nano is a simple text editor mostly controlled by keyboard. It is especially suitable for editing files (for example, configuration files) from the command line or from SSH, where a mouse is not always available.
The first version of Nano dates back to 1999. It was developed to resemble Pico, the editor of a popular email client of the time called Pine. Initially Nano was called TIP (TIP Isn’t Pico). Later it changed its name to Nano, playing with SI prefixes, since “nano is 1000 times greater than pico”.
In February 2001 Nano became part of the GNU Project and became part of the free software. It is the integrated text editor in most Linux distributions, such as Debian or Ubuntu, which includes Raspbian for Raspberry Pi.
Although it is not the only available alternative (some with many followers like VIM), it is almost inevitable that at some point you will use Nano to quickly edit a text. Fortunately, it is a simple and very easy to use program.
However, despite its simplicity, Nano has more options and tools than it seems at first. Therefore, and since it is a tool that we will end up using frequently, it is advisable that we delve a little into its use and lose our fear of it.
The Nano interface has 4 main sections:
- The top bar shows information about the edited file
- The main section where we edit the text
- At the bottom we have the status bar, which shows messages to the user
- The last two lines show keyboard shortcuts used in the editor
Help in Nano
As with many tools in Linux, Nano offers different aids to make its use easier. The first way to get help is before launching Nano, by running the following command:
It will show us a very long list of options that we can use to launch Nano and modify its behavior.
On the other hand, within Nano we can use the key combination:
Ctrl+G #Show help
And it will show us another very long list of options, commands, and shortcuts that we can use in Nano.
Keep in mind that it is not about knowing all the options by heart, but about knowing that they exist and being able to find them quickly if we need them.
If you are already very advanced users (congratulations!), you can hide the shortcut bar with the key combination:
Alt+X #Activate/deactivate shortcut bar
You can reactivate the bar with the same key combination.
Using commands in Nano
In Nano commands are executed using keyboard combinations. We have combinations with the Ctrl key (indicated as ^) and with the Alt key (indicated as M-, Meta).
In this post we will see the keyboard shortcuts converted to Ctrl+ and Alt+. Although purists of Linux might have a heart attack at not seeing ^ and M-, the view of others appreciates it :)
As an alternative to the Ctrl and Alt keys in Nano, the Esc key can be used. In this case, it is not necessary to press the key combination simultaneously.
- Pressing Esc and then ‘key’ is equivalent to pressing Ctrl+‘key’
- Pressing Esc twice and then ‘key’ is equivalent to pressing Alt+‘key’
Create or open a file
To open or create a file, in a command line we write:
For example, if we write:
We will open a new file called ‘hello.txt’ if it does not exist. If it already exists, we will open it for editing.
Remember that we can use the Tab key to complete file paths in the command line.
When opening a file we can use the -B modifier to open in Backup mode. nano -B When saving the file, the previous version will be saved as “file-name~“. For example, if we use:
nano -B hello.txt
When saving the file, a copy will be automatically saved as “hello.txt~“.
If we have opened a file without the Backup option, it is possible to activate/deactivate it at any time by pressing the keys
Close Nano and save files
To save a file (with the same or a different name) we use the keyboard shortcut:
Ctrl + O #Save/Save as
Nano will ask for the file name, and we press Enter to accept, or Ctrl+C to cancel. Check the shortcut bar for more combinations.
To close Nano, we press:
Ctrl + X #Close Nano
Which closes Nano and returns to the command line. However, if there are unsaved changes in the file before closing it, it will ask us if we want to save changes, with options similar to ‘Ctrl+O’.
We can move around the text editing window using the usual options, cursor keys, Page Up and Page Down keys, and Home and End keys.
But there are also many Nano-specific keyboard shortcuts for navigation. Although in most cases we will use the previous keys, we could not make a post without mentioning them at least.
Ctrl+A #Go to the beginning of the current line
Ctrl+E #Go to the end of the current line
Ctrl+Y #Go to the previous screen
Ctrl+V #Go to the next screen
Alt+\ #Go to the first line of the file
Alt+/ #Go to the last line of the file
Alt+( #Go to the beginning of the paragraph; then to the beginning of the previous paragraph
Alt+) #Go to the end of the paragraph; then to the end of the next paragraph
Alt+] #Go to the corresponding brace
Select, cut/copy and paste
Of course, in Nano it is possible to cut, copy, and paste text, although the operation and keyboard shortcuts are somewhat different from what we are used to in other programs.
First, we have to select text. To do this, we position ourselves at the first character we want to select and press:
Alt+A #Start selection
Now, we move to the final position, and Nano will highlight the selected text. The selected text does not include the character under the cursor.
To ‘cut’/‘copy’ and ‘paste’, we press one of the following keyboard shortcuts:
In Nano, ‘cutting’ and ‘pasting’ is more common than ‘copying’. In fact, you can see that the ‘copy’ shortcut is different from ‘cut’ or ‘paste’. That’s why many times ‘cut’ is followed by ‘paste’, instead of ‘copy’.
Another curiosity is that, if we use ‘cut’ without having a selection, we will cut the entire line. In addition, if we use ‘cut’ multiple times without moving the cursor, we will add the lines to the clipboard. When pasting them, we will add all the lines from the clipboard.
Using the mouse to select
Selecting text only with the keyboard is quite cumbersome. When we do not have a mouse available, we have no choice, but often we will have a mouse available.
A less known trick is that it is possible to activate/deactivate mouse support by pressing:
Alt+M #Activate/deactivate mouse support
Now we can double-click on the first character we want to select, and click again on the last one. Now that we have the text selected, we can cut or copy the text as we have seen in the previous section.
A very useful function when working with text files such as configuration files, logs, etc., is to combine two or more files. Thus, it is possible to insert the content of a file into the current file by pressing:
It will ask us for the name of the file we want to insert, and after pressing Enter, Nano will insert the file at the current cursor position.
Another way to combine files is when saving the file (pressing Ctrl+X or Ctrl+O) using the shortcuts:
Alt+A #Add file before the current one
Alt+P #Add file after the current one
Search and replace
Of course, in Nano it is possible to search for text within the current file. To do this, we press:
Ctrl + W #Search text
And we write the text to search for.
If we want to repeat the search for all occurrences in the file, we press repeatedly:
Alt + W #Search next occurrence
If instead we want to search and replace a text with another, we use:
Alt+R #Search and replace
We write the text we want to search for and then the one we want to replace. In the bottom menu, we have the options to replace the occurrences:
Alt+S #replace current occurrence and move to the next one
Alt+N #ignore current occurrence and move to the next one
Alt+A #replace all occurrences
Working with rows and columns
Sometimes it is useful to work with the lines and characters of a text document. If we want to see the row and column of the current cursor position, we use the combination:
Alt+C #See the row and column of the cursor
The status bar will show us the data about the cursor position, selection size, and document size.
If we want to move the cursor to a specific column and row, we press:
Alt+G #Move the cursor to a row and column
And we write the line and column we want to go to.
It is also possible to open a file directly on a line and column with the following command:
nano +line,column [file-name]
On the other hand, we can count the number of lines, words, and characters in the file by pressing
Alt+D #Show line, word, and character count
Working with indented lines
Nano has tools to facilitate working with indented text files (i.e., files with indented lines). This is especially useful in code files, Json, etc.
The first tool is the smart home key, which we can activate or deactivate by pressing:
Alt+H #Activate/deactivate smart home key
When it is activated, pressing the ‘Home’ key will move the cursor to the origin position of the upper line, instead of to the beginning of the line.
On the other hand, another very useful tool is the automatic indentation, which we can activate or deactivate by pressing:
Alt+I #Activate/deactivate automatic indentation
When automatic indentation is activated, pressing the ‘Enter’ key will position the cursor in the same position as the previous line, instead of at the beginning of the line.
Working with long lines
Sometimes we will have to work with lines that are wider than the screen. In these cases, Nano also includes functions to help us in our work. First, if a line “goes off” the screen to the left or right, Nano indicates it with the symbol ’$’ at the end or beginning of the line, respectively.
On the other hand, we have two options for handling long lines. In the first case, the “soft” line wrap, which is activated with the key combination:
Alt+$ #Activate/deactivate soft line wrap
In this case, Nano displays long lines in several lines, separating when they do not fit on the screen. This separation (wrapping) is only visual, but the line remains just as long. Therefore, we can activate or deactivate it at any time.
On the other hand, we also have the “hard” line wrap, which is activated with the shortcut:
Alt+L #Activate/deactivate hard line wrap
It also divides long lines into several lines that fit on the screen, but in this case, the change is actually made in the text, inserting line breaks. In this case, the change cannot be undone because the line has actually been changed.
Putting Nano in the background
As we already saw in the post on advanced use of Bash, it is possible to put a process in the background, and Nano is no exception. To put Nano in the background we press:
Ctrl+Z #Put process in the background
We can continue using the command line and when we want to recover Nano we run the command:
fg #Reactivate process in the background
If we want to prevent Nano from being able to go into the background (because you keep accidentally pressing the key, for example) we can enable or disable it by pressing:
Alt+Z #Activate/deactivate suspension
Of course, there are many more functions, but as we said it is not a matter of knowing them all. The important thing is to know that they are there, and that by taking a look at the bottom bar or the help we can remember them.
In the use of Linux, you will frequently use Nano, as well as the command line and SSH. So, you know! Get used to this simple but powerful tool available on Linux systems such as Raspbian on Raspberry Pi.