7

Say my current line was:

/tmp/path/to/file:123

Now I'm at the end of this line, now I want to delete :123 by pressing some key combination, was that possible? (colon was merely mentioned as an example, it could be other chars, just wondering if I could bind keys to do that)

It's similar to what alt + Backspace does, but more specific on the word separator.

Bash or zsh are all welcomed.

4 Answers 4

5

Search backward for :, then delete to the end of the line: Ctrl+R : Alt+D. Ctrl+R is the history search command, and the line you're editing is part of the history. Most commands, including Alt+D, terminate the incremental history search and have their usual effect.

In vi mode: F:C

This applies in both bash and zsh (in the default configuration, I don't guarantee that there aren't exotic combinations of options in zsh that will make it react differently, and of course this assumes the default keybindings).

1
  • I didn't know the current line counted as part of the history — nice! Dec 24, 2012 at 8:50
3

In bash you can try ctrl+alt+] to start the character-search-backward function, then :. The cursor will then move to the ":" you can then type alt+d to kill to the end of the current word.
If you use this often you can create a binding for it in ~/.inputrc.
These are readline functions and are documented in info: info readline.
I don't think there's a way to change readline's idea of what a word is without hacking the source.

1
  • The answer @Giles gives is more elegant than mine: use Ctrl + R : to search. Dec 24, 2012 at 8:55
0

As far as I know and from what I've just tested on bash 3.2

alt+backspace and ESC+backspace will delete the word up to the first "strange" character.

ctrl+w will delete until the first space in encountered.

From what I understood you are not scripting it but doing while typing.

0

In zsh, I can do this with the Emacs shortcuts. Your example above would be Alt-Backspace (or M-DEL in Emacs' lingo).

To do this, I have the following in .zshrc. (Note the URL -- I'm not taking credit for this!)

## emacs cursor-word movement (not identical but close enough)
#  also necessary for this:
#    /usr/share/zsh/functions/forward-word-match
#  (from http://stackoverflow.com/questions/10847255 )
autoload -U select-word-style
select-word-style bash

The file mentioned looks like this:

emulate -L zsh
setopt extendedglob

local curcontext=":zle:$WIDGET" word
local -a matched_words
integer count=${NUMERIC:-1}

if (( count < 0 )); then
  (( NUMERIC = -count ))
  zle ${WIDGET/forward/backward}
  return
fi

while (( count-- )); do
  match-words-by-style
  # For some reason forward-word doesn't work like the other word
  # commands; it skips whitespace only after any matched word
  # characters.

  if [[ -n $matched_words[4] ]]; then
    # just skip the whitespace and the following word
    word=$matched_words[4]$matched_words[5]
  else
    # skip the word but not the trailing whitespace
    word=$matched_words[5]
  fi

  if [[ -n $word ]]; then
    (( CURSOR += ${#word} ))
  else
    return 1
  fi
done

return 0
1
  • Oh, I misread your question! Well, perhaps that code could be useful to you, anyway. Dec 23, 2012 at 18:57

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .