How to repeat currently typed in parameter on bash console?

History expansion actually works on the current command as well, using the event designator !#. Combine this with the word designator for the last argument - $ - to get the parameter you just typed. And you can use all the regular modifiers over it, so if, e.g., you are renaming a file in a far away directory, you can just type:

mv path/you/do/not/want/to/type/twice/oldname !#$:h/newname

If I've planned ahead, I use brace expansion. In this case:

mv foo/bar/poit/zoid/{narf,troz}.txt

Here is another approach using the default readline keyboard shortcuts:

  • mv foo/bar/poit/soid/narf.txt: start
  • Ctrl-w: unix-word-rubout to delete foo/bar/poit/soid/narf.txt
  • Ctrl-y, Space, Ctrl-y: yank, space, yank again to get mv foo/bar/poit/soid/narf.txt foo/bar/poit/soid/narf.txt
  • Meta-backspace, Meta-backspace: backward-kill-word twice to delete the last narf.txt
  • troz.txt: type the tail part that is different

If you spend any non-trivial amount of time using the bash shell, I'd recommend periodically reading through a list of the default shortcuts and picking out a few that seem useful to learn and incorporate into your routine. Chapter 8 of the bash manual is a good place to start. Knowing the shortcuts can really raise your efficiency.


As in your example, you can use next construction:

mv foo/bar/poit/zoid/{narf.txt,troz.txt}

or even (as suggested Ansgar Esztermann):

mv foo/bar/poit/zoid/{narf,troz}.txt

instead ot typing/copypasting long address twice.