1142

If I'm using terminal and typing in a line of text for a command, is there a hotkey or any way to clear/delete that line?

For example, if my current line/command is something really long like:

> git log --graph --all --blah..uh oh i want to cancel and clear this line <cursor is here now>

Is there a hotkey or command to go from the above to:

>

?

Usually I will press the key, and if my current line is a brand new one on the history, that will clear it. But if I'm going through my command history via the key and start editing or using those commands, will only change the prompt to the next newest command in history, so it doesn't work here unless I press multiple times.

Benjamin W.
  • 46,058
  • 19
  • 106
  • 116
triad
  • 20,407
  • 13
  • 45
  • 50
  • 10
    [Shortcuts to Move Faster in Bash Command Line](http://teohm.com/blog/2012/01/04/shortcuts-to-move-faster-in-bash-command-line/) This page is more illustrative and explains better I think – Gab是好人 Aug 18 '15 at 08:22
  • 2
    FYI [How can I clear the current line of the Windows command prompt?](http://superuser.com/q/397189/116475) – Franck Dernoncourt Feb 21 '17 at 22:36
  • 1
    Possible duplicate of [In bash, how does one clear the current input?](http://stackoverflow.com/questions/1056394/in-bash-how-does-one-clear-the-current-input) – Cas May 14 '17 at 17:48
  • You will find a solution to your problem here [https://askubuntu.com/questions/470966/shortcut-to-clear-command-line-terminal](https://askubuntu.com/questions/470966/shortcut-to-clear-command-line-terminal) –  Jun 21 '21 at 12:41
  • Someone should put together a quick table of important shortcuts for the top 20 shells (bash, zsh, etc etc etc) and the top 3 OSs (*nix, macOS, and windows). That would be a great resource. – stevec Oct 06 '22 at 11:10

16 Answers16

2143

You can use Ctrl+U to clear up to the beginning.

You can use Ctrl+W to delete just a word.

You can also use Ctrl+C to cancel.

If you want to keep the history, you can use Alt+Shift+# to make it a comment.


Bash Emacs Editing Mode Cheat Sheet

Norman Lin
  • 453
  • 5
  • 6
kev
  • 155,172
  • 47
  • 273
  • 272
  • 338
    The line deleted by Ctrl-U is recallable with Ctrl-Y, too. – keks Dec 04 '12 at 10:59
  • 92
    Wut? So cool thanks keks & kev, ⌃+U is delete all before Cursor, ⌃+K is delete all after Cursor, ⌃+A is to go to the beginning of the line. So technically ⌃+C is the best solution for the Question – Binarian Apr 27 '13 at 20:42
  • 4
    Alt-Shift-# does not work? thought the others do. Alt-Shift-# prints a > character on the terminal – tgkprog May 29 '13 at 09:56
  • 4
    `U`/`A` and `Y` is more like a cut-paste. Try doing `Ctrl`+`Y` multiple times. – keyser Oct 01 '14 at 10:22
  • 12
    @LưuVĩnhPhúc `ESC` is it's own modifier key in bash. It's used for things like cancelling history navigation (`CTRL`+`r`), or swapping the last two words behind the cursor (`ESC`+`t`). – noobish Oct 15 '14 at 17:46
  • 1
    +1 for giving not just one option, but tree great tips. Ctrl+C should've been obvious, but I will most likely use Alt+Shift+# in near future and beyond. – andersoyvind Jan 29 '16 at 11:03
  • i thought u meant ctrl-c was undo of ctrl-u--- does that exist? – user391339 Apr 25 '16 at 15:27
  • Any way to do this on a keyboard without a # key? i.e. one you have to press SHIFT + 3 to access #? – SwimBikeRun Mar 14 '17 at 00:41
  • CTRL-U is a bad answer because if you currently have data you already want to paste it is replaced with the text you want delete from the command line. It does delete the line, but I would rather not have it copy the text. – JesseBoyd Oct 18 '17 at 12:29
  • If I delete by `Ctrl+U` and then redo it with `Ctrl+Y`, I can do it again and I get double... – WesternGun Feb 16 '18 at 08:28
  • `fn + shift + left/right arrow` will take you to start/end of line – Abhishek saharn Nov 18 '19 at 07:03
  • I seem to remember that this was the key combo we used on the BBC micro to do the same :) – SomaMan Apr 03 '20 at 10:02
  • I am not sure why, but `control` plus `w` clears the whole line for me. I use `tcsh` with ssh. – zyy May 19 '20 at 03:50
  • This is one of the greatest answers of all SO. `Ctrl + U` also works on Windows! – Dmitriy Popov Jan 09 '21 at 01:29
  • By reading https://superuser.com/a/298461 and https://unix.stackexchange.com/a/30991 I found you can put `Escape: kill-whole-line` in your `.inputrc` file, which allows you to use `Esc` to erase the entire line. Unlike `Ctrl+U`, it doesn't matter whether your cursor is at the end of the line, the beginning, or somewhere end the middle. I'm very happy with this! (I would leave an answer, but this question is closed.) – Garret Wilson Apr 05 '23 at 14:52
768

Just to summarise all the answers:

  • Clean up the line: You can use Ctrl+U to clear up to the beginning.
  • Clean up the line: Ctrl+E Ctrl+U to wipe the current line in the terminal
  • Clean up the line: Ctrl+A Ctrl+K to wipe the current line in the terminal
  • Cancel the current command/line: Ctrl+C.
  • Recall the deleted command: Ctrl+Y (then Alt+Y)
  • Go to beginning of the line: Ctrl+A
  • Go to end of the line: Ctrl+E
  • Remove the forward words for example, if you are middle of the command: Ctrl+K
  • Remove characters on the left, until the beginning of the word: Ctrl+W
  • To clear your entire command prompt: Ctrl + L
  • Toggle between the start of line and current cursor position: Ctrl + XX
J4cK
  • 30,459
  • 8
  • 42
  • 54
  • 11
    note ctrl-k deletes everything between the cursor and the end of the line. – Peter Berg Jan 09 '15 at 16:33
  • 12
    Remove next word: `Alt + D` – cnlevy May 09 '16 at 11:53
  • 2
    `Alt` + `D` does not work thats why I have not include it :( – J4cK Jun 28 '16 at 22:15
  • 3
    `CTRL + L` clears the screen but keeps what has been typed in the current command line. – Aziraphale Jul 01 '16 at 08:01
  • CTRL + XX is toggle between ~any~ two last positions of the cursor. Try it, type something, hit combo. You'll be at the start of the line, type something now at the start of the line, hit combo again. A few times. The toggle isn't with the start of the line. – Capstone Mar 24 '17 at 06:42
  • Where can I find the full list of shortkey? – alhelal Jun 10 '17 at 06:21
  • 1
    `Ctrl + Alt + >` moves to the end of the input history which is perfect when you want to cancel searching the history. – astasiak Jan 25 '18 at 16:58
  • Is there a way to change these somehow? I can't find anyone referencing that desire.... – mjs Sep 09 '18 at 13:14
  • @momomo: These key bindings are controlled by your shell (in cooperation with your terminal emulator, which decides what escape sequences to send for a given keypress). In bash, you can use `bind` or your `~/.inputrc`; in `zsh` or `tcsh`, use `bindkey`. Google " key bindings" for a bunch of info. – Andrew Janke Mar 09 '19 at 21:05
  • I don't think I've seen an `XX` key on any keyboard I've ever used. What is it shorthand for? – Mark Ransom Jan 11 '22 at 15:05
254

I have the complete shortcuts list:

  1. Ctrl+a Move cursor to start of line
  2. Ctrl+e Move cursor to end of line
  3. Ctrl+b Move back one character
  4. Alt+b Move back one word
  5. Ctrl+f Move forward one character
  6. Alt+f Move forward one word
  7. Ctrl+d Delete current character
  8. Ctrl+w Cut the last word
  9. Ctrl+k Cut everything after the cursor
  10. Alt+d Cut word after the cursor
  11. Alt+w Cut word before the cursor
  12. Ctrl+y Paste the last deleted command
  13. Ctrl+_ Undo
  14. Ctrl+u Cut everything before the cursor
  15. Ctrl+xx Toggle between first and current position
  16. Ctrl+l Clear the terminal
  17. Ctrl+c Cancel the command
  18. Ctrl+r Search command in history - type the search term
  19. Ctrl+j End the search at current history entry
  20. Ctrl+g Cancel the search and restore original line
  21. Ctrl+n Next command from the History
  22. Ctrl+p previous command from the History
Justin
  • 26,443
  • 16
  • 111
  • 128
tharunkumar
  • 2,801
  • 1
  • 16
  • 19
35

Another nice complete list:

TERMINAL Shortcuts Lists:

Left            Move back one character
Right           Move forward one character
Ctrl+b          Move back one character
Ctrl+f          Move forward one character

Alt+Left        Move back one word
Alt+Right       Move forward one word
Alt+b           Move back one word
Alt+f           Move forward one word

Cmd+Left        Move cursor to start of line
Cmd+Right       Move cursor to end of line
Ctrl+a          Move cursor to start of line
Ctrl+e          Move cursor to end of line

Ctrl+d          Delete character after cursor
Backspace       Delete character before cursor

Alt+Backspace   Delete word before cursor
Ctrl+w          Delete word before cursor
Alt+w           Delete word before the cursor
Alt+d           Delete word after the cursor

Cmd+Backspace   Delete everything before the cursor
Ctrl+u          Delete everything before the cursor
Ctrl+k          Delete everything after the cursor

Ctrl+l          Clear the terminal

Ctrl+c          Cancel the command
Ctrl+y          Paste the last deleted command
Ctrl+_          Undo

Ctrl+r          Search command in history - type the search term
Ctrl+j          End the search at current history entry and run command
Ctrl+g          Cancel the search and restore original line

Up              previous command from the History
Down            Next command from the History
Ctrl+n          Next command from the History
Ctrl+p          previous command from the History

Ctrl+xx         Toggle between first and current position
8c6b5df0d16ade6c
  • 1,910
  • 15
  • 15
28

Ctrl+A, Ctrl+K to wipe the current line in the terminal. You can then recall it with Ctrl+Y if you need.

Oliver Salzburg
  • 21,652
  • 20
  • 93
  • 138
Spyros Mandekis
  • 984
  • 1
  • 14
  • 32
17

or if your using vi mode, hit Esc followed by cc

to get back what you just erased, Esc and then p :)

bluTaz
  • 313
  • 2
  • 8
  • 6
    You can enable vi mode by `echo 'set editing-mode vi' >> ~/.inputrc`. Also works in places like python interpreter prompts and some SQL clients – dwurf Oct 13 '14 at 00:12
14

I'm not sure if you love it but I use Ctrl+A (to go beginning the line) and Ctrl+K (to delete the line) I was familiar with these commands from emacs, and figured out them accidently.

Oliver Salzburg
  • 21,652
  • 20
  • 93
  • 138
Muhammet Can
  • 1,304
  • 2
  • 16
  • 30
13

An alternative to Ctrl+A, Ctrl+K is Ctrl+E, Ctrl+U.

Oliver Salzburg
  • 21,652
  • 20
  • 93
  • 138
Andrey Starodubtsev
  • 5,139
  • 3
  • 32
  • 46
11

Ctrl+W will clear the word to the left.

Josh Correia
  • 3,807
  • 3
  • 33
  • 50
bendangelo
  • 438
  • 5
  • 8
8
  • Ctrl+u: move up to the beginning of your line to a ring buffer
  • Ctrl+k: move up to the end of your line to a ring buffer
  • Ctrl+w: move characters and (multiple) words left from your cursor to a ring buffer

  • Ctrl+y: insert last entry from your ring buffer and then you can use Alt+y to rotate through your ring buffer. Press multiple times to continue to "previous" entry in ring buffer.

Cyrus
  • 84,225
  • 14
  • 89
  • 153
4

CTRL+R and start typing to search for previous commands in history. Will show full lines.
CTRL+R again to cycle.

Sk8erPeter
  • 6,899
  • 9
  • 48
  • 67
Daniel
  • 41
  • 1
4

To delete the whole line no matter where the cursor is, you can use the kill-whole-line command, but it is unbound by default. It can be bound to, for example, Ctrl+Alt+K by inserting

"\e\C-k": kill-whole-line

into your Readline init file (conventionally ~/.inputrc).

Various remarks:

  • To avoid accidentally re-assigning a key sequence that is already in use for something else, you can check all your bindings with bind -P. Check for the suggested binding with

    bind -P | grep '\\e\\C-k'
    
  • The Readline init file name is is taken from the shell variable INPUTRC. If it is unset, the default is ~/.inputrc, or (if that doesn't exist) /etc/inputrc. Notice that if you have ~/.inputrc, /etc/inputrc will be ignored.
  • To reload your Readline init file, you can use Ctrl+X Ctrl+R.
  • Links to relevant manual sections:
Benjamin W.
  • 46,058
  • 19
  • 106
  • 116
4

In order to clean the whole line (2 different ways):

  • Home , Ctrl+K
  • End , Ctrl+U
Jose1755
  • 378
  • 3
  • 10
2

Add to the list:

In Emacs mode, hit Esc, followed by R, will delete the whole line.

I don't know why, just happens to find it. Maybe it's not used for delete line but happens to have the same effect. If someone knows, please tell me, thanks :)

Works in Bash, but won't work in Fish.

PickBoy
  • 1,234
  • 1
  • 13
  • 20
  • 1
    It's the `revert-line` readline command: "Undo all changes made to this line". It's bound, by default, to `M-r` (Meta-R), which can be both Alt-R or Esc R. This means it's not actually deleting the line, only when it was empty to begin with; if you scroll back in your command history and change a command, then issue `revert-line`, it will change back to the command it was originally and not delete the line. – Benjamin W. Mar 09 '17 at 00:27
2

Ctrl+Alt+Backspace for deleting the backward words from the end of the line

Ctrl+Delete for deleting the forward words from the beginning of the line

0

Alt+# comments out the current line. It will be available in history if needed.