3

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.)

1
  • Well, one reason not to is that it can be used in some shortcut sequences. I don't know exactly what they are called, which is why I landed here in search of a list of such sequences. One that I use a lot is Esc . which inserts the last parameter of the previous command at the current cursor position. – Superole Jul 28 '17 at 10:24
3

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.

7
  • interestingly, this affects the history - is there a way to get it not to? – Keith Hall Apr 22 '16 at 10:17
  • @KeithHall It shouldn't? What's happening exactly? The bind command will be stored in the history of course, but lines discarded by hitting ESC definetly shouldn't be stored. – kos Apr 22 '16 at 10:21
  • let's say I type ls ~, press Enter. Then, if I press the up arrow and then press Esc after having bound it, then the ls ~ no longer exists when I press up or down, and the output from history shows a blank entry. – Keith Hall Apr 22 '16 at 10:28
  • @KeithHall That looks like a bug: if you notice, the same happens with CTRL+U. Weird. – kos Apr 22 '16 at 10:39
  • so it does! I should have checked that first - sorry to have bothered you with it ;) I see that ctrl+k also updates the entry in the history - how annoying (for me)! I'll see if I can find any info about it - thanks for your time :) – Keith Hall Apr 22 '16 at 10:43
0

The only way i know is to show if there is a way to change the link of the keys. Maybe there is a setting of that in the terminal program you use.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.