less icon indicating copy to clipboard operation
less copied to clipboard

Line wrapping: break at space

Open TheBrainScrambler opened this issue 4 years ago • 5 comments

Currently, less's wrapping will display lines until the terminal's end and will then jump a line and put text that immediately follows. This splits words in the text. Could it be possible to add an option to instead make the line wrapping break at space ? That is, whenever the next word doesn't fit in the current line, it will be put on the next one with what follows. Here is a hack that replicates the behavior I want:

export LESSOPEN="| fold -s -w $(tput cols) %s"

and then just open the desired text file. Would be nice to have this natively in less, because it breaks when less reads from stdin. Basically,

less file.txt

Works, but not

cat file.txt | less

TheBrainScrambler avatar Jan 08 '21 15:01 TheBrainScrambler

You can make a LESSOPEN preprocessor work on stdin by adding a "-" after the pipe symbol. That is, you can use: export LESSOPEN="|- fold -s -w $(tput cols) %s" See the last paragraph in the man page in the INPUT PREPROCESSOR section.

gwsw avatar Jan 08 '21 16:01 gwsw

Right, this works, now I completely read this section. But I still think it would be nice to have a native option instead of using some hack. What do you think ?

TheBrainScrambler avatar Jan 09 '21 13:01 TheBrainScrambler

@TheBrainScrambler A hack? This is how less's manual defines it :-)

polluks avatar Jan 20 '21 16:01 polluks

I added this many years ago, but Mark never said much about it and I stopped trying.

adoxa avatar Feb 11 '21 01:02 adoxa

In my opinion Unix is like a toolbox, you combine different tools together. You have fold(1), use it!

polluks avatar Feb 11 '21 08:02 polluks

@adoxa Hi Jason, in 57dacf280a4e53a0aa883d5153702593298734f4 I've finally gotten around to integrating your word-wrapping change. Let me know if you see any issues with it.

Until now I had been of the philosophy that a pipeline with fold or something similar would suffice, but this feature has been requested often enough, and it's a straightforward enough change, that I think it's worth building it into less.

gwsw avatar Jan 14 '23 22:01 gwsw