Fix Command (FC) utility

Posted on June 14, 2021 by abhinav

Fix Command(FC) is a nifty utility to edit the previous command. This command isn’t new to me but it has also not been something I use often. Primarily because I never got into the habit of using it and also I don’t make mistakes in commands I type that often coughs_not_true_coughs.

But recently I came across a situation where I had to navigate my commands history and perform a combination of cursor navigation along with backspaces to edit said commands. This was starting to get annoying real fast!

If you are wondering, what command I needed to edit and run multiple times it’s terraform commands. I needed to run the terraform plan/apply commands on multiple modules and pressing left arrow and backspace to make changes started to get annoying after doing it couple of times.

In an attempt to improve the above workflow I learned about the power of fc. I always thought fc was to edit previous command(never thought to look beyond it).I learned that fc also takes a number which corresponds to the line number in the history.

Let’s say history command prints out something like this

$ history 
38 ls
39 terraform init
40 vim fun.txt
41 emacs -nc serious_work.py
...
45 terraform plan --target=module.abc
...
50 terraform plan --targe=module.def
51 ls

now if I wanted to edit my terraform command I can do fc 39. And it will open up my editor with terraform init in it.

But the more interesting thing I wasn’t aware of is you can give it text as well. $ fc terraform would automatically put the last command that matched terraform and put it in the editor. This is pretty cool. But that was still not enough for my usecase.

For my situation I need to this:

$ fc -l terraform
39 terraform init
45 terraform plan --target=module.abc
50 terraform plan --target=module.def

$ fc 50

So in the above snippet I use -l option to get the list of commands that match my search. Then use fc along with the number to edit it. If you don’t give it a text it is equivalent to history just that give lists the last n commands. But if you want the list between a range you can do that too fc -l 100 200 will list commands in the range 100-200 in your history.

Ever since I learned about this, I am using fc a lot more now.

FC will be lot more powerful if you have an editor you have a good command over. *>.> me_looking_at_vim_and_emacs*

Next time you make a mistake in a command and want to edit or want to edit a command you typed some time back give fc a try!

Edit 1:

You can combine history with fc like this

fc !terraform