Submit Hint Search The Forums LinksStatsPollsHeadlinesRSS
14,000 hints and counting!

Correct command line typos with carets UNIX
We were arguing the most useful command line editing feature at a meeting yesterday. The winner was Ctrl-R (backwards-history-search) until someone said, "Do you guys know about carets?" He explained:

Say you type a command to list the contents of the directory /arch/Xapps/packages, but you type it incorrectly, like this:
 % ls /arch/xapps/packages
Augh! The 'x' is lowercase! Instead of retyping the command with an uppercase 'X', or hitting a bunch of keystrokes to modify it, type this and hit the return key:
 % ^xapp^Xapp
When you hit return, the shell grabs the previous command, substitutes "Xapp" for the first occurrence of "xapp" and executes the command. So typing ^xapp^Xapp and return would execute what you wanted in the first place:
 % ls /arch/Xapps/packages
Saves some typing and time when the fingers work faster than the keyboard!
  • Currently 0.00 / 5
  • 1
  • 2
  • 3
  • 4
  • 5
  (0 votes cast)

Correct command line typos with carets | 8 comments | Create New Account
Click here to return to the 'Correct command line typos with carets' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
Can do it even faster
Authored by: hayne on Jul 10, '02 12:42:17PM

You can do it with fewer keystrokes:
% ^x^X
will change the first "x" to an "X" - which is all that is required in the example given.

This 'caret' technique is even more useful when combined with other shell history features. E.g. suppose I recall that I had sometime in the recent past copied a file to some directory and I now wanted to copy a different file to that same directory. I could get that previous command to be the latest one in my history list by using ":p":


This finds the last command where I used 'cp' and echos it (without executing it). Suppose it shows up as:
cp foo /Volumes/Disk42/long/directory/path
then to copy the file "bar" to the same directory, I need only do:

[ Reply to This | # ]
Can do it even faster
Authored by: seb2 on Jul 11, '02 04:10:00AM

Not only does ":p" show the line that matched, it also appends it to your history, so if the command was right, a single hit on the cursor key "arrow up" brings you there. -- I use it all the time.

[ Reply to This | # ]
Case Insensitive
Authored by: Chas on Jul 10, '02 01:09:17PM

This is a poor example since HFS+ is case-insensitive (sort of). I get your point about editing, but you wouldn't ever need to change the case in a statement like you describe.

[ Reply to This | # ]
Case Insensitive
Authored by: Anonymous on Jul 11, '02 04:16:34AM

Yeah, I'm sure we all get the idea, but it was a pretty stupid example on a Mac OS X site when 99% of Mac OS X users are using the case insensitive HFS Plus!

[ Reply to This | # ]
Option Key
Authored by: JohnnyMnemonic on Jul 10, '02 01:52:25PM
I posted this as a hint before, but it seems relevant here: in terminal preferences, you can turn on "option-select" which allows you to position the cursor in a command line under the mouse if you hold down option at the same time.

Therefore, I would correct the example by 'up-arrow', 'opt-click' after 'x', delete x, type X. No backspacing needed.

[ Reply to This | # ]
Option Key-not for WACOM
Authored by: kyngchaos on Jul 11, '02 01:02:16PM

I recall seeing that hint a while back, but forgot about it. Handy, but unfortunately option-clicking with my WACOM tablet doesn't work, I must drop my pen and click with a mouse, or the trackpad on my PB. Bummer.

[ Reply to This | # ]

Authored by: eisforian on Jul 10, '02 03:13:27PM
(Using ^xapp^Xapp instead of ^x^X was purposely done for the example. Additionally, I often work with my university's software hierarchy on Solaris machines, which is case sensitive and explains the /arch/Xapps. :-)

[ Reply to This | # ]
Correct command line typos with carets
Authored by: jonbauman on Jan 25, '05 11:37:19AM

I often find that I want to replace something in my previous command (or any previous command, really), but that there is more than one occurrence. For example, if I want to run he following commands

cp foo/bar/baz.txt qux/bar
cp foo/zar/bax.txt qux/zar

You can't use ^bar^zar, because only the first "bar" would be changed. You need to do a global replace. To do that, use the following command:


The s/old/new/ command is a substitution idiom used in many different languages. Adding the g makes it apply globally. Also, instead of !! to refer to the last command, you can use !-n to refer to n commands previous.



[ Reply to This | # ]