Ubuntu – Pressing esc in a terminal window

command linekeyboardshortcut-keys

In a Terminal (whether it be UXTerm, Mate Terminal, or a non-X/window system based terminal (not sure if it has an official name) like you get by pressing Ctrl+Alt+F1), if you press the Up arrow, you can cycle through the history of previously executed commands.

If you then decide you want to execute something completely different, you have to press Ctrl+U to clear the line, as mentioned here. In MS-DOS or Windows' Command Prompt, one can simply press Esc.

Is there a reason why the Esc key isn't set by default in Ubuntu Terminals to perform the same action as Ctrl+U? Is there a way to enable such functionality? I really want to not have to remember a different shortcut key just because I am typing in a different OS.

(I believe my question is not a duplicate of the one whose answer I linked to, because that one is asking for the equivalent shortcut key in Ubuntu, and I am asking for a way to use Esc or for a reason why it would be a bad idea to set it up this way.)

Best Answer

You can bind ESC to the Readline function unix-line-discard:

bind '"\e": unix-line-discard'

To make the change permanent, add the command to ~/.xinputrc:

printf "bind '\"\\\e\": unix-line-discard'\n" >>~/.xinputrc

And source ~/.xinputrc in ~/.bashrc:

printf 'source ~/.xinputrc\n' >>~/.bashrc

To load the change also in the current shell, source ~/.xinputrc:

source ~/.xinputrc

This is a Bash feature, so it will work across any terminal, unless the terminal is hijacking the ESC key itself, for example if ESC is part of a terminal-specific keystroke, in which case you'll have to disable the offending keystroke in the terminal.

See also help bind and bind -P for a list of the available Readline functions and the keys / keystrokes they're currently bound to.