11

less already uses j/k for vertical scrolling, but not h/l for horizontal scrolling (in --ch mode). The l key isn't bound to anything anyway, and h is just a synonym for H, so I wouldn't overwrite any important bindings.

How can I make h and l scroll horizontally?

Anna
  • 1,139

2 Answers2

9

man less tells us the following:

You  may  define your own less commands by using the program lesskey
(1) to create a lesskey file.  This file specifies a set of  command
keys  and  an  action  associated  with  each key.  You may also use
lesskey to change the line-editing keys (see LINE EDITING),  and  to
set  environment  variables.  If the environment variable LESSKEY is
set, less uses that as the name of  the  lesskey  file.   Otherwise,
less  looks  in  a  standard  place  for  the  lesskey file: On Unix
systems, less looks for a lesskey file called "$HOME/.less".

It tells us to use lesskey to generate a lesskey file reading man lesskey fills in the details. You can put the following in the lesskey input file (~/.lesskey by default)

h left-scroll
l right-scroll

Then run lesskey, and it will generate an output file (~/.less by default) for you.

As far as I can tell, you can't do character-by character horizontal scrolling.

Shawn J. Goff
  • 46,081
3

You can set your own key bindings with the lesskey program. Create a file called .lesskey in your home directory containing your keybindings, then run the lesskey command to convert them into a form that less understands (the compiled bindings are stored in ~/.less).

There is no command that scrolls one character left or right. You can do that by passing the numeric argument 1 to the left-scroll and right-scroll commands. You can't directly set a binding for a command with an argument, but you can set a binding to run a command and then parse more keys. So run the noaction command, then parse a key sequence that runs the scroll command with the desired arguments.

h noaction 1\e(
l noaction 1\e)
  • In my case (may be because of a recent less version?), doing the mapping to 1\e( or 1\e) always errored with No bracket in top/bottom line. However, with a mapping of my own for {left,right}-scroll, it did work perfectly! For example: H left-scroll then h noaction 1H (then the same for L and l) – bew Aug 21 '21 at 09:08