Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

Suppose I have the following in my terminal:

> cp my_file_name originals/#

Where the # represents the cursor.

Is there a key combination to insert my_file_name into the cursor position, so that I don't have to type it again? I'm aware of ALT+. to get the last argument from the previous command, but I want the previous argument from the current command!

Any ideas?

Thanks

Rich

share|improve this question
1  
Maybe I'm missing something, but doesn't cp myfile orginals/ do precisely the same thing as cp myfile originals/myfile? –  frabjous Sep 2 '10 at 16:24
    
You're absolutely right - it's a poor example, but a simple example of what I want :) I need to do the same thing in situations where originals/ isn't clear enough. –  Rich Sep 3 '10 at 6:56

5 Answers 5

up vote 9 down vote accepted

One way of doing it is using brace expansion:

cp {,originals/}my_file_name
share|improve this answer
    
+1. Just spent 5 mins trying cp file{,/orig}. doh ! thx for posting this. –  Sirex Sep 2 '10 at 10:03

You can use Ctrl+W to cut the argument and store it in your clipboard, and then Ctrl+Y to paste it again:

cp my_file_nameCtrl+W Ctrl+Y originals/Ctrl+Y
share|improve this answer

Complicated usage, but simpler setup:

You can bind a keystroke (I'll use Ctrl-Q) to the readline copy-backward-word function and do this:

Alt-2 Ctrl-Q Ctrl-y Ctrl-Alt-h

That means:

  • digit-argument copy-backward-word (copy the two preceding words)
  • yank (paste)
  • backward-kill-word (get rid of the extra word)

To bind the keystroke at the command line:

bind '"\C-q": copy-backward-word'

or add this to your ~/.inputrc:

"\C-q": copy-backward-word

Simpler usage, but more complicated setup:

You can make that long keystroke sequence into a macro triggered by one keystroke (I'll use Alt-q here and make use of the Ctrl-q binding from above):

At a shell prompt:

bind '"\eq": "\e2\C-q\C-y\e\C-h"'

or in your ~/.inputrc:

"\eq": "\e2\C-q\C-y\e\C-h"

Remember, you'll also need to bind Ctrl-q as above.

Now to copy the word before the current one all you need to press is Alt-q.

share|improve this answer

Here's another way, although I'm not certain why you are looking to do what you are trying to do, since the file name is implied in your cp command example (and would work as expected).

> F=my_file_name && cp $F Workspace/$F

This could be expanded out, also, to rename the file:

> F=my_file_name && cp $F Workspace/$F.new
share|improve this answer
    
Thanks. I actually need to do this in situations where the second use of the filename is required so my choice of using cp for the question was a poor choice. –  Rich Sep 3 '10 at 7:02

You could try this:

cp my_file_nameCtrl+WCtrl+Yoriginals/Ctrl+Y

But that doesn't work when you are already at the end of the line. Perhaps something similar might work though. (and with copy, not cut)

Edit: Try Alt+B, Ctrl+W, Ctrl+Y, End, Ctrl+Y — a bit of a messy solution though!

share|improve this answer

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.