Bash – How to Repeat Currently Typed Parameter on Bash Console

bashline-editorreadline

I was just typing something along the lines of:

mv foo/bar/poit/zoid/narf.txt

and suddenly realized, damn, I have to type large parts of that parameter again

mv foo/bar/poit/zoid/narf.txt foo/bar/poit/zoid/troz.txt

Even with tabcompletion, quite a pain. I know I can copy paste the parameter by mouse-selecting the text and middleclick but that is not good enough. I want to remain on the keyboard.

So is there a way to copy paste the current parameter of the line using the keyboard?

Best Answer

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.

Related Question