Bash – the keyboard shortcut opposite to Ctrl+k

bashkeyboard shortcuts

Ctrl + K deletes rest of line in an UNIX command line. How do I delete all the text before the cursor?

Best Answer

Use Ctrl+u:

From bash documentation, Killing And Yanking:

unix-line-discard (C-u)

Kill backward from the cursor to the beginning of the current line.

Ctrl+u behavior is not only controlled by the shells that have their own line editor like bash, zsh, tcsh, sh -o emacs, but also by the line discipline of the terminal driver when in canonical mode (like in cat, or basic implementations of sh/ksh when no line-editor is enabled). Thus it works everywhere.

You can check line discipline of the terminal driver using stty -a (or stty -everything in BSD system):

$ stty -a
speed 38400 baud; rows 24; columns 80; line = 0;
intr = ^C; quit = ^\; erase = ^?; kill = ^U; eof = ^D; eol = M-^?; eol2 = M-^?;
swtch = M-^?; 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
Related Question