40

Whenever some command generates long lines as output ( for example, when ls -l a folder which contains files with long names ), the long lines are wrapped to next line, thus messing up the column structure.

Is there any way of avoiding this ? Something akin to the 'nowrap' vim option ?


update

I noticed an issue with the accepted answer:
if I make an alias like: alias ll="tput rmam; ls -l; tput smam"
and then try to grep it's output: ll | grep foo
it will still print all files, like without the grep.

The solution I found is to put brackets around the whole alias:
alias ll="(tput rmam; ls -l; tput smam)"

karel
  • 13,390
  • 26
  • 45
  • 52
Mihai Rotaru
  • 2,799
  • 5
  • 26
  • 25
  • related https://unix.stackexchange.com/questions/20493/how-to-disable-line-wrap-in-a-terminal – Ciro Santilli OurBigBook.com Mar 09 '18 at 06:43
  • related https://superuser.com/q/600677/105108 – ks1322 Jan 23 '19 at 11:50
  • Small note: if you use curly braces `{ ... }` instead of parenthesis `( ... )`, your command won't be needlessly run in a subshell. Make sure to add a semicolon at the very end, to make it work properly: `alias ll="{ tput rmam; ls -l; tput smam; }"` – Sean Aug 03 '21 at 04:08

6 Answers6

30

Note that this has nothing to do with bash (once you've launched the command, bash just waits for it to finish) and everything to do with the terminal.

Most terminal emulators wrap at the right margin by default. But this can be turned off by using the appropriate control sequence, if the terminal emulator supports it; then long lines are simply truncated:

printf '\033[?7l'
ls -l /a/folder/that/contains/files/with/long/names
printf '\033[?7h'
Gilles 'SO- stop being evil'
  • 69,786
  • 21
  • 137
  • 178
24

If you'd like to be able to do horizontal scrolling instead of truncating the lines, use less -S.

Dennis Williamson
  • 106,229
  • 19
  • 167
  • 187
6

You could use a function like so:

nowrap() 
{ 
   cut -c-$(tput cols); 
}

keep in mind though you will have to prefix commands with nowrap or whatever you name the function.

John T
  • 163,373
  • 27
  • 341
  • 348
  • it works, but I lose color coding; any way of preserving that as well ? – Mihai Rotaru Sep 15 '10 at 11:37
  • @MihaiRotaru Many cli tools that support color disable color when they are outputting to another application. Some such tools have a flag such as `--color=always` which disables this color suppression. – Techrocket9 Sep 26 '22 at 23:10
5

pipe it to less command with -S switch:

ls -l | less -S

Then you can use arrows up/down/left/right to scroll and type q to quit.

qartal
  • 151
  • 1
  • 2
5

You can override a function so that it automatically runs tput rmam before your grep and tput smam after:

function grep () {
  tput rmam;
  command grep "$@";
  tput smam;
}

Drop that in your .bash_profile and whenever you run grep, it'll grep without line wrapping.

This has been heavily edited, apologies to the commentators.

ognockocaten
  • 238
  • 3
  • 8
1

Try this

function nowrap { export COLS=`tput cols` ; cut -c-$COLS ; unset COLS ; echo -ne "\e[0m" ; }