83

I need to view large logs files using a bash shell. I was using less to open the files, but since the lines are too lengthy there is some kind of line/word wrapping going on.

Since the files are Log4J logs, and there is a pattern in the beginning of each line, having lines wrapped makes it difficult to analyze the output, so I started using less -S which chops long lines.

But now I need to use tail -f, and it also line wraps the output. Is it possible to disable line wrap in a bash shell for all the commands?

Note: there is an answer to a different question that mentions the escape sequence echo -ne '\e[?7l', but it seems to not work on bash.

7
  • 5
    Note that this is unrelated to your choice of shell (bash), but it can depend on your choice of terminal (xterm, Terminal, gnome-terminal, konsole, …). See What is the exact difference between a 'terminal', a 'shell', a 'tty' and a 'console'? Sep 11, 2011 at 18:16
  • 4
    As Gilles mentioned, it is a matter of the terminal emulator. For example, in screen, there is a "wrap" command. In xterm, there is a -aw option to prevent automatic line wraps. Likely there are some X resources for other terminal emulators. What emulator are you using?
    – Arcege
    Sep 12, 2011 at 20:04
  • I am using gnome-terminal.
    – nunaxe
    Sep 17, 2011 at 8:53
  • 1
    @Arcege: Actually, it is +aw for xterm :) But, this solves the issue for output (e.g., on ls -l with a bunch on long Windows-ish names from TPB, it works fine), for input, when entering a command, you probably still want wrap (or, at least you don't want it the way it is now on +aw: try write beyond the "column border" and then backtrack for a huge GFX bug). Nov 9, 2012 at 23:37
  • By the way, it seems neither urxvt nor rxvt can disable line wraps. Nov 9, 2012 at 23:50

12 Answers 12

82

Try:

less -S +F filename

=

less --chop-long-lines +F filename

And then:

  • Press Ctrlc to stop tailing and now you can move left and right using cursor keys.
  • Press Shiftf to resume tailing
  • Press Ctrlc, q to quit

less manual: + If a command line option begins with +, the remainder of that option is taken to be an initial command to less.

For example, +F tells less to scroll forward, and keep trying to read when the end of file is reached

1
  • This is great! Been having my terminal span two screens, but since I read this yesterday I just pipe the wide output to less -S. Also very useful on the laptop. Dec 16, 2018 at 12:41
62

Found a good answer from superuser, that works out of the box for gnome-terminal, and probably for other terminals as well:

setterm -linewrap off

enter image description here

5
  • This is setterm --linewrap off for me using gnome-terminal with zsh on Ubuntu 18.04.
    – lindhe
    Jan 24, 2020 at 17:15
  • 7
    how do i scroll horizontally then?
    – whyer
    Oct 30, 2020 at 11:15
  • 1
    You don't. This is just a simple method for reducing noise. Other answers here deal with horizontal scrolling but are more cumbersome. You could even pipe everything into Vim | vim -. Apr 30, 2022 at 22:53
  • If this is in a script, how can I reset the linewrap option off at the end of the script or when the script is killed?
    – Flimm
    Apr 11, 2023 at 12:32
  • @Flimm you have to trap the signals to run a cleanup function
    – EkriirkE
    Feb 16 at 15:14
30

You can temporarily disable line wrapping by entering this command::

tput rmam

To restore line wrapping use this command:

tput smam
3
  • This fixed my issue! More details here: unix.stackexchange.com/a/558771/27685
    – andrew
    May 6, 2020 at 5:24
  • the issue with this is that is shows the latter part of a line instead of the beginning
    – doplumi
    Mar 25, 2022 at 12:33
  • @doplumi for me it shows only the first $COLUMNS columns.
    – Tom Hale
    Jun 26, 2023 at 12:11
22

Supposing you have the COLUMNS variable defined, you can execute

tail -f your-app.log | cut -c -$COLUMNS

otherwise substitute $COLUMNS with the columns width of the terminal, as obtained by stty -a.

10
  • 2
    Another alternative to $COLUMNS (is not immediately updated on SIGWINCH, only on the next prompt) and stty -a (harder to use in script) is tput cols.
    – manatwork
    Sep 11, 2011 at 12:43
  • Thanks enzotib. That works, but it trims the line and we are unable to see the end of the long lines. Is is possible to make cut behave like less -S where the long line are buffered and we are able to see the complete line using the directional keys?
    – nunaxe
    Sep 11, 2011 at 15:12
  • Uhm, not that simple. I'm thinking of wired solutions as e.g. two separate screen windows each with a tail -f | cut or a script emulating in some way the less behavior. But do not have a solution, at the moment.
    – enzotib
    Sep 11, 2011 at 16:16
  • 3
    Side-note: cut only counts bytes; unlike less -S, it's going to screw up on coloured text, or anything with ANSI escapes. Might screw up Unicode too. Jun 8, 2013 at 20:39
  • Yes, less -S is more useful with colored text. You can use less -S -E to exit immediately - useful for cutting colored output at $COLUMNS.
    – blueyed
    May 13, 2015 at 16:42
11

Do you actually need tail -f or would something like less +F do? Since it sounds like you still want an interactive pager, it seems to me it would be much easier to stick with less than to reimplement one yourself.

A final note: have you considered tail -f file | less?

2
  • I made some experiences with screen and xterm as those emulators have options to prevent line wraps. But there is a big difference between truncating the line and hiding the tail of the line so we can view it if we want. Using tail -f file | less -S is not perfect as it seems to disable the -f but it is the best solution I found.
    – nunaxe
    Sep 17, 2011 at 8:56
  • @Boris Marcos 's answer is perhaps better. It uses less directly.
    – adentinger
    Mar 18, 2019 at 21:55
8

Two good answers/examples here

http://www.commandlinefu.com/commands/view/1710/tail-a-log-file-with-long-lines-truncated

tail -f logfile.log | cut -b 1-80

tail -f logfile.log | cut -b -$(tput cols)

One caveat: at least on the built in terminal on my Mac cut does not seem to handle tab characters very well. It seems it displays them with e.g., 8 spaces but just calculates them as 4 space wide or something like that. In other words, if your terminal is 80 characters wide and your output contains several tabs per line you must set the width to maybe 60 or something like that. YMMV.

1
  • cut only counts bytes. It doesn't handle multi-byte characters well. Nor does it handle codes that set colours in the terminal.
    – Flimm
    Apr 11, 2023 at 12:34
3

The terminator (http://software.jessies.org/terminator/) terminal emulator allow to not wrap long lines and has horizontal scrolling (but is written in Java).

4
  • How do I enable it? It doesn't seem to work by default.
    – defhlt
    Aug 29, 2012 at 13:26
  • As of this writing, horizontal scroll is on by default. Great, cross-platform recommendation. Thanks!
    – user7089
    Jan 26, 2013 at 0:18
  • Please make this answer better by showing how one would enable this feature. I've searched in Preferences and can't seem to locate this option. FWIW, I'm running terminator v1.91.
    – AnthonyK
    Jan 18, 2019 at 23:28
  • 3
    @AnthonyK There are two unrelated terminal emulators sharing the name terminator, this one which gentledevil is referring to and that one which you are using.
    – jlliagre
    Feb 11, 2019 at 1:06
3

There are a lot of comments which stray from the question. OP's question was

But now I need to use tail -f, and it also line wraps the output. Is it possible to disable line wrap in a bash shell for all the commands?

Some comments were made about the autowrap feature, stating that not all terminals have it. Perhaps. But (aside from terminator, whose developers document no terminal description) all of the examples given were xterm, rxvt and some look-alike or descendent. Those are all related.

The +aw option in xterm corresponds to the autoWrap resource. Consulting the manual, it says that

   autoWrap (class AutoWrap)
           Specifies  whether  or  not  auto-wraparound should be enabled.
           This is the same as the VT102 DECAWM.  The default is "true".

and in XTerm Control Sequences it says

CSI ? Pm h
          ...
          DEC Private Mode Set (DECSET).
            Ps = 7  -> Wraparound Mode (DECAWM).

which certainly does not "depend on your choice of terminal", since any terminal with VT100-compatibility supports the feature. xterm and rxvt do this, for example. The others do as well.

Whether the feature would be useful to OP is debatable. Suppressing line-wrapping is only one aspect of the problem:

  • The shell knows the width of the terminal—but that can be overridden by setting COLUMNS to a "large" value.
  • Of course that means that applications will spend a lot of time writing on the right-margin (and some, getting it wrong, will start a new line anyway).
  • OP probably assumed that the application would scroll left/right to make the wide terminal usable. (terminator does this — partly — but its other drawbacks cancel that out, except for those who only use the terminal for cat'ing a logfile to the screen).
  • what OP actually is looking for is a set of tools that can be told to disable line-wrap, especially for viewing logfiles. If the terminal works well enough for general use, it is irrelevant to the choice of tools which one uses within the terminal.

There are pagers which can do what is needed, e.g., multitail which lists in its features

Line wrapping can be switched off, after that one can scroll to the left/right with the cursor keys

Being ncurses-based, it should work on any of the cited terminals.

2

There are a few examples on this page piping tail with -f ... from my tests it doesn't work properly, if you really need piping (eg. if you need to pipe something more, like grep) you may use something like:

watch -n 1 'tail -n $(($LINES-3)) file.log | cut -c -$COLUMNS'

not the best for performance probably but works... else if no extra pipping required you may use something different like:

less -S +F file.log
1

Use below options with less. It will disable word wrap and preserve text colors, if specified.

less -SR +F filename

-1

vi

inside vi type

:set nowrap

I beleive there is also a plugin for vi that will give you tail like behaviour.

-3

If you really want to see the full lines you can copy and paste the text into a text editor like Atom or Notepad++ and disable line wraps in there.

You must log in to answer this question.

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