Record, run previous commands from history

terminalSuppose you have been working on the terminal for a while and once done you want to edit and save all the commands quickly so that you can re-execute them anytime. There is a neat way to do that.

Make sure history is on. Let’s say you want to edit and save the commands from 500 to 550 in history. Run the following command:

fc 500 550 //fc stands for 'fix command'

This will open the history commands from 500 to 550 in your default editor (mine is vi) as a temporary file. If you want to change the editor or looking for more options, check here. Once you have edited the commands save them in a file. In vi, use :w /path/to/file (absolute path preferred) in command mode. Now when you quit all the commands will execute as a batch script. If you don’t want to run the commands immediately, delete all content (gg followed by dG) and use :wq to exit. This will not remove anything from the previous file you saved, as only the temporary file contents will be deleted.

Leave a Reply

Your email address will not be published. Required fields are marked *