11

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?

22

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.

| improve this answer | |
  • 1
    It's actually "\x1b[?7l" ended in lowecase L – idelvall Nov 8 '16 at 15:43
0

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.

| improve this answer | |
0

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
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.