120

Consider the following bash prompt, where ^ denotes the prompt location:

$ git commit -am "[bug 123456] Do this and that with the bug"
                               ^

Suppose that I want to commit again to the same bug, with a different commit message. Is there a way to delete the text from the cursor position till the end of line?

0

3 Answers 3

188

Use Ctrl+K to delete from the cursor to the end of the line.

Use Ctrl+U to delete from the cursor to the beginning of the line.

4
  • What is the key combination to put?
    – ShooShoSha
    Aug 21, 2018 at 0:44
  • @ShooShoSha, Try Shift + Insert
    – user128285
    Aug 21, 2018 at 5:31
  • 2
    what are the meanings of K and U, I can not recall this next week if I donot know the meanings, how to remember them?
    – http8086
    Jan 18, 2019 at 8:52
  • 8
    @hylepo Dunno but my mnemonic is Brexit - the UK leaves Europe - U deletes to the beginning and K to the end.
    – Snowcrash
    Feb 5, 2019 at 14:05
55

It depends on whether you are using vi(set -o vi) or emacs(set -o emacs) editing mode within your shell.

By default, your shell generally defaults to emacs editing mode.

  • In emacs mode, deleting to the end of the line can be accomplished by using the command ctrl-k.

  • If, however, you happen to be using vi editing mode in your command shell, the same result can be accomplished by typing Esc(which will place you in command mode) followed by d-$(if you want to delete the entire line, enter dd).

If you are uncertain as to which editing mode that you are currently using in your shell, enter the command set -o from the command line and you will be able to determine which editing mode that you are currently using:

set -o
...snip...
emacs           on
...snip...
vi              off

To switch your command-line editing mode, simply type either:

set -o vi

or

set -o emacs
7
  • 4
    If you want a mnemonic, the K stands for kill (the line starting from cursor position). Mar 17, 2013 at 13:53
  • 5
    In vi command mode, D works the same as d$ Mar 17, 2013 at 16:54
  • 4
    +1 for actually writing the right answer and explaining that shellopts may change it.
    – TC1
    Mar 17, 2013 at 20:57
  • 1
    >> Thanks. It seems that I'm using vi, so CTRL+K does the trick. – Adam Matan Adam, then that's emacs-mode you're in, not vi.
    – catch22
    Mar 19, 2013 at 19:14
  • 1
    In Kubuntu oneiric (konsole), Ctrl+K and ESC Shift+D both work. ESC d deletes some of the line (maybe up to a ';'). (Typing a $ after that just sends the $ to the console.) I've been trying to figure out how to do that for quite awhile. Thanks!
    – Joe
    Mar 20, 2013 at 18:56
5

In case you're only familiar with one of Zsh and Bash, here are the equivalent key bindings you (may) need:

bash

bind '"\C-k"':kill-line

zsh

bindkey "\C-k"      vi-kill-eol

Other relevant information

To get a list of key bindings:

# bash
bind -p

# zsh
bindkey
3
  • I'm confused. For Bash, shouldn't it be "^k" for Control+K ?
    – wjandrea
    Sep 25, 2017 at 21:18
  • Sorry, that is my personal binding. I would have put the default binding but was not comfortable with bash syntax. Since you've mentioned what it should be I've taken your word for it and edited my post. Sep 25, 2017 at 21:30
  • 2
    Oh, OK. I'm not familiar with it myself, but just checked and it should be "\C-k"
    – wjandrea
    Sep 25, 2017 at 21:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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