Index ¦ Archives

Key bindings in nano

Introduction

We're going to explain how to configure nano's key bindings here. A post about a simple text editor might seem unnecessary, but I still meet junior admins who only think about vim or emacs. But nano has its place - it's installed on just about every system I come across, it's easy to use and it's quite capable. And with a little effort it can be reconfigured to use Windows-style keystrokes.

nano is a character-mode text editor. It was originally a pico clone. pico was the editor in the e-mail client pine, which was a follow-on of elm. nano was once named tip which is a recursive acronym for tip isn't pico.

Links:
http://www.nano-editor.org/
https://www.nano-editor.org/dist/ # source

Installation

nano (usually a non-current version) is installed by default on Debian.

rnano (restricted nano) is a softlink to nano. When run as rnano, nano runs in restricted mode.

$ nano --version
 GNU nano version 2.2.6 (compiled 14:12:08, Oct  1 2012)
 (C) 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007,
 2008, 2009 Free Software Foundation, Inc.
 Email: nano@nano-editor.org    Web: http://www.nano-editor.org/
 Compiled options: --disable-wrapping-as-root --enable-color --enable-extra --enable-multibuffer --enable-nanorc --enable-utf8

Usage

nano's command line syntax is straightforward:

nano [<option> ...] <file> ...

nano presents an empty file buffer if no file is specified on the command line. Multiple files may be specified and edited if the multibuffer setting is on.

nano settings may be configured via command-line options or via an rc file using the set/unset commands. Key bindings may only be configured via an rc file using the bind/unbind commands. nano reads the rc file /etc/nanorc followed by ~/.nanorc.

A couple of things to note:

  • binary settings are unset by default
  • command-line options take precedence over commands in rc files;
  • rc files should be in Unix format i.e. LF as the line-terminator
  • an /etc/nanorc file is installed with nano

Not all command-line options are settings-related, e.g. the -I command-line option disables rc file processing and is useful for testing.

For configuring only a few settings the command line is sufficient, e.g. one could enable some useful settings by running nano -FSELT4.

-F (--multibuffer)
       Enable multiple file buffers, if available.
-S (--smooth)
       Enable smooth scrolling.  Text will scroll line-by-line, instead of the usual chunk-by-chunk behavior.
-E (--tabstospaces)
       Convert typed tabs to spaces.
-T cols (--tabsize=cols)
       Set the size (width) of a tab to cols columns.  The value of cols must be greater than 0.  The default value is 8.
-L (--nonewlines)
       Don't add newlines to the ends of files

If nano encounters an error in an rc file it will display e.g.:

Error in /etc/nanorc on line 341: Could not map name "wheris" to a function

Press Enter to continue starting nano.

Key Bindings

nano key bindings map keystrokes to nano functions. nano relies on normal keys as well as Ctrl and meta key combinations. The meta key is typically the Alt key. If Alt is not available for some reason, then the meta key is the Esc key. Some of the default nano key bindings are quite traditional, whereas others are rather idiosyncratic, but they are generally mnemonically based.

A couple of things to note:

  • the default key bindings are built into nano, i.e. they are not in the default rc file
  • some binary settings have related functions that toggle the value, e.g. nohelp and nohelp, const and constupdate, whitespace and whitespacedisplay
  • in Gnome Terminal, Alt is the menu key, but the keystroke is passed through to an application if it is not being used for a Gnome Terminal menu

Below are the default key bindings for the main menu section. In various menus and other contexts, Ctrl-x is typically used to exit and Ctrl-c to cancel.

ctl-r         read i.e. open file into current buffer, or into new buffer in multibuffer mode; press enter for new empty buffer
ctl-o         writeout i.e. save file
ctl-x         exit i.e. quit; also exits from buffer in multibuffer mode; prompts for writeout/save
ctl-g         get help/aid/assistance

enter, ctl-m  newline
bksp, ctl-h   delete previous character
del, ctl-d    delete current character
left, ctl-b   backward character
right, ctl-f  forward character
home, ctl-a   beginning of line
end, ctl-e    end of line
up, ctl-p     previous line
down, ctl-n   next line
pgup, ctl-y   previous page
pgdn, ctl-v   next page

