2

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)

nevster
  • 123
  • 5

5 Answers5

3

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.

Telemachus
  • 571
  • 2
  • 11
  • Excellent! Exactly what I wanted. stackoverflow/serverfault shows it's awesomeness yet again. – nevster Jun 10 '09 at 04: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 03 '11 at 09:03
2

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.

Ian Kelling
  • 2,661
  • 6
  • 23
  • 21
  • 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 07 '09 at 12:44
1

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.

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

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

uloBasEI
  • 686
  • 1
  • 4
  • 11
0

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.

nik
  • 7,100
  • 2
  • 25
  • 30
  • 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 07 '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 07 '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 07 '09 at 12:39