6

Like most people I use less as my terminal pager. Occasionally I view files consisting of very long lines of plain or marked-up text. By default less folds these lines at the terminal window edge. That is, words get broken at the last column, making the text difficult to read. Is there any way of getting less to instead wrap the lines at word boundaries, the same way that fmt or Emacs visual line mode does?

I am aware that I could simply pipe the input through fmt before viewing it, though that requires me to know my terminal width in advance. I was hoping there was some way of getting less to nicely wrap lines, and to automatically reformat them when the terminal window is resized.

3 Answers 3

2

No. To verify, download the latest less source and review input.c around line 178:

177             /*
178              * The char won't fit in the line; the line
179              * is too long to print in the screen width.
180              * End the line here.
181              */
182             if (chopline || hshift > 0)
183             {
184                 do
185                 {
186                     if (ABORT_SIGS())
187                     {
188                         null_line();
189                         return (NULL_POSITION);
190                     }
191                     c = ch_forw_get();
192                 } while (c != '\n' && c != EOI);
193                 new_pos = ch_tell();
194                 endline = TRUE;
195                 quit_if_one_screen = FALSE;
196             } else

Formatting in the way that fmt does is non-trivial, performance-wise. The fmt algorithm is about 50 lines of forward and backward scanning to get the optimal layout. Also, the fmt algorithm looks (IMO) wonky if the width you want is significantly larger than the actual contents, so it might not be a good general fit.

man uses less -is by default which IMO is pretty good, but not what you want.

So... I think the only way, though admittedly it doesn't follow terminal resizes, is:

fmt -w $(tput cols) | less
1

Excerpt from the less man page:

-S or --chop-long-lines
    Causes lines longer than the screen width to be chopped rather than folded.
    That is, the portion of a long line that does not fit in the screen width
    is not shown. The default is to fold long lines; that is, display the
    remainder on the next line.
1
  • 1
    I'm afraid this doesn't answer my question. I want lines to be wrapped at word boundaries, not chopped or folded.
    – Psychonaut
    Commented Jun 10, 2014 at 10:40
-1

The best option that I have found for this is to use fmt to format the text and pipe it into less.

fmt file_name | less

That will achieve what you are looking for.

1
  • 4
    In the question, Psychonaut says that he knows that this is an option. If you're saying that this is the only way you know how to do it, that's not really a useful answer. You say "The best option that I have found for this is ...." You could make your answer more valuable if you described at least one other option. Commented Feb 2, 2015 at 23:06

You must log in to answer this question.

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