60

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

2
  • 1
    Related question.
    – Thor
    Sep 8, 2012 at 23:26
  • Readline 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, 2022 at 9:57

3 Answers 3

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

11
  • 7
    On my system ctrl-b and ctrl-f move one character. Sep 8, 2012 at 21:31
  • 1
    Where is the "official" documentation about these? Why you only show fish without showing how to fishing?
    – qazwsx
    Sep 8, 2012 at 21:35
  • 4
    Any idea what's the equivalent to Ctrl+u for zsh?
    – qazwsx
    Mar 22, 2016 at 23:13
  • 2
    In zsh, Alt+w clears all characters before the cursor.
    – xjcl
    Apr 30, 2017 at 12:39
  • 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 tiswww.case.edu/php/chet/readline/rluserman.html . Also man 3 readline is full of exotica.
    – Jack Wasey
    Apr 14, 2022 at 9:50
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.

1
  • 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 tiswww.case.edu/php/chet/readline/rluserman.html
    – Jack Wasey
    Apr 14, 2022 at 9:55
6

The hotkey Ctrl+U should do this for you.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.