m-space       previous word
ctl-space     next word
alt-\         beginning of file
alt-/         end of file
ctl-c         display cursor position
ctl-/         go i.e. jump to line and column

ctl-^         set/unset mark; or alt-a
alt-^         copy marked, or copy line if nothing marked; actually alt-6 i.e. do not need shift key
ctl-k         cut marked, or cut line if nothing marked, or cut to end of line if cut-to-end is enabled using alt-k
ctl-u         paste cut or copied
alt-t         cut to end of file

ctl-w         search
alt-w         search again
alt-r         search and replace

alt-<         previous file buffer; actually alt-, i.e. do not need shift key
alt->         next file buffer; actually alt-. i.e. do not need shift key

alt-x         toggle bottom help display
alt-y         toggle color syntax highlighting; colors configured via /usr/share/nano/*.nanorc files
alt-c         toggle cursor position display
alt-d         toggle dos/unix format option at writeout/save prompt
alt-k         toggle cut to end of line

ctl-t         show file list at read/open prompt
ctl-x         prompt for external command to execute at read/open prompt and insert output

ctl-z         suspend to shell; use fg to return

nano can be given a more mainstream user interface by appending a handful of lines to /etc/nanorc or ~/.nanorc; the strategy used is to leave the traditional cursor keys alone and rebind their ctl equivalents in more mainstream ways.

Add to the end of /etc/nanorc or ~/.nanorc:

# Local changes
set linenumbers
set multibuffer
set nohelp
set nonewlines
set quickblank
set regexp
set tabsize 4
set tabstospaces
set zap

unbind ^K main
unbind ^U main
unbind ^W main

bind ^O insert main
bind ^S savefile main
bind ^W writeout main
bind ^Q exit all
bind ^A help main

bind ^P prevword main
bind ^N nextword main
bind ^B firstline main
bind ^E lastline main
bind ^J gotoline main

bind ^Space mark main
bind ^X cut main
bind ^C copy main
bind ^V paste main

bind ^F whereis main
bind ^G findnext main
bind ^T findprevious main
bind ^H replace main

bind ^/ comment main

The above configuration yields:

ctl-o         read i.e. open file into current buffer, or into new buffer if multiple buffers are enabled
ctl-s         writeout i.e. save file
ctl-q         exit i.e. quit; also exits from a file buffer if multiple file buffers are enabled; prompts for writeout/save
ctl-a         get help/aid/assistance

enter, ctl-m  newline
bksp          delete previous character
del           delete current character
left          backward character
right         forward character
home          beginning of line
end           end of line
up            previous line
down          next line
pgup          previous page
pgdn          next page

ctl-p         previous word
ctl-n         next word
ctl-b, alt-\  beginning of file
ctl-e, alt-/  end of file
ctl-l         display cursor position
ctl-j         go i.e. jump to line and column

ctl-space     set/unset mark
ctl-c         copy marked, or copy line if nothing marked; actually alt-6 i.e. do not need shift key
ctl-x         cut marked, or cut line if nothing marked, or cut to end of line if cut-to-end is enabled using alt-k
ctl-v         paste cut or copied
ctl-d         cut to end of file

ctl-f         search
ctl-g         search again
ctl-h         search and replace

ctl-z         undo
ctl-y         redo

alt-<         previous file buffer; actually alt-, i.e. do not need shift key
alt->         next file buffer; actually alt-. i.e. do not need shift key

alt-a         toggle bottom help display
alt-y         toggle color syntax highlighting; colors configured via /usr/share/nano/*.nanorc files
alt-l         toggle cursor position display
alt-d         toggle dos/unix format option at writeout/save prompt
alt-k         toggle cut to end of line

ctl-t         show file list at read/open prompt
ctl-x         prompt for external command to execute at read/open prompt and insert output

alt-z         suspend to shell; use fg to return

And there we are. How to configure nano's key bindings.

© Keith Brown. Built using Pelican. Theme by Giulio Fidente on github. Member of the Internet Defense League.