Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

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
3  
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
2  
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
add comment

4 Answers 4

up vote 3 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
add comment

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
1  
This is called brace expansion‌​. –  CodeGnome Jun 6 '12 at 13:03
add comment

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
add comment

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
add comment

Your Answer

 
discard

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.