Edit the last or previous command line in an editor then execute

fc [history-number]
If you would like to edit a previous command, which might be long and complicated, you can use the fc (I think it stands for fix command). Invoke fc alone will edit the last command using the default editor (specified by $FCEDIT, $EDITOR, or emacs, in that order). After you make the changes in the editor, save and exit to execute that command. The fc command is more flexible than what I have described. Please 'man bash' for more information.

10
By: haivu
2009-03-20 15:09:43

These Might Interest You

  • Next time you are using your shell, try typing ctrl-x e (that is holding control key press x and then e). The shell will take what you've written on the command line thus far and paste it into the editor specified by $EDITOR. Then you can edit at leisure using all the powerful macros and commands of vi, emacs, nano, or whatever. Show Sample Output


    489
    ctrl-x e
    fool · 2009-03-11 09:26:05 14
  • zsh has a powerful correction mechanism. If you type a command in the wrong way it suggests corrections. What happend here is that dir is an unknown command and zsh suggests gdir, while maybe ls was what you wanted. If you want to execute gdir hit y (yes) If you want to try to execute dir anyway hit n (no) If you want to execute completely different spelt command like ls hit a (abort) and type your command If you want to execute a similar spelt commant like udir hit e (edit) and edit your command. Show Sample Output


    -1
    setopt correct
    evandrix · 2012-09-11 01:47:20 0
  • Next time you are using your shell, try typing ctrl-x ctrl-e # in emacs mode or <ESC> v # in vi mode The shell will take what you've written on the command line thus far and paste it into the editor specified by $EDITOR. Then you can edit at leisure using all the powerful macros and commands of vi, emacs, nano, or whatever. Show Sample Output


    20
    <ESC> v or ctrl-x ctrl-e
    pkufranky · 2011-04-30 11:08:02 0
  • Copies file to a temporary location, edit and set to real file's time stamp then copy back. Assumes access to /tmp and has $EDITOR, but can be replaced with better values.


    0
    edit-notime () { FILE=$1; TMP=`mktemp /tmp/file-XXXXXX`; cp -p $FILE $TMP; $EDITOR $TMP; touch -r $FILE $TMP; cp -p $TMP $FILE; rm -f $TMP; }
    jecxjoopenid · 2012-10-31 00:54:19 0

What Others Think

cool! 'fc $!' is great!
andreyevbr · 483 weeks ago
Good shell builtin. Didn't know it even existed, but I love it.
atoponce · 483 weeks ago
that is command-line-fu-tastic! tnx
mpb · 483 weeks ago
Very nice.
Williebee · 483 weeks ago
To get the 3rd to last history item use a dash in front of the number: fc -3 To get a list of your history type: history then you can call one of the items by its number, like so: fc 666
sudopeople · 483 weeks ago
@andreyevbr: fc is the same as fc $!
haivu · 474 weeks and 1 day ago

What do you think?

Any thoughts on this command? Does it work on your machine? Can you do the same thing with only 14 characters?

You must be signed in to comment.

What's this?

commandlinefu.com is the place to record those command-line gems that you return to again and again. That way others can gain from your CLI wisdom and you from theirs too. All commands can be commented on, discussed and voted up or down.

Share Your Commands



Stay in the loop…

Follow the Tweets.

Every new command is wrapped in a tweet and posted to Twitter. Following the stream is a great way of staying abreast of the latest commands. For the more discerning, there are Twitter accounts for commands that get a minimum of 3 and 10 votes - that way only the great commands get tweeted.

» http://twitter.com/commandlinefu
» http://twitter.com/commandlinefu3
» http://twitter.com/commandlinefu10

Subscribe to the feeds.

Use your favourite RSS aggregator to stay in touch with the latest commands. There are feeds mirroring the 3 Twitter streams as well as for virtually every other subset (users, tags, functions,…):

Subscribe to the feed for: