Search code examples
bashshelliterm2

How to turn off bash's "readline arguments" for character repetition?


In a bash shell, you can type M-<number> <key>, where M is the meta key, in order to repeat that <key> press <number> times. For example, typing M-6 a puts aaaaaa on your command line.

On my computer, meta is mapped to the Esc key. That means that a keystroke of Esc then 6 is interpreted as M-6, and makes the terminal expect another character to repeat:

(arg: 6)

This is problematic in my case. I use iTerm2 on macOS, and it has a wonderful little feature where it pops up an autocomplete window when you start typing a command with your previous command history. For example, if you use Mercurial and type hg update, it lets you select from all the previous bookmarks and commit hashes you've updated to in the past, sorted by most frequent.

The problem comes when I use Esc to dismiss this window. For example, if I start typing hg update , and then the autocomplete window comes up, and then I accidentally hit escape more than once, and then paste in a commit hash like 6dd0e54, what I actually end up with in the command line is this:

$ hg update ddddddd0e54

Why? Because the first Esc key press dismisses the autocomplete window, the second starts the M- key combination, and then the 6d in the pasted hash is interpreted as "repeat the d character 6 times."

This is infuriating, especially when dealing with more problematic commit hashes like 787075d: in this case it will literally put nearly 800 thousand d characters into my terminal, rendering it unusable.

So knowing that I never use these readline repetition arguments except by accident, is there any way of turning this feature off, ideally in bash but alternatively in iTerm2?


Solution

  • Running

    bind -p | grep '"\\e1"'
    

    returns

    "\e1": digit-argument
    

    So, just remove all the bindings to digit-argument by bind -r:

    for i in - {0..9} ; do
        bind -r '\e'$i
    done
    

    - is bound to digit-argument, too, to allow for negative arguments.