Pressing esc in a terminal window

6,361

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.

Share:
6,361

Related videos on Youtube

Keith Hall
Author by

Keith Hall

BY DAY: .NET developer working on WCF services, Microsoft SQL Server databases and websites (some web forms, some MVC). My philosophy: automate everything! If a task is assigned to you as a "one-off", what's the bet that you'll be asked to do it again? So doing something manually isn't always quicker, and it's certainly less fun ;) Plus you learn more that way. In any case, save your work and notes, archive it in such a way that you can find it again :P BY NIGHT: maintainer of open source software, such as a couple of Sublime Text 3 plugins (Python)... FOR FUN: TF2 & Rocket League player My toolbox? LINQPad, Sublime Text, Clipboard Fusion, GitKraken

Updated on September 18, 2022

Comments

  • Keith Hall
    Keith Hall over 1 year

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

    • Superole
      Superole over 6 years
      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.
  • Keith Hall
    Keith Hall about 8 years
    interestingly, this affects the history - is there a way to get it not to?
  • kos
    kos about 8 years
    @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.
  • Keith Hall
    Keith Hall about 8 years
    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.
  • kos
    kos about 8 years
    @KeithHall That looks like a bug: if you notice, the same happens with CTRL+U. Weird.
  • Keith Hall
    Keith Hall about 8 years
    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 :)
  • kos
    kos about 8 years
    @KeithHall No problem, I never ever even noticed that, so I'm happy you made me aware of that. It's either an undocumented feature or a bug. Well I'll look into that myself as well, I'm curious at this point.
  • Keith Hall
    Keith Hall about 8 years
    I think it is intended - this answer says how to turn it off