Tech, Gadgets, Photography, Social Media and Poor Spelling

OSX Tip: Re-Run the Last Command While Replacing Syntax or Typos

If you’ve ever entered a lengthy command into the terminal and upon execution discovered asyntax error, a “no such file or directory”, or “command not found” message because you made a typo, you’ll love this quick tip that lets you rerun the last executed command while replacing the erroneous syntax. You’ll need to be using the default bash shell for this to work.

The general syntax to use is as follows:


!! runs the last command again, while :gs/old/new replaces instances of the text “old” with “new” within the last command.

For example, here’s a simple change directory command with an error in the path:

cd /System/Library/CoerServices/

Notice “CoerServices” should be “CoreServices”, but rather than type out the entire command and path again, you can enter the following:


And suddenly you’re in the proper directory.

This is obviously extremely useful when you’ve discovered errors floating in command line syntax somewhere, but it’s also perfect for toggling settings on and off through things like defaults write commands by replacing “no” with “yes” and so forth:


You can also place sudo in front of !! to run the previous command as root.

Source: OSXDaily


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s


This entry was posted on June 24, 2012 by in Apple, howto, OsX, osxdaily, tip and tagged , , , , , .
%d bloggers like this: