I tend to find myself in the terminal.app on my Mac more often than not, SSHing into servers, checking whois, host, and any number of other things. My most frequent incantation is searching the command history so I don’t have to re-type a previous command, something like this:
grep curl ~/.bash_history
I’ll then copy and paste the command and hit return. It’s pretty quick, and I’ve got the muscle memory right down now… but there’s got to be more elegant commands, right? Or course there are.
I remember Mike Little explaining various quicker ways to do this on the train back from Edinburgh, but as my memory is a sieve I had to go and Google them just now. Hopefully writing the incantations down will brand them into my neurons… here goes.
To view the command history naturally, the command is pretty simple:
SWMBP:~ simon$ history 3 sh ./netbeans.sh ~/Projects/Promotone/htdocs/index.php 6 4 sh ./netbeans.sh 5 nbopen ~/Projects/Promotone/htdocs/index.php 6 6 nbopen ~/Projects/Promotone/htdocs/index.php 7 sh ./netbeans.sh ~/Projects/Promotone/htdocs/index.php 8 sh ./netbeans.sh ~/Projects/Promotone/htdocs/index.php 9 sh ./netbeans.sh ~/Projects/Promotone/htdocs/index.php 10 sh ./netbeans.sh ~/Projects/Promotone/htdocs/index.php 6 11 sh ./netbeans.sh ~/Projects/Promotone/htdocs/index.php 8 12 sh ./netbeans.sh ~/Projects/Promotone/htdocs/index.php 8 13 ls -alh 14 chmod u+x netbeans.sh 15 ls -alh 16 mv netbeans.sh netbeans.command 17 ls -alh 18 open . 19 pwd 20 apache2ctl restart 21 cd ~/Projects/FSD 22 svn info .
This is better than simply less
ing ~/.bash_history because you can use those numerical prefixes to execute the command. So to repeat command 15 I can just hit:
!15
I can search the command history by piping the results of history into grep
; so to find all previous incantations involving curl
I can use:
SWMBP:~ simon$ history | grep curl 66 curl http://google-maps-utility-library-v3.googlecode.com/svn/trunk/markermanager/src/markermanager.js > ~/Projects/Promotone/htdocs/MarkerManager/markermanager.js 67 curl http://google-maps-utility-library-v3.googlecode.com/svn/trunk/markermanager/examples/weather_map.html > ~/Projects/Promotone/htdocs/MarkerManager/examples-weather-map.html 104 curl -I http://google-maps-utility-library-v3.googlecode.com/svn/trunk/markermanager/docs/reference.html 105 curl http://google-maps-utility-library-v3.googlecode.com/svn/trunk/markermanager/examples/weather_map.html | subl 106 curl http://google-maps-utility-library-v3.googlecode.com/svn/trunk/markermanager/docs/reference.html | subl 107 curl http://google-maps-utility-library-v3.googlecode.com/svn/trunk/markermanager/examples/weather_map.html | subl 209 curl -I http://wwwrollingstones.wpengine.netdna-cdn.com/wp-content/themes/rolling-stones-2012/style.css?ver=8 211 curl -I http://wwwrollingstones.wpengine.netdna-cdn.com/wp-content/themes/rolling-stones-2012/style.css?ver=8 228 curl http://www.4wfilm.org/
Again, I can use the command history number to execute a command, e.g. !67
.
There’s a simpler way to search for the immediately previous command, so I can search for the last use of curl
by hitting ctrl+r
which gets me:
(reverse-i-search)`curl': curl http://www.4wfilm.org/
I can then hit enter, or hit the right arrow to edit the command.
Finally there’s the simple up arrow, to iterate through all previous commands in turn. Oh, and not forgetting tab
completion for commands and paths: so I can type cur
and then hit tab once, and it’ll complete to curl
…unless I have multiple available paths and commands starting with cur
, at which point I can hit tab twice to see all available possibilities.
So… will I remember these things now I’ve written them up? Only time will tell.
Many thanks and a heavy tip o’the hat towards The Geek Stuff’s 15 examples to master the Linux command line.