Take the 2-minute tour ×

Is there a way to limit the length of the line matched by ack? If ack matches, for example, a minified javascript file, the line it prints out can be very large. I still want it to show the highlighted match, but ideally with the line cut before and after the match with elipses: "... stuff match stuff ...". Not seeing anything conclusive in the ack docs.

I tried digging into it with something like

ack pattern | awk '{print substr($0,1,200) }'

but awk seems to be stripping coloring and formatting, which is undesired. Also I tried (bear with me for a second):

ack pattern | ack pattern

To see if I could re-ack on each line and chop it up or something, but that has the same formatting problem. Piping ack output removes the line breaks apparently?

Edited based on comment:

Maybe it will help to explain the main reason why I want this. I use ack.vim to search in my project. I often want to ack through my javascript files to find a word, like "cookie". We have jQuery and other minified libraries versioned in our code, so they show up in the ack results. Since those files are minified, there are very few line breaks, and it shows a gigantic line in the split window which I have to scroll past. I almost never care about that match so it's annoying to take up so much space. I could just add those minified file names to my ackrc to ignore but I'd prefer not to have to add a new file name every time we add a minified library. There is an open issue on the ack.vim repo about this. I want to know if it's possible to do purely through bash-fu.

share|improve this question
It would be helpful to know what pattern you're matching against, and what the relevant parts of the javascript file look like... I suspect that you're going to have more luck doing pattern matching rather than taking a fixed width substring. –  Barton Chittenden May 13 '12 at 6:03
I added an edit –  Andy Ray May 13 '12 at 23:43
add comment

3 Answers

up vote 2 down vote accepted

You can:

  • use -o option to Show only the part of a line matching PATTERN
  • use -l option to Only print filenames containing matches
share|improve this answer
Hmm, neither are really that helpful, I still want to see the context around a match –  Andy Ray May 14 '12 at 1:16
Modify your pattern to -o '.{0,24}pattern.{0,24}' where 24 is the length of the context you want, and pattern is your original pattern. You could embed this in a script. –  tripleee May 14 '12 at 7:55
Ah, I see. Close to what I want. I'll see if I can modify the vim script to re-add highlighting after the fact –  Andy Ray May 20 '12 at 17:47
I liked @tripleee's solution but note that it made my searching dramatically slower, even for 24 characters! –  Tyler Collier Jan 23 at 5:31
add comment

ack cleverly disables color when it is being piped, but you can override that with --color:

ack --color foo | cut -c1-80 | sed "s/$/\e[0;30m/"

(the last pipe is to reset colors in case text is cut in the middle of selection)

There are similar options for formatting (--heading, --break).

share|improve this answer
add comment

If what you really want is to ignore minified Javascript files, then upgrade your ack. ack 1.96 and beyond ignores minified JavaScript files.

share|improve this answer
add comment

Your Answer


