Running Almost the Same Command

Problem

After running a long and difficult-to-type command, you get an error message indicating that you made one tiny little typo in the middle of that command line. Do you have to retype the whole line?

Solution

The !! command that we discussed in Repeating the Last Command allows you to add an editing qualifier. How good are your sed-like skills? Add a colon after the bang-bang and then a sed-like substitution expression, as in the following example:

$ /usr/bin/somewhere/someprog -g -H -yknot -w /tmp/soforthandsoon
Error: -H not recognized. Did you mean -A?

$ !!:s/H/A/
/usr/bin/somewhere/someprog -g -A -yknot -w /tmp/soforthandsoon
...

You can always just use the arrow keys to navigate your history and commands, but for long commands on slow links this syntax is great once you get used to it.

Discussion

If you’re going to use this feature, just be careful with your substitutions. If you had tried to change the -g option by typing !!:s/g/h/ you would have ended up changing the first letter g, which is at the end of the command name, and you would be trying to run /usr/bin/somewhere/someproh.

If you want to change all occurrences of an expression in a command line, you need to precede the s with a g (for global substitution), as follows:

$ /usr/bin/somewhere/someprog -g -s -yknots -w /tmp/soforthandsoon
...

$ !!:gs/s/S/
/usr/bin/Somewhere/Someprog -g -S -yknotS -w /tmp/SoforthandSoon
...

Why does this g have to appear before the s and not after it, like in sed syntax? Well, anything that appears after the closing slash is considered new text to append to the command—which is quite handy if you want to add another argument to the command when you run it again.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.117.72.224