If I need to edit text files directly on my Raspberry Pi my text editor of choice is nano. There are other text editors available but I prefer nano’s relatively straightforward interface.
As a command line based utility it may feel strange for users who are more familiar with a graphical interface but it is easy to learn the basics. Syntax colouring is available which makes reading and reviewing scripts easy.
Launching nano
To start nano you can simply type nano at the command prompt. This will launch with a “new buffer”. In other words an empty text file that has no name.
In order to launch an existing text file you type nano followed by the file name :
nano berryclip_01.py
You will see something like this :
If your file is not in the current directory you will need to specify the full path :
nano /home/pi/berrryclip/berryclip_01.py
If the file is a system file you may need to use elevated permissions :
sudo nano -w /etc/fstab
Note: Always use the -w switch when opening system files. It disables wrapping long lines and ensures the file isn’t modified in a way that may affect your system.
Editing Text
Once in nano you can start typing. Additional functions can be activated by using the CTRL or ESC keys. These short-cuts are listed at the end of this post. You can list these keys while in nano using CTRL+G (Press and hold the CTRL key then press the G key) :
Return to the main screen using CTRL+X.
To get you started here is a brief summary of nano’s most useful features.
Saving and Quiting
To save a file you can use CTRL+O.
To quit nano you can use CTRL+X. You will be prompted to save your file if it has changed and you can answer this prompt with either a Y or a N. If you’ve changed your mind at this point you can return the main edit screen using CTRL+C.
Cutting and Pasting
Cut a line using CTRL+K. To copy a line use ALT+6.
Paste a line at the cursor position using CTRL+U.
To cut or copy multiple lines use CTRL+K or ALT+6 on each line and then paste them all using a single CTRL+U. They are pasted in the order that you cut/copied them. Using CTRL+U again will paste another set of text if required.
You can also mark a block of text before cutting. Position your cursor at the start of the text and press CTRL+6. Then position your cursor after the text and use CTRL+K or ALT+6 to cut or copy. CTRL+U to paste.
Searching and Replacing Text
To search for text use CTRL+W. Enter the text you need to find and press Enter. To repeat the search use ALT+W.
To perform a search and replace use ALT+R.
You can’t open files once nano is running. Although you can insert a file into the current buffer you can not open a file directly once you are in nano. You need to launch it from the command line. I find this quite strange. If you need to edit a number of files you need to quit and then load each one in turn from the command line.
Key Short-cut Summary
For some reason the official nano website keeps the nano “Control” and “Meta” short-cuts a closely guarded secret so I have listed them here.
Control | Meta | Description |
---|---|---|
CTRL+G | Display the help text | |
CTRL+X | Close the current file buffer / Exit from nano | |
CTRL+O | Write the current file to disk | |
CTRL+R | Insert another file into the current one | |
CTRL+W | Search for a string or a regular expression | |
ALT+W | Repeat last search | |
CTRL+\\ | ALT+R | Replace a string or a regular expression |
Control | Meta | Description |
---|---|---|
CTRL+K | Cut the current line and store it in the cutbuffer | |
ALT+6 | Copy the current line and store it in the cutbuffer | |
CTRL+U | Paste from the cutbuffer into the current line | |
ALT+T | Cut from the cursor position to the end of the file | |
ALT+A | Mark text at the cursor position | |
CTRL+C | Display the position of the cursor | |
CTRL+_ | ALT+G | Go to line and column number |
CTRL+T | Invoke the spell checker, if available | |
ALT+} | Indent the current line | |
ALT+{ | Unindent the current line |
Control | Meta | Description |
---|---|---|
CTRL+Y | Move to the previous screen | |
CTRL+V | Move to the next screen | |
CTRL+F | Move forward one character | |
CTRL+B | Move back one character | |
CTRL+Space | Move forward one word | |
ALT+Space | Move back one word | |
CTRL+P | Move to the previous line | |
CTRL+N | Move to the next line | |
CTRL+A | Move to the beginning of the current line | |
CTRL+E | Move to the end of the current line | |
ALT+( or ALT+9 | Move to the beginning of the current paragraph | |
ALT+) or ALT+0 | Move to the end of the current paragraph | |
ALT+\\ or ALT+| | Move to the first line of the file | |
ALT+/ or ALT+? | Move to the last line of the file | |
ALT+] | Move to the matching bracket | |
ALT+- or ALT+_ | Scroll up one line without scrolling the cursor | |
ALT++ or ALT+= | Scroll down one line without scrolling the cursor | |
ALT+< or ALT+, | Switch to the previous file buffer | |
ALT+> or ALT+. | Switch to the next file buffer | |
ALT+V | Insert the next keystroke verbatim | |
CTRL+I | Insert a tab at the cursor position | |
CTRL+M | Insert a newline at the cursor position | |
CTRL+D | Delete the character under the cursor | |
CTRL+H | Delete the character to the left of the cursor |
Control | Meta | Description |
---|---|---|
CTRL+J | Justify the current paragraph | |
ALT+J | Justify the entire file | |
ALT+D | Count the number of words, lines, and characters | |
CTRL+L | Refresh (redraw) the current screen | |
ALT+X | Help mode enable/disable | |
ALT+C | Constant cursor position display enable/disable | |
ALT+O | Use of one more line for editing enable/disable | |
ALT+S | Smooth scrolling enable/disable | |
ALT+P | Whitespace display enable/disable | |
ALT+Y | Color syntax highlighting enable/disable | |
ALT+H | Smart home key enable/disable | |
ALT+I | Auto indent enable/disable | |
ALT+K | Cut to end enable/disable | |
ALT+L | Long line wrapping enable/disable | |
ALT+Q | Conversion of typed tabs to spaces enable/disable | |
ALT+B | Backup files enable/disable | |
ALT+F | Multiple file buffers enable/disable | |
ALT+M | Mouse support enable/disable | |
ALT+N | No conversion from DOS/Mac format enable/disable | |
ALT+Z | Suspension enable/disable |
Note : Some operations have multiple short-cuts.
The official GNU nano editor homepage : https://www.nano-editor.org/
6 Comments
vi
Thanks for this. I’ve always found nano a bit too hard to get into, having used emacs many years ago, and I’m now used to emacs commands. That said, I have to recommend zile (zile is like emacs). A full emacs implementation is a bit big for a Pi, but I find zile to be just as good. Not wanting to start a religious war…
VI much better.
and much more difficult
Seriously… This editor is a hog’s breath away from vi in terms of horrid usability curve. Alt-6, really????
It is a very easy use text editor but it is easier just to use leafpad.