Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

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.

share|improve this question

migrated from stackoverflow.com Oct 10 '12 at 17:19

This question came from our site for professional and enthusiast programmers.

2 Answers 2

up vote 48 down vote accepted

You just have to type ctrl+u.

Enjoy =)

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

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

share|improve this answer
2  
+1 for the useful info and reading –  StackUnder Oct 10 '12 at 19:53
    
Great explanation. –  Shashwat Kumar Oct 11 '12 at 17:37

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.