11

I want to change the delimiters bash (or readline) uses to separate words. Specifically I want to make '-' not delimit words, so that if I have the text

ls some-file

and I press Alt-Backspace it deletes the entire some-file text and not just up to the '-' char. This will also cause deletions of long flags like --group-directories-first faster and easier, needing only one key-press.

I believe that this is how zsh behaves and I'd like to make bash behave in the same way.

4
  • The '-' character has a special meaning in a command line, so the default behaviour isn't surprising. If you can change it, will there be any side effects? Suppose you had typed 'ls -al some-file', would you want to delete everything?
    – pavium
    Aug 31, 2009 at 12:35
  • 1
    @pavium in that case I'd want some-file to be deleted at first, and when pressed a second time I'd want -al to be deleted.
    – drrlvn
    Aug 31, 2009 at 12:48
  • 1
    I just found the answer on superuser.com.
    – Ekans
    Nov 12, 2013 at 15:12
  • See also unix.stackexchange.com/questions/78990/…
    – tripleee
    Aug 9, 2019 at 13:49

3 Answers 3

7

ctrl-w does exactly what you want.

2
  • I read that part of the manual a zillion times, I can't believe I missed it. Thanks :)
    – drrlvn
    Aug 31, 2009 at 12:49
  • 3
    I'll just note that after experimenting with it a little it's still not optimal since slashes are ignored as separators, which is annoying when working with paths. I added the line "\ew": unix-filename-rubout to /etc/inputrc and now M-e deletes a word while ignoring hyphens but does treat slashes as separators.
    – drrlvn
    Aug 31, 2009 at 13:01
5

One thing to keep in mind is that the bash key mapping for ctrl-W will not work if you have the stty werase setting assigned to ctrl-W. If you run "stty -a" and you see "werase = ^W" that will take precedence and use the tty idea of what a word boundary is. The tty's idea of a word boundary is usually whitespace, whereas bash's backward-kill-word function also includes - and /.

If you want to make Alt-Backspace do the same thing as the werase setting, you can do this: bind '"\M-\C-h": unix-word-rubout' bind '"\M-\C-?": unix-word-rubout'

Also, if you actually wanted to make ctrl-W do what Alt-Backspace does, you would do: stty werase undef # unless you do this, bash ignores the follow bind command bind '"\C-w": backwards-kill-word'

1
  • 1
    Unrelated to the OP, but stty werase undef totally fixed my issue! Thanks! Apr 28, 2014 at 14:37
1

This might be useful: Ctrl-r will initiate a reverse-i-search (for history AND the current line), so you can just hit space and escape and it's back where you want it, or ctrl-r again (after hitting the first space) if you want to go back one more arg. Then you can optionally kill the rest of the line.

Especially useful if you're dealing with long path arguments (e.g. in cp or diff), and need to modify the end of the first arg.

Tried to get \M-b to do this, but it stops at slashes.

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

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.