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 regularly find myself having to execute a lengthy command on a file, then process the results with other commands. To process the next file, I usually rerun the same command by hitting the Up key until I find the command I want and arduously replace the old filename with the new filename.

Is there a way to combine caret substitution (^oldfile^newfile) with the n th-last command? I have (unsuccessfully) tried to pipe the n th-last command into the substitution like so:

$ !-4 | ^old^new

Of course, I am open to other suggestions. These little shortcuts really help with productivity...

share|improve this question
add comment

2 Answers 2

up vote 5 down vote accepted

You can't do it with a quick substitution directly, because ^foo^bar is shorthand for:

!!:s/foo/bar/

The !! part (which refers to the last command) isn't part of the quick syntax (that's what makes it quick), but you can use the longer syntax directly and then modify the !! to whatever you want:

!-4:s/foo/bar/

I explained as much of the history syntax as I know in this post; the last section includes the :s modifier

share|improve this answer
    
This is pretty much what I'm after, though the title may suggest otherwise (didn't know about the :s modifier!). Thanks a bunch. –  Zaid Aug 22 '10 at 20:28
    
...and !! itself is merely a shorthand for !-1. –  isomorphismes Jan 3 '13 at 19:01
1  
@isomorphismes The linked answer gets into that (and quite a lot more) –  Michael Mrozek Jan 3 '13 at 19:24
add comment

You could write lengthy command as a shell function that takes a filename as a parameter and then just type function filename when you need it.

share|improve this answer
    
I have considered doing this, but the nature of the command makes it more amenable to in-place editing. –  Zaid Aug 22 '10 at 20:06
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.