2

I'm using Bash in vi mode with set -o vi.

Often I re-run a command but need to change the name of a file in the same directory.

Example:

mv /tmp/myfileA.txt /home/daniel/myfileA.txt

Then, I decide to also move another file:

mv /tmp/w00t.txt /home/daniel/w00t.txt

Typically I press the up-arrow key to get the last command, and press b until I get to the last slash character, and then insert the new file name. Since I do this very often, I'm wondering if there is an easy way to get the cursor just right after or on the last slash character.

Please note the above is only an example; I know I could do something like mv /tmp/{myfileA,w00t}.txt ... but often I simply type faster than I think :-)

2 Answers 2

2

T/ puts the cursor after the last / character, so:

Up Esc c Shift+T / w00t.txt Enter

or, to replace the just first word in the file name:

Up Esc Shift+T / c w w00t Enter

or, if there are multiple words before the extension and you want to keep the extension:

Up Esc Shift+T / c t . w00t Enter

Alternatively, with history expansion:

!!:h/w00t.txt
3
  • lC is a nice touch.
    – jasonwryan
    Nov 25, 2012 at 21:46
  • @jasonwryan actually T would have been better. Nov 25, 2012 at 21:59
  • Indeed: that will come in very handy. Thanks.
    – jasonwryan
    Nov 25, 2012 at 22:23
1

Escape

Shiftf

/

  • Escape puts you in edit mode

  • Shiftf Searches backwards for the pattern:

  • /

You can then execute a number of different options:

  • Controlk: deletes to the end of the line

  • df/: the same

i Puts you back in insert mode...

You must log in to answer this question.

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