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.

4
  • 13
    It's also worthwhile to note that you can set bash (and several other shells) to understand vi keys: set -o vi. You could then do esc, dd (or otherwise edit/replace as you could do in vi[m]). Sep 4, 2012 at 16:35
  • 6
    Home, '#', Enter Sep 4, 2012 at 22:05
  • 5
    this is a cheap hack but for what it's worth, I usually just hit up-arrow until I get to a really short command (e.g., ls)and then backspace - this usually only a couple of key strokes so it isn't that big a' deal.
    – DQdlM
    Sep 5, 2012 at 2:20
  • CTRL <- tapped a couple of times usually lets me go back to the beginning of my query.. is this what you mean ? Oct 25, 2012 at 21:39

9 Answers 9

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

4
  • This answer works for both bash (Ubuntu default) and tsch.
    – Nick
    Sep 4, 2012 at 21:02
  • I thought dash was Ubuntu default?
    – Chris
    Sep 5, 2012 at 3:23
  • 15
    dash is the default interpreter for /bin/sh commands. bash is the default login and TTY shell.
    – user87602
    Sep 5, 2012 at 3:29
  • 9
    I find Ctrl + C to be a bad habit, since I'll often accidentally send signals to programs with their own prompt, killing them and losing my progress and/or momentum.
    – user606723
    Sep 6, 2012 at 20:26
114

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.

5
  • 7
    Actually ctrl-r searches text from history. Sep 4, 2012 at 12:07
  • @EricCarvalho You are right. Removing that option from the answer.
    – devav2
    Sep 4, 2012 at 12:16
  • 2
    These are generic UNIX control codes. The default ubuntu terminal is bash-based, which is different.
    – OrangeDog
    Sep 5, 2012 at 10:19
  • 1
    Ctrl-U deletes from the cursor to the beginning of the line. I often use it by placing the cursor just before the arguments to a command from history. That way, I can type a new command using the same arguments - like changing less file to kate file so I can edit the file after I've looked at it to see if any edits are needed.
    – Joe
    Sep 5, 2012 at 20:33
  • 1
    @OrangeDog -- These are fine -- I use all of these in my Ubuntu bash-bashed terminal, with the exception of the CTR-\ one.
    – belacqua
    Jun 15, 2013 at 1:05
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

5
  • 8
    Ctrl+W does the same thing.
    – hytromo
    Sep 4, 2012 at 19:28
  • Won't work for terminals such as rxvt though.
    – sqram
    Sep 5, 2012 at 4:36
  • 1
    @lyrae For me it works perfectly in rxvt, though not in xterm. And it only works if set -o emacs is set.
    – Marco
    Sep 6, 2012 at 9:55
  • I use ALT+Backspace as well as CTRL+C. This works on any linux. :)
    – tftd
    Sep 8, 2012 at 21:17
  • @hakermania: No, its different. Ctrl + w deletes a word and Alt + Backspace deletes until the previous special character(/, ;, ` `, etc).
    – jobin
    May 28, 2014 at 11:22
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.

4
  • This is the most-complete list, Though the best answer is Ctrl+C or (Ctrl+E Ctrl+U) Sep 5, 2012 at 16:24
  • How many on the list are applicable to tcsh, which I'm interested in?
    – TCSGrad
    Sep 5, 2012 at 23:21
  • @shan23 Read the "Editor commands" section of man tcsh, or use bindkey -l. It looks like it's very different.
    – OrangeDog
    Sep 6, 2012 at 18:12
  • Note that some of these will fail if you're using vi bindings for bash's (or another shell's) Readline. These should mostly apply to tcsh as well; these pre-dated bash. bash also incorporated a lot of the nice history shortcuts from csh/tcsh.
    – belacqua
    Jun 15, 2013 at 1:24
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.

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.

1
  • 5
    I prefer this simply because of muscle memory, and you can still copy/paste anything important because the aborted command-line stays there.
    – ish
    Sep 4, 2012 at 15:28
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.

0
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

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .