-3

Sometimes, I would type in some shell command in my console before I decide not to execute it. For example, I type something below in my console terminal:

sudo rm -r /   # not yet committed! Thank God

Well, I realize it is kind of dumb and decide to delete before real execution.

But, I am lazy, I don't want to press the delete and wait for the backspacing cursor. The truth is this kind of situation is somehow normal to me.

I want to quickly delete the shell command with as less keyboard pressing as possible. Is there any solutions?

Thanks~

Eric Zheng
  • 1,084
  • 1
  • 11
  • 23

1 Answers1

2

Ctrl+u and Ctrl+k are what you are looking for:

 Ctrl + k    # Cut the Line after the cursor to the clipboard.
 Ctrl + u    # Cut/delete the Line before the cursor to the clipboard.

Other common used shortcuts you might want to have a try:

 Ctrl + w    # Cut the Word before the cursor to the clipboard.
 Ctrl + y    # Paste the last thing to be cut (yank)
 Ctrl + a    # Go to the beginning of the line (Home)
 Ctrl + e    # Go to the End of the line (End)
 Alt + b     # Back (left) one word
 Alt + f     # Forward (right) one word
 Ctrl + f    # Forward one character
 Ctrl + b    # Backward one character
 Alt + d     # Delete the Word after the cursor.
 Ctrl + d    # Delete character under the cursor
 Ctrl + h    # Delete character before the cursor (Backspace)

And as always, I suggest you to go to Bash Keyboard Shortcuts to learn more advanced Bash skills.

mainframer
  • 20,411
  • 12
  • 49
  • 68
  • Funny how you managed to list so many keyboard shortcuts yet you omitted `ctrl + c` which is probably what the OP is looking for (as also noted by *kirbyfan64sos*). – ShellFish May 31 '15 at 02:40
  • Because the OP is looking for a way to "quickly delete the shell command" not "quickly abort the shell command" , so I believe that `Ctrl + u` and `Ctrl + k` are what the OP want, not `Ctrl + c` – mainframer May 31 '15 at 02:44
  • Fair enough, may still be useful to add it to the list of extra commands though. Nice post by the way, I'll definitely read up on those keyboard shortcuts! – ShellFish May 31 '15 at 02:46
  • 1
    Note that on many systems, there is also a terminal 'kill' character which kills the input so far. Often, that is control-X. You can check with `stty -a`. And that will work with any program that is not actively modifying the terminal controls. – Jonathan Leffler May 31 '15 at 03:51