83

One thing that annoys me using Linux's terminal is when I have to type a invisble password, like when you run ssh. Sometimes I mistype one or more letters and then I have to press backspace key a few times to make sure that I erased everything to start again.

I know this is not a big deal but since on the other day I found out that pressing CTRL+L "clears" the terminal I was wondering if there's a way to clear/erase the invisible password without relying on backspace or pressing enter key to try again.

2 Answers 2

119

I know this was already answered, but the answer doesn't explain what's going on.

Characters like Ctrl+L are handled by the application. In the case of entering input into the shell, like bash, it clears the screen. Some applications, like emacs, use it to redraw the screen and/or recenter the cursor.

Characters like Ctrl+U at a password prompt are handled by the TTY line discipline. They'll work any time the input terminal is in the "canonical" line-editing mode, which is what mode your programs are in while you're entering a password. In this mode, it's the Linux kernel itself that is interpreting these characters. You can see what functions are assigned to which keys in the output of stty -a:

speed 38400 baud; rows 45; columns 128; line = 0;
intr = ^C; quit = ^\; erase = ^?; kill = ^U; eof = ^D; eol = <undef>; eol2 = <undef>; swtch = <undef>; start = ^Q; stop = ^S;
susp = ^Z; rprnt = ^R; werase = ^W; lnext = ^V; flush = ^O; min = 1; time = 0;
-parenb -parodd cs8 -hupcl -cstopb cread -clocal -crtscts
-ignbrk -brkint -ignpar -parmrk -inpck -istrip -inlcr -igncr icrnl ixon -ixoff -iuclc -ixany -imaxbel iutf8
opost -olcuc -ocrnl onlcr -onocr -onlret -ofill -ofdel nl0 cr0 tab0 bs0 vt0 ff0
isig icanon iexten echo echoe echok -echonl -noflsh -xcase -tostop -echoprt echoctl echoke

Here, you can see that Ctrl+U is tied to kill, which (according to the stty man page) will "erase the current line". You could also use stty to change this mapping:

stty kill ^E   # make ctrl-e erase the line

This page has a lot of great information about the TTY layer if you're interested.


If things weren't confusing enough: when at the prompt of an interactive bash, you're actually not in the "canonical" line-editing mode. Instead, bash is receiving each character directly as you type it, and interpreting the control sequences itself, through the readline library. This library will honor the mappings you've set on your terminal device, though, so you can still use stty to change them. Or, you can customize them independently inside bash and other readline-based programs by setting the unix-line-discard property inside your ~/.inputrc file. See the bash man page for more info.

When you run a program, bash re-enables canonical line editing mode (stty icanon) if it was enabled before, and then disables it again when the program finishes (stty -icanon).

1
  • 1
    Thanks, that helped me to trace down and fix my tilda / yay 'erase' issue!
    – tafaust
    Aug 10, 2019 at 7:01
82

You just have to type ctrl+u.

Enjoy =)

3
  • Never knew about that. Usually I'd just ctrl+C out of an unwanted command mid-typing.
    – Spidey
    Oct 10, 2012 at 17:30
  • 12
    See my explanation in the other answer if you want to know what's going on.
    – Jim Paris
    Oct 10, 2012 at 19:47
  • I'm using Ctrl+W and pretty OK with it.
    – shabunc
    Oct 11, 2012 at 19:38

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .