Horizontal scrolling in smaller increments with less -S
The only horizontal scrolling commands scroll by half a screenful, but you can pass a numeric argument to specify the number of characters, e.g. typing 4 Right scrolls to the right by 4 characters. Less doesn't really have a notion of “current line” and doesn't split a line into words, so there's no way to scroll by a word at a time.
You can define a command that scrolls by a fixed number of characters. For example, if you want Shift+Left and Shift+Right to scroll by 4 characters at a time:
Determine the control sequences that your terminal sends for these key combinations. Terminals send a sequence of bytes that begin with the escape (which can be written
\e
,\033
,^[
in various contexts) character for function keys and keychords. Press Ctrl+V Shift+Left at a shell prompt: this inserts the escape character literally (you'll see^[
on the screen) instead of it being processed by your shell, and inserts the rest of the escape sequence. A common setup has Shift+Left and Shift+Right send\eO2D
and\eO2C
respectively.Create a file called
~/.lesskey
and add the following lines (adjust if your terminal sends different escape sequences):#command \eO2D noaction 4\e( \eO2C noaction 4\e) \eOD noaction 40\e( \eOC noaction 40\e)
In addition to defining bindings for Shift+arrow, you may want to define bindings for arrow alone, because motion commands reuse the numeric values from the last call. Adjust
40
to your customary terminal width. There doesn't appear to be a way to say “now use the terminal width again, whatever it is at this moment”. A downside of these bindings is that you lose the ability to pass a numeric argument to Left and Right (you can still pass a numeric argument to Esc ( and Esc )).
Then run lesskey
, which converts the human-readable ~/.lesskey
into a binary file ~/.less
that less
reads when it starts.
You can use option "-# " to set the number of columns for horizontal scrolling.
From man page:
-# or --shift
Specifies the default number of positions to scroll horizontally in the RIGHTARROW and LEFTARROW commands. If the number specified is zero, it sets the default number of positions to one half of the screen width.
Example (set horizontal scrolling to 10 columns):
less -S -# 10 my_file
Type a number, which is the number of columns you want to scroll to, then hit the arrow key.
Source: List of useful `less` functions