-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathcheat.rb
More file actions
executable file
·125 lines (95 loc) · 2.89 KB
/
cheat.rb
File metadata and controls
executable file
·125 lines (95 loc) · 2.89 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
#!/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 <torsten.becker@gmail.com> 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: http://www.termsys.demon.co.uk/vtansi.htm
# 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_cheat header, body, search_term=nil
if search_term
regex = /#{Regexp::escape(search_term)}/i
header.gsub! regex do |match|
HIGHLIGHT + match + "\033[0;1m"
end
body.gsub! regex do |match|
HIGHLIGHT + match + "\033[0m"
end
end
print "\033[1m#{header}\033[0m"
puts body
end
tips = DATA.read.split(/^(\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
else
memory << [this]
end
memory
end
search = ARGV.shift
if search
search = search.downcase
matching = tips.find_all do |header, body|
header.downcase.include? search or body.downcase.include? search
end
# If cheat is running in a subshell: try to print just
# the body of that cheat so that it can be used inside backticks.
inside_backticks = (ENV["SHLVL"] == "0")
if inside_backticks
if matching.length == 1
cheat = matching.first[1].strip
STDERR.puts "Running `#{cheat}`"
STDOUT.puts cheat
else
puts "echo Found #{matching.length} cheats matching '#{search}'"
exit 1
end
else
matching.each do |header, body|
print_cheat header, body, search
end
end
else
tips.each(&method(:print_cheat))
end
# Add more cheats here:
__END__
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
com.apple.metadata:kMDItemWhereFroms 157B
xattr -p com.apple.metadata:kMDItemWhereFroms 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:
rm **/*.pyc