Monday, 9 July 2012

What was that command again?

I get most of my work done via a command line interface, I use iTerm2 on my MacBook Air which lets me set up profiles run full screen and has many other useful features.

One very useful command when working on the terminal is the history command, it lists all the commands you have entered in that session numbered in the order you entered them, it also shows the time. Below is an example,


dev-macbook # history
....
460  20:04 > vagrant destroy
461  20:04 > vagrant up
462  20:06 > vagrant ssh
463  20:07 > vim node-init.sh 
464  20:08 > vagrant up
......

You can then repeat a command by typing its number with a bang (Exclamation mark) preceding it, it will then show the command in the prompt, but it won't run it, this is useful incase you accidentally enter the wrong number or need to tweak the command before executing it.

dev-macbook # !461
dev-macbook # vagrant up

To make it even easier to use I have this alias in my .bashrc file.

alias h='history | grep'

This means I can filter my history with the following easy command, showing the history containing the word git.

dev-macbook # h git
....
109  19:40 > vim .git/config 
111  19:41 > git pull
279  21:30 > cd .git/
289  21:45 > cd .git/
296  21:49 > vim .git/config 
316  21:51 > vim ../devops/puppet/.git/config 
......

Hope that helps, I will try and share some more tips along the way, but next I will return to the main topic.

No comments:

Post a Comment