How do I input n repetitions of a digit in bash, interactively

Try

echo Alt+1Alt+6Ctrl+V0

That's 6 key strokes (assuming a US/UK QWERTY keyboard at least) to insert those 16 zeros (you can hold Alt for both 1 and 6).

You could also use the standard vi mode (set -o vi) and type:

echo 0Escx16p

(also 6 key strokes).

The emacs mode equivalent and that could be used to repeat more than a single character (echo 0Ctrl+WAlt+1Alt+6Ctrl+Y) works in zsh, but not in bash.

All those will also work with zsh (and tcsh where that comes from). With zsh, you could also use padding variable expansion flags and expand them with Tab:

echo ${(l:16::0:)}Tab

(A lot more keystrokes obviously).

With bash, you can also have bash expand your $(printf '0%.0s' {1..16}) with Ctrl+Alt+E. Note though that it will expand everything (not globs though) on the line.

To play the game of the least number of key strokes, you could bind to some key a widget that expands <some-number>X to X repeated <some-number> times. And have <some-number> in base 36 to even further reduce it.

With zsh (bound to F8):

repeat-string() {
  REPLY=
  repeat $1 REPLY+=$2
}
expand-repeat() {
  emulate -L zsh
  set -o rematchpcre
  local match mbegin mend MATCH MBEGIN MEND REPLY
  if [[ $LBUFFER =~ '^(.*?)([[:alnum:]]+)(.)$' ]]; then
    repeat-string $((36#$match[2])) $match[3]
    LBUFFER=$match[1]$REPLY
  else
    return 1
  fi
}
zle -N expand-repeat
bindkey "$terminfo[kf8]" expand-repeat

Then, for 16 zeros, you type:

echo g0F8

(3 keystrokes) where g is 16 in base 36.

Now we can further reduce it to one key that inserts those 16 zeros, though that would be cheating. We could bind F2 to two 0s (or two $STRING, 0 by default), F3 to 3 0s, F1F6 to 16 0s... up to 19... possibilities are endless when you can define arbitrary widgets.

Maybe I should mention that if you press and hold the 0 key, you can insert as many zeros as you want with just one keystroke :-)


Presuming your terminal emulator doesn't eat the keystroke (e.g., to switch tabs)—plain xterm works—you can press Alt-1 6 Ctrl-V 0. The control-V is needed in there to break up the number and the character to repeat (if you were repeating a letter, you wouldn't need it).

(This is a readline feature known as digit-argument, so you should be able to use bind to change the modifier keys involved)


In Emacs, I'd normally use C-q to separate prefix argument from digit input.

Bash uses Ctrl+v instead of C-q, to avoid problems with XON/XOFF flow control.

So you can use Alt+1 Alt+6 Ctrl+v 0.