570

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.

4 Answers 4

735

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.

4
  • 268
    ... and toggle inside less with the same keys (-, then Shift-S).
    – peth
    Apr 19, 2011 at 16:27
  • 2
    Now that I disabled "line chopping", how do I scroll horizontally?
    – a06e
    Sep 17, 2017 at 17:08
  • 4
    @becko use the right and left arrow keys.
    – xxpor
    Jun 22, 2018 at 4:11
  • 2
    For GNU less, -S does not work in combination with -r
    – Alcamtar
    Jun 22, 2021 at 18:26
48

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

echo "#env" >> ~/.lesskey
echo "LESS = -S" >> ~/.lesskey
lesskey
7
  • 3
    It'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
    Jan 31, 2014 at 22:39
  • 8
    Or even using an alias: alias less='less -S'
    – Nick
    Aug 20, 2015 at 19:04
  • 1
    I'm missing instructions here for undoing these spells...
    – einpoklum
    Nov 1, 2015 at 15:39
  • 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
    Nov 13, 2015 at 10:56
  • 2
    @tripleee running lesskey also modifies ~/.less. I had to remove ~/.lesskey and ~/.less in order to revert the changes.
    – Greg
    Apr 26, 2016 at 16:40
17

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)

4
  • 1
    most 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
    Oct 31, 2012 at 10:39
  • 3
    @haridsv Pretty sure the End key works in less, to go to the end of the buffer; At least in my gnome-terminal Jun 9, 2014 at 16:43
  • less also allows scrolling left/right. Works even when the file wasn't opened with -S option.
    – Owen
    Sep 12, 2018 at 22:25
  • use B to go to file bottom and T to go to file top Aug 30, 2022 at 16:56
9

To setup git so it always does not wrap:

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

You must log in to answer this question.

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