Tip : Quick editing of a command in Bash

May 23, 2005
Sometimes when you try to execute a long command, it scrolls beyond the screen. Then if you want to modify the command and re-execute it, there is an easy way for it. Just type "fc" which will load the command in your default editor; in my case vi. Now you can modify the command in the editor and exit the editor, and your modified command is executed automatically.
For example try typing the following command in the bash shell and type "fc".
$ find /etc -iname '*.conf' -exec grep -H 'log' {} \;
$ fc
"fc" will bring the last command typed into an editor, "vi" if that's the default editor. Of course you can specify a different editor by using the -e switch as follows:
$ fc -e emacs
To list last few commands, type:
$ fc -l
For the last 10 commands it will be:
$ fc -l -10
To seach for a command, type "CTRL+r" at the shell prompt for starting a search as you type prompt. Once you found your command, press enter to execute it.
If you want to transpose two characters say you typed 'sl' instead of 'ls'. Then move the cursor between 'sl' and type "CTRL+t".

2 comments:

  • SatishTalim

    If it's okay with you, could you send me your email id? You have mine.

  • Awanindra

    Hi Ravi

    Nice it is working well...