174

I use terminal for almost all tasks. Let's say I have entered a huge command like this:

sudo a-huge-command

What is the easiest way to delete the whole command with a single shortcut, rather than keep on hitting backspace key?

I'm a Ubuntu newbie looking to use Ubuntu in a professional way.

batman
  • 8,071

9 Answers9

181

Use Ctrl+U to cut text from cursor position to the beginning of the command line. Later, if you want, use Ctrl+Y to paste the cut text.

If you just want to discard the current command line and get a new clean prompt hit Ctrl+C.

For more reference please check the Ubuntu documentation for Using The Terminal

Sambit
  • 1,264
Eric Carvalho
  • 55,453
113

Ctrl + U should help you.

Below are the rest of the options available. Grabbed from here

UNIX understands several control-key commands, commands you enter by holding down the control key (ctrl) while striking a second key

CTRL + S - freezes the screen and stops any display on the screen from continuing (equivalent to a no-scroll key) (sometimes takes a moment to work)

CTRL + Q - un-freezes the screen and lets screen display continue

CTRL + C - interrupts a running program

CTRL + \ - same as CTRL - C but stronger (used when terminal doesn't respond)

CTRL + Z - suspends a running program (use the fg command to continue the program, see s$

CTRL + H - deletes last character typed

CTRL + W - deletes last word typed

CTRL + U - deletes last line typed

CTRL + R - searches text from history

CTRL + D - ends text input for many UNIX programs, including mail and write.

Note:

When we delete using CTRL + W or CTRL + U, we are also performing a (edit) "cut" (yank in) operation (delete and store in buffer/clipboard). To paste (yank out) the string in buffer/clipboard, use CTRL + Y.

devav2
  • 37,290
61

I'm usually using Alt+Backspace. If you are using bash, this will let you delete untill the previous special character (/, ;, , etc.). If you are using zsh, it will remove the slashes and semicolons as well. It is a lot faster than just hitting Backspace.

In bash, this is different from Ctrl+w in the sense that Ctrl+w deletes the previous word wheres Alt+Backspace deletes until the previous special character is found. In zsh, both key combinations do the same thing

38

Here is a listing of keyboard shortcuts that can be used with the bash shell.

Ctrl + A    Go to the beginning of the line you are currently typing on
Ctrl + E    Go to the end of the line you are currently typing on
Ctrl + L    Clears the Screen, similar to the clear command
Ctrl + U    Clears the line before the cursor position. If you are at the end of the line, clears the entire line.
Ctrl + H    Same as backspace
Ctrl + R    Lets you search through previously used commands
Ctrl + C    Kill whatever you are running or start a new prompt
Ctrl + D    Exit the current shell
Ctrl + Z    Puts whatever you are running into a suspended background process. fg restores it.
Ctrl + W    Delete the word before the cursor
Ctrl + K    Clear the line after the cursor
Ctrl + T    Swap the last two characters before the cursor
Esc + T     Swap the last two words before the cursor
Alt + F     Move cursor forward one word on the current line
Alt + B     Move cursor backward one word on the current line
Tab         Auto-complete files and folder names

What you are wanting to do is achieved by either Ctrl+C or Ctrl+U at the end of the line.

OrangeDog
  • 909
  • 11
  • 20
25

Alt+# (i.e., Alt+Shift+3) will comment out the current command and continue on the next line.

For example, if you type t, e, s, t, Alt+#, you'll get:

you@computer$ #test
you@computer$ 

If you want to get your old command back, you can press the up arrow and delete the hash character (Up, Home, Delete or Up, Ctrl+A, Delete).

I think it's a feature of GNU Readline, since it works in Bash, Python, and MySQL.

Snowball
  • 771
  • 6
  • 5
24

Ctrl + C - in addition to interrupting running commands, it may be used to "interrupt" your command line input as well.

In contrast to the Ctrl + U, you will still see what you have typed but your cursor will jump to the new line and you will get an empty command line prompt.

jokerdino
  • 41,732
8

The Bash readline shortcut Ctrl+X+E is very useful indeed when you are working on the command-line. If you are in the process of entering a long command and decide that you want to instead open it in your default text editor, all you have to do is use the shortcut.

It makes use of Bash's readline library and this particular shortcut is called the edit-and-execute-command. You can set your default editor by placing export EDITOR="/usr/bin/vi" in ~/.bashrc or ~/.bash_aliases.

Enter bind -P to see your current readline bindings and refer to man readline or the Ubuntu manpages online for more information.

7

I also use Esc+Backspace to delete all previous characters until a special character. This is same is Alt+Backspace. Handy if you're just deleting a few words at a time.

jobin
  • 28,567
jytou
  • 170
  • 3
3

For vi key bindings

When usin a vi key map like in bash with set -o vi or in zsh with bindkey -v:

Insert mode

It is just the same keystroke as in default/emacs mode:

Ctrl+U

deleting form the current position to the start of line. So it deletes the whole line if the cursor is at the end.

Normal mode

There are multiple ways to delete the line in two keystrokes:

Delete linewise dd, with the default count of one line:

dd

which is the simplest in terms of keys touched.

Another way is: Go to start of line with ^, and delete to the end of line with D:

^D

These both delete the entire line, not only to the left, like Ctrl+U in insert mode, or the variant bleow.

If you are at the end of line currently, so you do not need to delete anything to the right, this would also do:

Delete from here d, to start of line ^:

d^

You can use these all from inset mode, you need the usual escape first to go to normal mode. For example to delete the whole line from insert mode, use:

Escdd

Volker Siegel
  • 13,295