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.
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
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.
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. Inzsh
, both key combinations do the same thingHere is a listing of keyboard shortcuts that can be used with the
bash
shell.What you are wanting to do is achieved by either Ctrl+C or Ctrl+U at the end of the line.
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:
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.
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.
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 theedit-and-execute-command
. You can set your default editor by placingexport EDITOR="/usr/bin/vi"
in~/.bashrc
or~/.bash_aliases
.Enter
bind -P
to see your currentreadline
bindings and refer toman readline
or the Ubuntu manpages online for more information.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.
For
vi
key bindingsWhen usin a
vi
key map like inbash
withset -o vi
or inzsh
withbindkey -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 withD
:^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