Monday, September 2, 2013

Simple Cheat Sheet Search using Grep for your Mac

Often I find myself referring to the same list of commands for utils such as git, hadoop, mysql, ruby, tmux, etc. The usual way of doing it is to just google them and you are very likely to find what you're looking for in a few clicks. A faster way is to keep a cheat sheet of commands you use often and then use a search on that text file when you are looking for commands.

The problem with this approach is that it is just as painful as the first one. Imagine you have 8-10 tabs open at a time and your text editor tab is somewhere in the middle. That's 4 key strokes, Now search for the text thats one more. Now scroll to find the one that you like. Too many keystorkes/mouse events and its just real frustrating.

I created a simple hack that just greps the line you want and shows 2 lines beneath the lines it finds a match on.

dn.sh
#!/bin/sh
read search_string
grep -n -i -A 2 $search_string ~/cheatsheet.txt


So for example if you have your cheatsheet which has command description followed by the command usage and which looks something like this:

. . .
Git Squash Commit (prefix every line to squash with s)
git rebase -i master

Git show remotes
git remote show

Git branches
https://github.com/Kunena/Kunena-2.0/wiki/Create-a-new-branch-with-git-and-manage-branches

Git Delete Branch
git branch -D
 . . .

Then you can open spotlight with Command + Space, type dn, enter. Type your search string "squash" and you will see the following:
315:Git Squash Commit (prefix every line to squash with s)
316-git rebase -i master
317-

No comments:

Post a Comment