59

On bash command-line, how to delete all letters before cursor? I know Ctrl-k deletes all afterward the cursor.

qazwsx
  • 25,536
  • 30
  • 72
  • 106
  • 1
    [Related question](http://stackoverflow.com/questions/9679776/how-do-i-clear-delete-the-current-line-in-terminal). – Thor Sep 08 '12 at 23:26
  • [Readline](https://tiswww.case.edu/php/chet/readline/rluserman.html) handles the keyboard shortcuts. See also `man 3 readline`. These apply to very many ineractive command-line tools, not just bash. – Jack Wasey Apr 14 '22 at 09:57

3 Answers3

109

Ctrl-u - Cut everything before the cursor


Other Bash shortcuts,

  • Ctrl-a Move cursor to beginning of line
  • Ctrl-e Move cursor to end of line
  • Ctrl-b Move cursor back one word
  • Ctrl-f Move cursor forward one word
  • Ctrl-w Cut the last word
  • Ctrl-k Cut everything after the cursor
  • Ctrl-y Paste the last thing to be cut
  • Ctrl-_ Undo

And discover more via man page for bash shell: man bash

Additional bash command-line shortcut cheat sheet: http://www.bigsmoke.us/readline/shortcuts

See the documentation here: http://www.gnu.org/software/bash/manual/bashref.html#Commands-For-Killing

Obligatory: Learn more about Bash, Linux, and Tech through Julia's comics: https://twitter.com/b0rk/media

Julia on Bash

Mayura
  • 1,879
  • 1
  • 19
  • 21
  • 7
    On my system ctrl-b and ctrl-f move one character. – Vaughn Cato Sep 08 '12 at 21:31
  • 1
    Where is the "official" documentation about these? Why you only show fish without showing how to fishing? – qazwsx Sep 08 '12 at 21:35
  • @Problemania I've added a link. In general, man or info pages are a good place to start, try running the command `man` followed by the name of the unix tool you're using (eg `man bash`). – John Carter Sep 08 '12 at 21:40
  • I know about `man`. How can you find info about the above commands in `man bash`? I tried and couldn't. – qazwsx Sep 08 '12 at 21:42
  • 1
    Yeah, for GNU stuff (such as bash), `info` pages can be more complete - that's what the link I posted in the above answer goes to. To get this from the command line use `info bash` -> Command Line Editing -> Bindable Readline Commands -> Commands for Killing. – John Carter Sep 08 '12 at 21:46
  • Actually, though the above is in `man bash` for me as well (use `/` to search for text). – John Carter Sep 08 '12 at 21:48
  • 4
    Any idea what's the equivalent to Ctrl+u for zsh? – qazwsx Mar 22 '16 at 23:13
  • 2
    In zsh, Alt+w clears all characters before the cursor. – xjcl Apr 30 '17 at 12:39
  • thank you so much for this info. I didnt know bash had its own man page – lollerskates May 14 '18 at 17:18
  • 2
    @qazwsx For at least some of this, GNU readline is the relevant software, maintained by the GNU bash maintainer, and in step with it. See https://tiswww.case.edu/php/chet/readline/rluserman.html . Also `man 3 readline` is full of exotica. – Jack Wasey Apr 14 '22 at 09:50
  • @VaughnCato, alt-b,alt-f works. – hunter_tech Aug 16 '23 at 07:04
17

In zsh, Alt+w clears all characters before the cursor.

In contrast to bash this does NOT cut them; it just deletes them.

This applies to zsh's Emacs mode (which is the default), NOT to Vi mode.

xjcl
  • 12,848
  • 6
  • 67
  • 89
  • Actually, I did recently learn that bash does 'cut' when readline (not bash, actually) deletes. On my Linux machine now, in bash, I can cut back a word with control+W and "yank" it back with control-Y. Many vexing and obscure key combos are there, but learning the big hitters is already useful when working in a shell. (If you dare to use vim mode for readline, you can have yank with a different meaning.) My head hurts, but cursor keys are less worn. See https://tiswww.case.edu/php/chet/readline/rluserman.html – Jack Wasey Apr 14 '22 at 09:55
6

The hotkey Ctrl+U should do this for you.

mjgpy3
  • 8,597
  • 5
  • 30
  • 51