Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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?

share|improve this question
My advice doesn't answer your question, but is a small tip. As in your example, you can use next construction: mv foo/bar/poit/zoid/{narf.txt,troz.txt} instead ot typing/copypasting long address twice. – rush Jun 6 '12 at 10:05
Slightly shorter than @Rush's solution: mv foo/bar/poit/zoid/{narf,troz}.txt. This works especially well if we have something like .../zoid/this_source_file_has_to_be_patched.c{,.orig} – Ansgar Esztermann Jun 6 '12 at 10:14
@Rush Do you mind pointing it out as an answer? It may not be directly what I asked, but then again, your solution is much more to the point of what I want. It deserves a token of acceptance. – k0pernikus Jun 6 '12 at 10:21
up vote 5 down vote accepted

If I've planned ahead, I use brace expansion. 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-ySpaceCtrl-y: yank, space, yank again to get mv foo/bar/poit/soid/narf.txt foo/bar/poit/soid/narf.txt
  • Meta-backspaceMeta-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.

share|improve this answer
Neat! In my case I had to press Alt + Backspace instead of Meta + Backspace. – k0pernikus Jun 7 '12 at 9:44
@k0pernikus Yes on most keyboards, the Alt key is the Meta key. – jw013 Jul 12 '12 at 16:19

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.

share|improve this answer
This is called brace expansion‌​. – CodeGnome Jun 6 '12 at 13:03

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
share|improve this answer
Very useful! This works especially well with bash's magic-space option in inputrc to automatically expand history expansions. (I couldn't use the top-voted answer because I've set ^W to break at slashes since that's usually more useful.) – Soren Bjornstad Jan 22 at 17:40

Playing around I got this to work:

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

Hit Enter
to store the last parameter.

Now use to get last typed in line back. Enter a space and to get the last used parameter use:

Alt + .

I hate provoking an error, but it gets the job done in this use case.

share|improve this answer
Cool, another great tip learned! – Bram Jun 6 '12 at 18:07
<kbd>Esc</kbd> + <kbd>.</kbd> is (it?) the same as <kbd>Alt</kbd> + <kbd>.</kbd>. – Nikos Alexandris Jul 15 '13 at 10:19

You can easy use variables:

a=test.csv ; cp $a $a.bak

Or in you case (note tab competition works for the a part):

a=foo/bar/poit/zoid/ ; mv ${a}narf.txt ${a}troz.txt
share|improve this answer

Use the same thing in the {,} "duplicator". I.e.:

git tag -m {v,v}1.0.1

# same as 

git tag -m v1.0.1 v1.0.1
share|improve this answer

Your Answer


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.