There is a readline
function that is probably not bound to a keystroke called kill-whole-line
that will kill the whole line as opposed to only killing the part before the cursor (unix-line-discard
which is somewhat of a misnomer and is bound to Ctrl-u).
You can bind that to any available keystroke. I like ShiftAlt-U since it's a related function. That keystroke may be bound to do-lowercase-version
which means it does whatever the unshifted version does (in this case upcase-word
). Since we don't really need two keys to do that, let's use that one.
You can try it out at the command line by creating the binding this way:
bind '"\eU":kill-whole-line'
or make it persistent by putting this line in your ~/.inputrc
file:
"\eU":kill-whole-line
To undo a Ctrl-u or ShiftAlt-U (or any operation that can be undone), press Ctrl-Shift-_ (underscore) or Ctrl-x Ctrl-u (two keystrokes). Or you can paste back in (yank) what you killed by pressing Ctrl-y which can be repeated if you want multiple copies of that text.
By the way, if you want to kill text from the cursor to the end of the line, you can press Ctrl-k. So instead of doing the binding described above, you can kill a whole line by pressing Ctrl-u Ctrl-k. Only the part killed by the second of those keystrokes will be saved in the kill buffer, but you can still do undo twice to recover them both.
How do you cause this in a script? – Jonny – 2014-06-20T03:17:58.183
Thanks! that did it. Is there a format list of keyboard shortcuts documented somewhere? – Paul Alexander – 2010-09-24T22:29:54.453
1http://www.ice2o.com/bash_quick_ref.html – ghoppe – 2010-09-24T22:30:52.660
2This worked for me in zshell too. – LandonSchropp – 2012-07-12T18:04:38.083
1This only works if your cursor is at the END of the line. For those situations where your cursor is at the beginning you need to use CTRL-K – Jarrod Nettles – 2013-03-10T05:51:38.460