Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

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)

share|improve this question

5 Answers 5

up vote 3 down vote accepted

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

meta-backspace

(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.

share|improve this answer
    
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

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

share|improve this answer
    
Not exactly what I wanted but handy to know. Thanks! –  nevster Jun 10 '09 at 4:50

From man bash:

COMP_WORDBREAKS
    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.

share|improve this answer
    
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

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.

share|improve this answer
    
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

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.

share|improve this answer
    
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 Jurkiewicz Jun 7 '09 at 12:39

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.