Quick Guide To nano Text Editor On The Raspberry Pi

6

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 :

nano Text Editor Screenshot #1

nano Main Screen

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) :

nano Text Editor Screenshot #2

nano Help Pages (CTRL+G)

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

To cut a line use CTRL+K. To copy a line use ALT+6.

To paste a line at the cursor position use 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.

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.

ControlMetaDescription
CTRL+GDisplay the help text
CTRL+XClose the current file buffer / Exit from nano
CTRL+OWrite the current file to disk
CTRL+RInsert another file into the current one
CTRL+WSearch for a string or a regular expression
ALT+WRepeat last search
CTRL+\\ALT+RReplace a string or a regular expression
CTRL+KCut the current line and store it in the cutbuffer
ALT+6Copy the current line and store it in the cutbuffer
CTRL+UPaste from the cutbuffer into the current line
ALT+TCut from the cursor position to the end of the file
ALT+AMark text at the cursor position
CTRL+CDisplay the position of the cursor
CTRL+_ALT+GGo to line and column number
CTRL+TInvoke the spell checker, if available
ALT+}Indent the current line
ALT+{Unindent the current line
CTRL+YMove to the previous screen
CTRL+VMove to the next screen
CTRL+FMove forward one character
CTRL+BMove back one character
CTRL+SpaceMove forward one word
ALT+SpaceMove back one word
CTRL+PMove to the previous line
CTRL+NMove to the next line
CTRL+AMove to the beginning of the current line
CTRL+EMove to the end of the current line
ALT+( or ALT+9Move to the beginning of the current paragraph
ALT+) or ALT+0Move 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+VInsert the next keystroke verbatim
CTRL+IInsert a tab at the cursor position
CTRL+MInsert a newline at the cursor position
CTRL+DDelete the character under the cursor
CTRL+HDelete the character to the left of the cursor
CTRL+JJustify the current paragraph
ALT+JJustify the entire file
ALT+DCount the number of words, lines, and characters
CTRL+LRefresh (redraw) the current screen
ALT+XHelp mode enable/disable
ALT+CConstant cursor position display enable/disable
ALT+OUse of one more line for editing enable/disable
ALT+SSmooth scrolling enable/disable
ALT+PWhitespace display enable/disable
ALT+YColor syntax highlighting enable/disable
ALT+HSmart home key enable/disable
ALT+IAuto indent enable/disable
ALT+KCut to end enable/disable
ALT+LLong line wrapping enable/disable
ALT+QConversion of typed tabs to spaces enable/disable
ALT+BBackup files enable/disable
ALT+FMultiple file buffers enable/disable
ALT+MMouse support enable/disable
ALT+NNo conversion from DOS/Mac format enable/disable
ALT+ZSuspension enable/disable

Note : Some operations have multiple short-cuts.

Share.

6 Comments

  1. 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…

  2. Seriously… This editor is a hog’s breath away from vi in terms of horrid usability curve. Alt-6, really????

Leave A Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.