Have you ever written a long and fancy long shell one-liner, execute it, then an error due to a silly typo sitting in the middle of that command? Or you just need to compose a command which you know will be long before its written?

You can always create a file, but sometimes, its a one-time only task. So, is there any way to get the job done easily? Of course, there is and more than one method.

1   Substitution

For fixing a typo in a command you just type, you can use history expansion:

$ typo bash
-bash: typo: command not found
$ ^typo^type
type bash
bash is /bin/bash

or using fc:

$ fc -s typo=type

or substituting other than last command:

$ !-3:s/foo/bar/

The command above substitutes foo with bar in the third previous command.

2   Editing

If you just run fc, then it brings your editor up and feed it with last command. You can even give fc a range, so you can edit a list of commands at once:

$ fc -5 -3

You will have commands from 5th previous command to 3th previous command in your editor. If you are not sure the numbers, you can use -l option to get a numbered list, then issue the command with according numbers:

$ fc -l
$ fc 100 105

Another easier way to edit current command in Bash is to press Ctrl+X then Ctrl+E, or Esc then V if Vi mode enabled.