Can I setup less
program to exit from it by pressing ESC
key?

- 829,060
2 Answers
To bind Esc+Esc to quit
with lesskey
, do the following:
- Create a
~/.lesskey
file with the line:
\e\e quit
Run
lesskey
. This will create a binary~/.less
file used byless
.Use
less
as usual. Esc+Esc will do aquit
.
If you no longer want your bindings, you can remove the ~/.less
file.
For more details, see man lesskey
or lesskey.nro
in the less
package source.
Debian -- Details of source package less in wheezy
SYNOPSIS
lesskey [-o output] [--] [input]
The input file is a text file which describes the key bindings.
If the input file is "-", standard input is read.
If no input file is specified, a standard filename is used
as the name of the input file, which depends on the system being used:
On Unix systems, $HOME/.lesskey is used;
on MS-DOS systems, $HOME/_lesskey is used;
and on OS/2 systems $HOME/lesskey.ini is used,
or $INIT/lesskey.ini if $HOME is undefined.
Not really.
less
has many functions bound to ESC + something else. See the manpage: ESCv, ESCn, ESCF, many others. You wouldn't be able to type any of those is less
exited upon receiving just ESC.
Concievably, you could rebind all those functions to other keystrokes or live with being unable to type them, and rebind ESC to quitting, but it still wouldn't be a good idea.
It's not a good choice of key to use. In VT100 (the terminal emulation used basically everywhere these days) all terminal control sequences begin with the ASCII ESC
character. This means that every time you press something like an arrow key, your terminal sends ESC
followed by other stuff. When you press the ESC key, your terminal also sends ESC
, but it's not followed by anything. How can software tell the difference after having received an ASCII ESC
character? It is forced to wait a some time to see if anything follows the ESC
. So things bound to ESC alone always incur a delay, a time lag before the software can respond.

- 44,132
-
1It a really good explanation. As you said, many functions of less bound to
esc
+something else. It would be cool to setup quit from less by pressingEsc
+Esc
. Is it possible? – Artur Eshenbrener Feb 07 '15 at 09:19 -
Terminal control sequences starting with
ESC
are sent to the terminal, not received from it. Pressing the Escape key adds keycode 27 to the input stream immediately... You can test this yourself with the code at http://shtrom.ssji.net/skb/getc.html – Stephen Kitt Feb 07 '15 at 09:30 -
1@ArturEshenbrener You can bind to ESC+ESC with
\e\e
. See the lesskey(1) man page. – vinc17 Feb 07 '15 at 09:33 -
@StephenKitt terminal control sequences starting with
ESC
are both sent to the terminal and received from the terminal. An example of from the terminal is an arrow key. An example of to the terminal is an instruction to move the cursor. – Celada Feb 07 '15 at 09:36 -
Oops, you're right about arrow keys, sorry. Handling Escape doesn't necessarily involve much of a delay though (see how Emacs handles Esc-X vs. arrows for example). – Stephen Kitt Feb 07 '15 at 09:42
-
Double
esc
does the job. Thank you. Think that it could be added to your answer. – Artur Eshenbrener Feb 07 '15 at 10:11 -
@ArturEshenbrener You know what? I looked into
lesskey
before I wrote my answer and I couldn't figure out how to use it. I just kept on gettingCannot use lesskey file ".lesskey"
. If you got it working and got it to bind ESC+ESC then I encourage you to add your own self-answer with the method or edit the information into mine; your choice. – Celada Feb 07 '15 at 10:25 -
1
lesskey
. – iyrin Feb 08 '15 at 00:17