23

In a virtual terminal I (accidentally) pressed Win+1 and got a new line consisting of

(arg: 1)

As long as I keep Win pressed, all numbers I press get appended to the number in the parentheses, e.g. Win and 1+4 yields (arg:14).

When I (after releasing Win) press any key, that character is printed as many times as the number reads and the (arg: ...) expression is replaced by the standard prompt.

Does anybody know what happens here?

27

That’s a digit argument, a feature of the readline library built into bash:

digit-argument (M-0, M-1, ..., M--)
    Add  this digit to the argument already accumulating, or start a
    new argument. M-- starts a negative argument.

This feature makes it simpler to add or delete text in the currently edited command line. Let’s say you want to write echo gggg:

  1. Enter echo followed by space
  2. Press the Meta key (which may be Win, Alt and/or Esc for you) and type 4g

But how often do you need to type a single letter so many times that this would save you anything? The feature is primarily designed for readline commands, see chapters 8.2.1–8.2.3 of the Reference Manual (“C” meaning Ctrl, “M” meaning Meta).

You can move back one word with Meta and B, but what if you wanted to move back multiple words at once? You can either hold Alt and press B multiple times, or use the digit argument:

  1. There’s a typo on your command line five words back:

    $ echo one two tree four five six seven█
    
  2. Press the Meta key, type 5 and press Meta and B. Et violà:

    $ echo one two █ree four five six seven
    
  3. Fix the typo and either press Enter to shoot or End or Ctrl+E to move to the end of the line again.

You can delete a single word by pressing Meta and Backspace, but what if you wanted to delete multiple words at once?

  1. You forgot a word on your command line:

    $ echo one two four five six seven█
    
  2. Press the Meta key, type 4 and press Meta and Backspace.

    $ echo one two █
    
  3. Type the missing word followed by space and press Ctrl+Y to paste what was just deleted. Et violà:

    $ echo one two three four five six seven█
    

You’re right, you could’ve also moved to there as just explained without deleting anything. We’re on Linux, there are always several ways of doing the same thing.

And what’s a “negative argument”? It does the same, but in the opposite direction, so Meta -2 Meta Backspace deletes the next two words of the command line. “But one could use Meta 1 Meta D to do the same thing!” Glad you got that, welcome to your command line.

Further reading:

| improve this answer | |
  • What bash version did this come out in ? Looks really useful. +1ed. – Sergiy Kolodyazhnyy Jun 9 '18 at 22:24
  • on WSL it's Esc – αғsнιη Jun 10 '18 at 7:55
  • 1
    Wow! I am glad to have given you the inducement for such a great answer!! – guntbert Jun 10 '18 at 15:05
  • There is nothing to "admit", often when we are talking about things we know already we ('I') tend to not do the research again. Your research makes this answer so comprehensive - including sources. – guntbert Jun 10 '18 at 15:48
  • 1
    What's neat is that digit-argument works, mutatis mutandis, in vi-like editing mode (set -o vi), too. – Geremia Dec 14 '18 at 4:16

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.