How to turn off word-wrap in less

446

62

Short version: How can I make the less utility in Linux not wrap lines?

Long version: Often I need to view huge CSV files using less with hundreds of columns. I frequently only care about the first couple columns. However, word wrap causes one line to become several lines even on wide-screen monitors.

User1

Posted 2011-04-19T14:48:26.573

Reputation: 7 653

Answers

567

Note: For the key binding, see the second part.

In less, it's called line folding rather than line wrapping.  To set it not to fold, use the -S option:

-S, --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.

less(1)


Alternatively, as mentioned in the below comment, if you already opened the file, you can toggle the mode by typing -S (and then Enter for some implementations).

After disabling line folding, you can scroll sideways with the arrow keys.

Jaap Eldering

Posted 2011-04-19T14:48:26.573

Reputation: 7 596

1Now that I disabled "line chopping", how do I scroll horizontally? – becko – 2017-09-17T17:08:54.057

3@becko use the right and left arrow keys. – xxpor – 2018-06-22T04:11:21.417

210... and toggle inside less with the same keys (-, then Shift-S). – peth – 2011-04-19T16:27:49.420

45

If you want to stop wrapping permanently, cast these spells:

echo "#env" >> ~/.lesskey
echo "LESS = -S" >> ~/.lesskey
lesskey

Henrik Heino

Posted 2011-04-19T14:48:26.573

Reputation: 561

Yes, using the env var LESS seems to be a bit more straight forward: LESS=-S less logfile.txt – Nick – 2015-08-20T19:00:50.547

5Or even using an alias: alias less='less -S' – Nick – 2015-08-20T19:04:36.077

1I'm missing instructions here for undoing these spells... – einpoklum – 2015-11-01T15:39:36.470

1@einpoklum The shell commands simply add two lines to the end of the file .lesskey in your home directory. They are easy to remove with a text editor. – tripleee – 2015-11-13T10:56:55.663

2@tripleee running lesskey also modifies ~/.less. I had to remove ~/.lesskey and ~/.less in order to revert the changes. – Greg – 2016-04-26T16:40:56.333

Change less (pager) default options – OrangeDog – 2016-10-31T15:52:37.110

1It's far more straightforward, IMO, to simply set the LESS variable in your shell RC. I'm not sure if the LESS variable is supported as widely as the very old lesskey mechanism, but if so, I'd recommend using it. – Ryan Long – 2014-01-31T22:39:47.330

15

Don't know if less has a option for that, but I use the most command which does that by default (and allows scrolling left/right to view it)

jor

Posted 2011-04-19T14:48:26.573

Reputation: 636

less also allows scrolling left/right. Works even when the file wasn't opened with -S option. – Owen – 2018-09-12T22:25:52.583

1most seems like a nice program, but I can't believe it doesn't have a shortcut to go to the end of the file. The convenient less command "G" asks for line number and doesn't recognize "$". While it seems like it mimics less in certain ways, I don't understand why the author didn't make it fully compatible. – haridsv – 2012-10-31T10:39:28.703

3@haridsv Pretty sure the End key works in less, to go to the end of the buffer; At least in my gnome-terminal – ThorSummoner – 2014-06-09T16:43:41.713

5

To setup git so it always does not wrap:

git config --global core.pager 'less -S'

user566245

Posted 2011-04-19T14:48:26.573

Reputation: 151