raw .md file used in screenshot
1

sack

2
3

s(hortcut)-ack: a faster way to use ack (and grep)!

4
5

sack acts as a wrapper for ack to provide convenience for the repetitive menial tasks

6
7

What is ack?

8
9

ack is the replacement for grep!

10
11

Here is why you should use ack over grep: http://betterthangrep.com/

12
13

(Now that you are sold on ack) To install ack in a one-line script:

14
15
curl http://betterthangrep.com/ack-standalone > ~/bin/ack && chmod 0755 !#:3
16
17
18

How to Install:

19
20

Open a terminal and run the following:

21
22
git clone git@github.com:sampson-chen/sack.git && cd sack && chmod +x install_sack.sh && ./install_sack.sh
23
24
25

How to Use:

26
27

You can use sack in exactly the same way you currently use ack! Woot!

28
29

For why sack is faster (and more fun!) to use, read on about its main / side features...

30
31

Main Feature 1 - Shortcuts:

32
33

sack prefixes shortcut tags to ack's search results:

34
35
user@linux:~/src$ sack thumbnail
36
37
... (omitted results)
38
/home/user/src/reviewboard/reviewboard/attachments/mimetypes.py
39
[13] 6:from djblets.util.templatetags.djblets_images import thumbnail
40
[14] 120:    def get_thumbnail(self):
41
[15] 125:        return mark_safe('<pre class="file-thumbnail"></pre>')
42
[16] 135:    def get_thumbnail(self):
43
[17] 136:        """Returns a thumbnail of the image."""
44
[18] 137:        return mark_safe('<img src="%s" class="file-thumbnail" alt="%s" />'
45
[19] 138:                         % (thumbnail(self.attachment.file, size='1000x1000'),
46
[20] 146:    def get_thumbnail(self):
47
[21] 157:        return mark_safe('<pre class="file-thumbnail">%s</pre>'
48
49
50

Now, instead of having to spend time tediously navigating to some deep directory and typing out the file name, simply do:

51
52
user@linux:~$ F 21
53
54
55

And sack will open the file associated with that particular search result with your favorite editor (-cough-vim-cough-) and even go to the right line for you automatically. (It doesn't have to be "F", see config file for reassigning the shortcut command)

56
57

The repetitive 10-15 sec chore has now been reduced to only 2 keystrokes (~1 second)!! More importantly, now you won't lose your train of throught from mentally context switching from the task at hand to deal with menial things like typing out a file path. Yay productivity!!

58
59

(For why removing such distractions is important to coding "in the zone", see the excellent Joel Test: http://www.joelonsoftware.com/articles/fog0000000043.html)

60
61

Cross-Terminal Shortcuts:

62
63

Note that whenever you perform a search in any terminal with sack, you can use the shortcuts in all other terminals (including new ones).

64
65

So now you can use one terminal to keep the search results open for reference, and use other terminals to open the files with shortcuts!

66
67

Main Feature 2 - Profiles:

68
69

About Profiles:

70
71

Profiles allow you to specify "preset" flags and directories, so you don't have to waste keystrokes typing them out each time, or having to change directories to include the folders you need to search.

72
73

For example:

74
75
user@linux:~$ sack -sp RB
76
77
78

will switch you to the "RB" profile. Now any searches via sack will run with this profile's preset flags (e.g. flags="-ia -A 2 -B 3") and under this profile's preset directories (e.g. directories="~/src/reviewboard/ ~/src/rbtools ~/src/djblets")

79
80

Now suppose you are working under some deep directory in terminal:

81
82
user@linux:~/src/reviewboard/reviewboard/reviews/management/commands/diffs$
83
84
85

and you wanted to search the all relevant codebases to find all locations where a particular function is invoked.

86
87

The old options were:

88
89
  • cd to ~/src/, run ack there, and then later cd back to ~/src/reviewboard/reviewboard/reviews/management/commands/diffs$ (arggggghh!!!)
90
  • typing out this on the prompt: ack -i pattern1 ~/src/reviewboard/ ~/src/rbtools ~/src/djblets
91
  • opening a new terminal to run ack
92
93

Now you can simply do:

94
95
user@linux:~/src/...//.../diffs$ sack pattern1
96
97
98

And the rest is taken care of for you.

99
100

Note: The no_profile profile is special: it never uses any preset flags or search directories. sack behaves exactly like ack while under no_profile (except for the shortcuts)

101
102

Which Profile?

103
104

To find out which profile you are currently on:

105
106
sack -wp
107
sack --whichprofile
108
109
110

Switch Profile

111
112

To switch to a different profile (no_profile is the empty profile):

113
114
sack -sp PROFILE_NAME
115
sack --switchprofile no_profile
116
117
118

Rename Profile

119
120

To rename the current profile:

121
122
sack -rp NEW_PROFILE_NAME
123
sack --renameprofile ReviewBoard
124
125
126

Set Flags

127
128

To set new preset flags to use for the current profile:

129 (All searches run using this profile will use these flags)

130
131
sack -sf NEW_FLAGS
132
sack --setflags -ia -A 2 -B 3
133
134
135

Set Directory

136
137

To set new preset directory(s) to use for the current profile:

138 (All searches will be run under this directory with this profile)

139
140
sack -sd DIRECTORY_1 DIRECTORY_2 DIRECTORY_3
141
sack --setdirectory ~/src/reviewboard ~/src/rbtools ~/src/djblets
142
143
144

Add New Profile

145
146

To add a new empty profile:

147
148
sack -anp PROFILE_NAME
149
sack --addnewprofile ReviewBoard
150
151
152

List Profiles

153
154

To show the current available profiles:

155
156
sack -lp
157
sack --listprofiles
158
159
160

ToDo:

161
162

Additional features / functionalities to be implemented:

163
164
  • Implement support for other editors (emacs, sublimeText etc)
165
  • Implement high-light for vim for the searchword when following a shortcut
166
  • Functionality for deleting a profile
167
  • Check that the user has ~/bin in their path
168
  • Add a check in install_sack.sh to also install ack if it's not already on the system.
169
  • Finish implementation for a decrement count for the beginner msg so it displays a certain number of times at the start
170
  • (Suggest a feature that you want for sack!)