43

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

3

6 Answers 6

31

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'
6
  • yep, this is what I was looking for, thanks Gilles ! Commented Sep 15, 2010 at 21:32
  • 19
    More portably: tput rmam; ls -l longname; tput smam Commented Sep 15, 2010 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 ? Commented Sep 16, 2010 at 0:26
  • 2
    @Mihai: try the terminfo documentation (man 5 terminfo on Linux). Commented Sep 16, 2010 at 7:23
  • indeed, it's there Commented Sep 16, 2010 at 12:09
25

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

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.

2
  • it works, but I lose color coding; any way of preserving that as well ? Commented Sep 15, 2010 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. Commented Sep 26, 2022 at 23:10
6

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.

1
  • This is clean, simple and effective. Commented Nov 11, 2021 at 15:58
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
1

Try this

function nowrap { export COLS=`tput cols` ; cut -c-$COLS ; unset COLS ; echo -ne "\e[0m" ; }
1
  • sorry for the gold shovel Commented Jan 11, 2013 at 1:15

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .