Say I have a directory with one file in it mylongfilename.blahdeblah.txt now the keystrokes mv [tab] [tab] will result in mv mylongfilename.blahdeblah.txt mylongfilename.blahdeblah.txt on my command line

What I'd now like to do is type something in the place of the second blahdeblah If I hit ctrl-b a few times I'll be back at the . before .txt At this point I want to type something like ctrl-w to delete to the preceding . Ie I'd end up with mv mylongfilename.blahdeblah.txt mylongfilename..txt

Is there a keystroke similar to ctrl-w that will do that? (Ctrl-w will delete to the preceding space - not what I want)


You appear to be in emacs mode. To do what you want there, this works:


(Strictly speaking it's M-Del. See this page for more Emacs keybindings. I don't think Bash's emacs mode supports them all, but it supports this one.)

telemachus ~ $ mv mylongfilename.blahblah.txt mylongfilename.|.txt

The pipe shows were the cursor ends up.

  • Excellent! Exactly what I wanted. stackoverflow/serverfault shows it's awesomeness yet again. – nevster Jun 10 '09 at 4:48
  • Various Emacs bindings do work in bash (terminator, gnome-terminal or xterm in Ubuntu 10.04 LTS), but M-Del doesn't (it returns ~3). Any ideas? – SabreWolfy Oct 3 '11 at 9:03

Bash has emacs and vi modes. In vi mode, simply dw for delete word, or cw for change word. set -o vi to get into vi mode. There's probably an emacs mode equivalent.

man readline is the reference for bash keyboard stuff, and bash specific editions to readline are in man bash.

  • I'm a Vim user, but I still find vi mode for Bash a bit painful. Maybe it's just that vi-mode isn't quite Vim. Still, b, b, db, i seems to do what the OP wants here. (For some reason 2b produces odd results for me. It should move me back two word boundaries - onto the first '.', no?) In this case emacs mode seems to win. – Telemachus Jun 7 '09 at 12:44

From man bash:

    The set of characters that the Readline library treats as word separators when
    performing  word  completion.   If COMP_WORDBREAKS is unset, it loses its special
    properties, even if it is subsequently reset.

Changing this should change the characters used as word breaks, so if you added '.' to it's current value, then Ctrl-w would delete back to the '.' instead of back to the preceeding space.

  • Sorry, that doesn't work, should have tried it before posting. Telemachus is right when he says meta-backspace or M-Del. – pgs Jun 7 '09 at 12:29
  • Funny, since I was just about to try your idea. It sounds like it should work. – Telemachus Jun 7 '09 at 12:30
  • Yeah, it's for word/command completion. The COMP_ prefix should have been a giveaway. – pgs Jun 7 '09 at 12:35

You can try to swap the words to put the part blahdeblah at the end of the line (with Esc-T) and then remove the end of the line:

  • mv [tab] [tab]

    mv mylongfilename.blahdeblah.txt mylongfilename.blahdeblah.txt

  • [Esc-T] (Swap the last two words before the cursor)

    mv mylongfilename.blahdeblah.txt mylongfilename.txt .blahdeblah

  • [Ctrl-w]

    mv mylongfilename.blahdeblah.txt mylongfilename.txt

  • Not exactly what I wanted but handy to know. Thanks! – nevster Jun 10 '09 at 4:50

Presuming that you have quite a few such 'mv' operations to do in that directory, why not write a short translation script using say, 'sed'?

You'll find some tricks here. But don't let that limit you, but all means lookup some more sed and awk too.

  • Yeah - I've been doing some sed for other things. But I'm looking for a simple solution for once-off things. Or rather, not once-off but where scripting is overkill. rather than pressing backspace 8 times. It'd just be handy to have a keystroke like ctrl-w. – nevster Jun 7 '09 at 12:10
  • Ok, have it your way. But, i have seen many times that trying to get a 'simple' solution ends up with a lot of time lost. – nik Jun 7 '09 at 12:12
  • 1
    Or you could see that this question will apply to far more situations in future than the one at hand, and is thus worth answering directly! – Alex J Jun 7 '09 at 12:39

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.