How to truncate long matching lines returned by grep or ack

You could use the grep option -o, possibly in combination with changing your pattern to ".{0,10}<original pattern>.{0,10}" in order to see some context around it:

       -o, --only-matching
              Show only the part of a matching line that matches PATTERN.

..or -c:

       -c, --count
              Suppress normal output; instead print a count of matching  lines
              for  each  input  file.  With the -v, --invert-match option (see
              below), count non-matching lines.

Pipe your results thru cut. I'm also considering adding a --cut switch so you could say --cut=80 and only get 80 columns.


You could use less as a pager for ack and chop long lines: ack --pager="less -S" This retains the long line but leaves it on one line instead of wrapping. To see more of the line, scroll left/right in less with the arrow keys.

I have the following alias setup for ack to do this:

alias ick='ack -i --pager="less -R -S"'