You customize bash via an .inputrc
file in your /home/username
, you can copy the default
cp /etc/inputrc.default ~/.inputrc
here is mine (comments start with # )
# Key-bindings for the command-line editor.
# Ask before displaying >50 items
# Since $WINDIR $PATH var can be in $PATH, this could list
# all window exectables in C:\WINDOWS
set completion-query-items 50
# Ignore case for the command-line-completion functionality
# on: default to a Windows style console
# off: default to a *nix style console
set completion-ignore-case on
# none, visible or audible
set bell-style audible
# disable/enable 8bit input
set meta-flag on
set input-meta on
set output-meta off
set convert-meta on
# visible-stats
# Append a mark according to the file type in a listing
set visible-stats off
set mark-directories on
# Show all instead of beeping first
set show-all-if-ambiguous off
# MSYSTEM is emacs based
$if mode=emacs
# Common to Console & RXVT
"\C-?": backward-kill-line # Ctrl-BackSpace
"\e[2~": paste-from-clipboard # "Ins. Key"
"\e[5~": beginning-of-history # Page up
"\e[6~": end-of-history # Page down
$if term=msys # RXVT
"\e[7~": beginning-of-line # Home Key
"\e[8~": end-of-line # End Key
"\e[11~": display-shell-version # F1
"\e[15~": re-read-init-file # F5
#$endif
#$if term=cygwin # Console
$else
"\e[1~": beginning-of-line # Home Key
"\e[4~": end-of-line # End Key
"\e[3~": delete-char # Delete Key
#~ "\e\e[D": backward-word # Alt-LeftArrow
#~ "\e\e[C": forward-word # Alt-RightArrow
"\M-\e[D": backward-word # Alt-LeftArrow
"\M-\e[C": forward-word # Alt-RightArrow
`#~` "\C-\E[D": backward-word # Ctrl-LeftArrow, nowork, can't be made to work
#~`enter preformatted text here` "\C-\E[C": forward-word # Ctrl-RightArrow, nowork, can't be made to work
#~ to see current bindings use bind -q backward-kill-line
"\e\e": kill-whole-line # double/triple escape works :) Esc/Escape to delete current line like cmd.exe
$endif
$endif
to find out what you need to type in your inputrc on the left side (the escape code, as it can vary between laptop/desktop...), at the prompt type echo '
then type Ctrl-V
followed by the key , like Home
, then type '
example
$ echo ' home key ^[[1~ '
home key
~
$ echo ' end key ^[[4~ '
end key
~
$ echo ' pg up page up ^[[5~ '
pg up page up
~
$ echo ' pg dn page down ^[[6~ '
pg dn page down
~
then replace each ^[
with \e
add \M-
for Alt
theoretically you'd use \C-
for Ctrl
but it currently doesn't work (windows limitation)
the available commands (like backward-kill-line
) are listed in http://www.gnu.org/software/bash/manual/bashref.html#index-backward_002dkill_002dline-_0028C_002dx-Rubout_0029
you can view existing keyboard shortcuts/bindings with bind -p
or
$ bind -q backward-kill-word
backward-kill-word can be invoked via "\M-\C-h", "\M-\C-?".
~
$ bind -q backward-word
backward-word can be invoked via "\M-\M-[D", "\M-b", "\C-\E[[D".
~
$ bind -q beginning-of-line
beginning-of-line can be invoked via "\C-a", "\M-OH", "\M-[1~", "\M-[H".
~
don't mess with TERMCAP
Best Answer
There isn't a default shortcut mapping to do what you asked for. A full documentation of command mode editing can be found through
:help cmdline-editing
. You are welcomed to browse through it to find anything useful to you. But I don't think the function you asked for is in it.I guess the default way of editing of command-line doesn't require such operation. Usually we remove the word before cursor by
<Ctrl-W>
without moving cursor around. But of course, if you have your own way of editing it, you can always bend vim's behaviour to your favour.You can have the shortcut you want by mapping
<C-K>
to a function defined to suit your required behaviour. Here I write an example. You can just paste the following code to your~/.vimrc
. Re-source the file. And you can have the short-cut working in your own way.If you are interested, this invokes an default command mode shortcut(
<C-\>e
) for evaluating {expr} and replacing the whole command line with the result.strpart()
is an string truncating function given by vim.Upon your
<C-k>
keystroke, vim will extract the content from the command mode as a string. And put back only the content from the start of the string to one position ahead of the cursor position. Rest of the content will be truncated and thus deleted.