Can I setup less program to exit from it by pressing ESC key?
2 Answers
To bind Esc+Esc to quit with lesskey, do the following:
- Create a
~/.lesskeyfile with the line:
\e\e quit
Run
lesskey. This will create a binary~/.lessfile used byless.Use
lessas 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.
-
Nice one! This works well!Sparhawk– Sparhawk2015-02-07 13:46:14 +00:00Commented Feb 7, 2015 at 13:46
-
I had just found this while poking around in the source only to realize this answer was here already. I really appreciate that the consideration the developers have shown for the user by including
lesskey.iyrin– iyrin2015-02-08 00:17:53 +00:00Commented Feb 8, 2015 at 0:17 -
Lesskey is not available on MacOS old BSD fork...sorin– sorin2025-06-26 16:17:47 +00:00Commented Jun 26 at 16:17
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.
-
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– Artur Eshenbrener2015-02-07 09:19:16 +00:00Commented Feb 7, 2015 at 9:19 -
Terminal control sequences starting with
ESCare 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 shtrom.ssji.net/skb/getc.htmlStephen Kitt– Stephen Kitt2015-02-07 09:30:10 +00:00Commented Feb 7, 2015 at 9:30 -
1@ArturEshenbrener You can bind to ESC+ESC with
\e\e. See the lesskey(1) man page.vinc17– vinc172015-02-07 09:33:22 +00:00Commented Feb 7, 2015 at 9:33 -
@StephenKitt terminal control sequences starting with
ESCare 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– Celada2015-02-07 09:36:54 +00:00Commented Feb 7, 2015 at 9:36 -
1@Celada See my answer on how to use
lesskey.vinc17– vinc172015-02-07 13:21:55 +00:00Commented Feb 7, 2015 at 13:21