Sometimes, being rushed, it is possible to type some characters wrong in a command-line. If the command is long, the mistake correction may be cumbersome (using arrow keys). So the problem is: how can I replace characters typed wrong in the previous command as quickly as possible?


One character or more can be replaced with zero, one or more characters as follow:

$ some-wrong-long-command
$ ^wrong^correct^

This quick substitution repeat the last command-line from terminal, replacing wrong string with correct string.


enter image description here

  • Nice, but I actually prefer the arrow keys. And ctrl+arrow and begin/end keys (to jump words or go to start/end of the line directly). Your string replacement solution is nice, but it's easy to make mistakes. Now for cd, that's harmless, but rm could be a desaster. Besides, cd doesn't usually have typos as you'd use tab completion anyway. – frostschutz May 29 '13 at 11:47
  • @frostschutz I used cd just for example; it could be anything. And, personally, I don't think that I can be passible by a typing mistake if I made another mistake 1 second ago – Radu Rădeanu May 29 '13 at 12:06
  • 6
    +1. the ^bad^good form only replaces the first one. for global replace in the previous command use !!:gs/bad/good – glenn jackman May 29 '13 at 12:25
  • 1
    To repeat the substitution, use :&, i.e. ^bad^good^:& will change bad bad bad to good good bad. You can keep stacking :& but after two probably you should use :gs as suggested. – Chan-Ho Suh May 29 '13 at 20:50
  • You can also explore the vi mode in bash. This seems to make these things easier. – Phani Oct 1 '14 at 0:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.