User Tools

Site Tools


nano

The nano editor

The ‘nano’ editor is a GNU-project editor based on ‘pico’ a simple but easy to use editor created for the PINE email program for creating and editing emails. It has been expanded quite a bit: there is enhanced navigation, and search which include regex search.

nano cheatsheet

Nano, like ‘pico’, has Help. So if you start with the pico cheatsheet and read the Help, you'll get what you need.

A great addition, especially if you connect to SDF on a color terminal, is nano's use of “syntax” files, which provide syntax highlighting for several programming languages and related filetypes, such as HTML files, shell scripts, shell resource files (like .bashrc) and even the nano resource file .nanorc, where the settings for syntax file are kept.

Using GNU Nano

It's quite easy to use Nano to write anything you want.

To run Nano, enter in the terminal:

nano

You can indicate the name and extension of the file you want to edit:

nano file.txt

In the top bar you will find the version of the program, the name of the loaded file (if we have not loaded anything it will say “new buffer”). It will also inform you if the file has been modified with respect to the original loaded.

The main commands to unleash the potential of Nano are activated by using the Control key, which is abbreviated in Nano with the circumflex symbol (^). For example, to save a file, Nano tells us that the key combination to use is “^O”, so we press Ctrl+o.

Result Shortcut
Cut (current line or region) Ctrl+k
Paste Ctrl+u
Find Ctrl+w
Justify paragraph Ctrl+j
Justify all Esc,AltGr+j
Undo Esc,AltGr+u
Redo Esc,AltGr+e
Line numbering Esc,AltGr+n
Save Ctrl+o
Switch nano to background and return to the shell. Use fg to return to nano. Ctrl+t,Ctrl+z
Exit Nano Ctrl+x

To search for a text string, use Ctrl+w. With Ctrl+j will justify a long paragraph in several short lines (although it is not used in configuration files, it is very useful in any common text).

With Ctrl+G or F1 will provide Help screens with the rest of the commands. To exit Nano press Ctrl+x.

We also find the Meta commands (or “metas”, abbreviated in Nano with M-). On PC, you may access these Metacommands by pressing Escape key and AltGr, followed by the meta letter.

For example, to add right indentation to a paragraph is shown as M-}. Therefore, on a PC keyboard press Esc,AltGr+} to indent the paragraph.

Once you are familiar with Nano, you can get a less cluttered screen by removing its title bar with Shift+Alt+z and the help bar with Shift+Alt+x.

Nano Configuration

To configure Nano you can directly edit the ~/.nanorc file.

Nano Commands

File Handling
Ctrl+s Save current file
Ctrl+o Save as a file
Ctrl+r Insert a file into current one
Ctrl+x Close buffer and exit Nano
Editing
Ctrl+k Cut current line into buffer
Alt+6 Copy current line into buffer
Ctrl+u Paste contents of buffer
Ctrl+] Complete current word
Alt+3 Comment/Uncomment line/region
Alt+u Undo last action
Alt+e Redo last undone action
Search and Replace
Ctrl+f Start forward search
Ctrl+b Start backward seach
Alt+f Find next occurrence forward
Alt+b Find next occurence backweard
Alt+r Start a replacing session
Deletion
Ctrl+h Delete character before cursor
Ctrl+d Delete character under cursor
Alt+Bksp Delete Word to the left
Ctrl+Del Delete word to the right
Alt+Del Delete current line
Information
Ctrl+c Report cursor position
Esc,AltGr,d Report line/word/char counts
Ctrl+g Display help text
Miscellaneous
Ctrl+t Executes an external command.
Ctrl+t,Ctrl+s Run a spell check
Ctrl+t,Ctrl+y Run a sytax chec
Ctrl+t,Ctrl+o Run a formatter
Tab Indent marked region
Shift+Tab Unindent marked region
Esc,AltGr,A Set or unset the marker
Alt+v Enter next keystroke verbatim
Esc,AltGr,n Turn line numbers on/off
Esc,Altgr,P Turn visible whitespace on/off
Esc,AltGr,S Turn softwrapping on/off
Ctrl+L Refresh the schreen

How to Activate Syntax Hightlighting

To activate syntax highlighting, we are going to copy the default nanorc file to your user space and edit it. The steps are:

  1. copy the default file nanorc to your local as a resource file, .nanorc (yes, with the dot)
    cp /usr/pkg/share/nanorc ~/.nanorc
  2. edit the file
    nano ~/.nanorc
  3. in nano, search for the nanorc highlight line:
    1. press ctl-w, then type # include "/usr It will take you to a line that looks like:
      # include "/usr/pkg/share/nano/nanorc.nanorc"
  4. delete the ‘#’ at the start of the line
  5. use the arrow keys to go down the rest of the file, and remove more of the comment characters “#” at the start of “include” lines, especially
    1. # Cascading Style Sheets
      # include "/usr/pkg/share/nano/css.nanorc"
    2. and
      ## HTML
      # include "/usr/pkg/share/nano/html.nanorc"
    3. and
      ## PHP
      # include "/usr/pkg/share/nano/php.nanorc"
  6. Save the file with ctl-x and press ‘Y’ at the save prompt and press enter to confirm you are saving the file as .nanorc.

The next time you open nano on an HTML or other file (it keys off the file extension, like index.html), you should see the code hightlighted, which should help your editing significantly!

Nano Tips

Old Suspend Key

As of nano 6.4 suspend behavior was changed. Instead of ctl-z (also typed ^z, in the nano menu itself) being suspend as it was before, typing ^z warns you that you need to ^t^z if you ‘really’ mean to suspend.

  • You can reset to the old way by putting this in your .nanorc. That makes ^z suspend again.
    add this to .nanorc
    ## Allow nano to be suspended with ^z the old way, not ^t^z
    bind ^Z suspend main
nano.txt · Last modified: 2025/02/07 13:07 by papa