how to make bash not to wrap output?
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)"
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'
If you'd like to be able to do horizontal scrolling instead of truncating the lines, use less -S
.
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.
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.