Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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)"

share|improve this question

5 Answers 5

up vote 10 down vote accepted

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'
share|improve this answer
    
yep, this is what I was looking for, thanks Gilles ! –  Mihai Rotaru Sep 15 '10 at 21:32
3  
More portably: tput rmam; ls -l longname; tput smam –  Dennis Williamson Sep 15 '10 at 22:29
    
even better; but where did [rs]mam come from ? I searched tput and termcap manuals but couldn't find anything about them ? –  Mihai Rotaru Sep 16 '10 at 0:26
1  
@Mihai: try the terminfo documentation (man 5 terminfo on Linux). –  Gilles Sep 16 '10 at 7:23
    
indeed, it's there –  Mihai Rotaru Sep 16 '10 at 12:09

Here's the .bash_profile alias I'm using to automatically disable wrapping when grepping:

alias grep='tput rmam; { sleep 20 && tput smam & }; grep'

tput rmam disables wrapping.

{ sleep 20 && tput smam & } will reactivate wrapping in 20 seconds. The & at the end makes it run in the background so grep can work in the meantime. You need to reactivate wrapping because not having it messes with your terminal. In my case, tput rmam caused commands longer than the terminal width to overwrite themselves instead of wrapping to a new line (very annoying).

And then grep does its magic, with no wrapping! If it takes longer than 20 seconds to run, though, wrapping will come back with a vengeance, so you may want to set it longer depending on your system.


EDIT:

An even better approach is to use a function that automatically runs tput ram before your grep and tput smam after:

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

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

share|improve this answer

Try this

function nowrap { export COLS=`tput cols` ; cut -c-$COLS ; unset COLS ; echo -ne "\e[0m" ; }
share|improve this answer
    
sorry for the gold shovel –  drigoSkalWalker Jan 11 '13 at 1:15

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

share|improve this answer

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.

share|improve this answer
    
it works, but I lose color coding; any way of preserving that as well ? –  Mihai Rotaru Sep 15 '10 at 11:37

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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