Skip to content

Instantly share code, notes, and snippets.

@torsten torsten/cheat.rb
Created Feb 3, 2012

What would you like to do?
cheat.rb: Self-contained script & database to remember and search for uncommon shell commands.
#!/usr/bin/env ruby
# Self-contained ruby script & database to remember uncommon shell commands
# and search for them using the command line.
# Originally written by Torsten Becker <> in 2012.
# Usage: cheat.rb [SEARCHTERM]
# If SEARCHTERM is given, cheat.rb lists only cheats that contain this term.
# Otherwise lists all cheats.
# To customize: Extend this file at the bottom and add your own cheats.
# ANSI Codes:
# 0: reset all
# 1: bright
# 2: dim colors
# 4: underscore
# 31: red foreground
# 33: yellow fg
# Highlight matches with a underlined yellow
HIGHLIGHT = "\033[0;2;4;33m"
def print_tip header, body, search_term=nil
if search_term
regex = /#{Regexp::escape(search_term)}/i
header.gsub! regex do |match|
HIGHLIGHT + match + "\033[0;1m"
body.gsub! regex do |match|
HIGHLIGHT + match + "\033[0m"
print "\033[1m#{header}\033[0m"
puts body
tips =^(\S.+)$/)
tips.shift # first element is always the empty string
# Transform string into a list of tuples
tips = tips.inject([]) do |memory, this|
if memory.last and memory.last.size <= 1
memory.last << this
memory << [this]
search = ARGV.shift
if search
search = search.downcase
tips.each do |header, body|
h = header.downcase
b = body.downcase
if h.include? search or b.include? search
print_tip header, body, search
# Add more cheats here:
Changes MAC address:
sudo ifconfig en1 ether 00:11:22:33:44:55
Delete SVN directories:
find . -name .svn -print0 | xargs -0 rm -rf
Showing extended file attributes:
ls -@ video.m4v
-rw-r--r--@ 1 torsten staff 2.7M Nov 17 00:34 video.m4v 157B
xattr -p video.m4v
Saving as root from within an unprivileged vim:
:w !sudo tee % > /dev/null
Flush DNS Cache:
dscacheutil -flushcache
Tarballing without ._* and .DS_Store files:
COPYFILE_DISABLE=true tar vzcf archive.tgz --exclude=.DS_Store folder
Stopping a crashing xinit:
launchctl remove org.x.startx
Mounting a ramdisk:
diskutil erasevolume HFS+ "ramdisk" `hdiutil attach -nomount ram://1165430`
Generating the raw data for a git impact chart ala GitHub:
git log --pretty=tformat:'---'%at --numstat
Remove *.pyc files:
find . -iname '*.pyc' | xargs -n1 rm

This comment has been minimized.

Copy link

i0rek commented Feb 6, 2012

I like that! My version of this is

alias cheats='more ~/Dropbox/Misc/'

I haven't that much stuff in it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.