44

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,706
Mihai Rotaru
  • 2,979

6 Answers6

32

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'
25

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

8

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
  • 181
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.

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.

1

Try this

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