Bash: delete from cursor till end of line with a keyboard shortcut
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?
Solution 1:
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.
Solution 2:
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
Solution 3:
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