If you can't mess with your terminal, here's a quick script I rely on heavily:
# Truncate input(s) to the current terminal width
# Usage: trunc [-r] [FILE...]
trunc() {
local B='^' A=
if [ -z "$COLUMNS" ]; then COLUMNS="$(tput cols)"; fi
if [ "$1" = '-r' ]; then shift; B= A='$'; fi
expand "$@" |GREP_COLORS=ms= egrep -o "$B.{0,$COLUMNS}$A"
}
This converts tabs to spaces with expand
and then gives you only the first zero to $COLUMNS
characters of the text in standard input or the given files. It supports -r
to reverse the truncation so you see the last zero to $COLUMNS
characters.
(How? grep -o
"only" shows matching content. The extended regex ^.{0,80}
will match only the first 0-80 characters while .{0,80}$
will only match the final 0-80 characters. I'm using those ranges to ensure that shorter lines still show up. Regexes are greedy, so they'll match as much as is available.)
You can change the width you truncate to by setting the $COLUMNS
variable manually. For example, to truncate to 72 characters, use COLUMNS=72 trunc FILE
.
This script assumes each character displays with a width of one. That's not true for escapes, wide characters, or zero-width characters, so perhaps you want to strip ANSI escape sequences or use my full version of trunc (a perl script), which uses colors to note when content was truncated and preserves colors (their control codes are skipped when measuring the width). It also supports -m
for skipping content in the middle.