14

In a (linux) terminal, sometimes it is less important to see line endings, but more important not to clutter the line startings. e.g.

line1 sddd dd ddd
line2 sdafss ss s
line 3 da aaaa aa 

rather than

line1 sddd dd ddd
dd dddd dd
line2 sdafss ss s
s ss
line 3 da aaaa aa 

is there a way to "cut" or "hide" line ending at the end of the terminal's window, in the same manner as "less -S" does, but for kind-of-normal output?

4 Answers 4

25

Line wrap disabling support is terminal dependent. For example if using screen you can hit Ctrl-A Ctrl-R to toggle line wrap.

Otherwise, you might try setterm -linewrap off with or without increasing the number of terminal columns with stty (haven't tried this).

Terminal emulators like PuTTY (if you connect to the server from a Windows box) have their own settings.

If the terminal supports VT escape codes, echo -ne "\x1b[?7l" will disable screen wrap (echo -ne "\x1b[?7h" will enable it).

Notice that what works in one terminal might not work in another - for example I'm now on a PuTTY window on a Linux OpenSuSE 12.3 with bash, using screen: its control sequence works perfectly, while VT codes and term do not. On a text-mode console on a older SuSE 11 (not GUI), the VT sequence works (probably screen would, too), stty is apparently ignored.

1
  • 2
    It's actually "\x1b[?7l" ended in lowecase L
    – idelvall
    Nov 8, 2016 at 15:43
1

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.

1

create a script called nowrap then...

some_command | nowrap

Suggested script:-

#!/usr/bin/perl -w

my $cols=`/bin/tput cols`; chomp $cols;

while(<>){
  chompnl($_); my $tmp=$_; my $add=0;
  while($tmp=~/(.*?)(\033\[[\d\;]+m)(.*)/) {
    $add+=length($2) if(length($1)<$cols);
    $tmp=$1 . $3;
  }
  while($tmp=~/(.*?)\t(.*)/) {
    my $spc=8-(length($1) % 8);
    $tmp=$1 . ( ' ' x $spc ) . $2;
    $add-=($spc-1);
  }

  print substr($_,0,$cols+$add) . "\n";
}

# chomp() on unix doesn't eat "\r"...
sub chompnl {   # Warning: don't stuff up $1
  chop $_[0] while((substr($_[0],-1) eq "\015")||(substr($_[0],-1) eq "\012"));
} # chompnl
1

I am surprised that even after 10 years, no-one has yet mentioned that cut (from gnu coreutils) does exactly what you want. Just pipe your output to cut:

Using a 55-column wide terminal window (just to show that this works):

$ cat cutTest.txt # shows automatic folding/wrapping
Line 1 is too long, line 1 is too long, line 1 is too l
ong, yes line 1 is too long!
Line 2 is too long, line 2 is too long, line 2 is too l
ong, yes line 2 is too long!

$ cut -c-40 < cutTest.txt # cuts after 40 columns/characters
Line 1 is too long, line 1 is too long, 
Line 2 is too long, line 2 is too long, 

$ cat cutTest.txt | cut -c-40 # Using a pipe
Line 1 is too long, line 1 is too long, 
Line 2 is too long, line 2 is too long,

If your terminal tracks the window width (as bash does), you can use the following to cut "at the end of the terminal's window (as per the original question):

$ cat cutTest.txt | cut -c-$COLUMNS
Line 1 is too long, line 1 is too long, line 1 is too l
Line 2 is too long, line 2 is too long, line 2 is too l

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .