Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

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?

share|improve this question

1 Answer 1

up vote 7 down vote accepted

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

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

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

Examles:

enter image description here

share|improve this answer
    
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
4  
+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 at 0:51

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.