32

I'm using less to parse HTTP access logs. I want to view everything neatly on single lines, so I'm using -S.

The problem I have is that the first third of my terminal window is taken up with metadata that I don't care about. When I use my arrow keys to scroll right, I find that it scrolls past the start of the information that I do care about!

I could just delete the start of each line, but I don't know if I may need that data in the future, and I'd rather not have to maintain separate files or run a script each time I want to view some logs.

Example

This line:

access.log00002:10.0.0.0 - USER_X [07/Nov/2013:16:50:50 +0000] "GET /some/long/URL"

Would scroll to: ng/URL"

Question

Is there a way I can scroll in smaller increments, either by character or by word?

27

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:

  1. 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.

  2. 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.

| improve this answer | |
  • Very nice explanation, thanks for the details. It seems the normal scrolling with Left/Right get switched to 4 characters also after using Shift + Left/Right, any workaround for this? – Slim Jul 26 at 8:36
  • 1
    @Slim Not easily I think. – Gilles 'SO- stop being evil' Jul 26 at 19:47
  • looks like your edit mentioning binding arrow is good enough (for me at least). thanks! – Slim Jul 26 at 20:02
25

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

| improve this answer | |
  • Is there posibility to add this option to some configuration file so I don't have to pass -# 1 option everytime i type the command? – Trismegistos Feb 21 '19 at 11:39
  • 2
    @Trismegistos Yes, you can set the environment variable in your ~/.bashrc with export LESS="-# 1" – wisbucky Apr 9 '19 at 21:54
  • 1
    You can also set a percentage of screen width if the number starts with a decimal point. For example, -# .25 would scroll by 1/4 of the screen width. – wisbucky Apr 9 '19 at 21:56
10

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

| improve this answer | |
2

An alternative is to use the most pager.

In most, no-wrap (-S) is the default and Right scrolls one column to the right instead of a half screen. Use > or Tab to scroll faster.

Another alternative is the w3m pager (also one of the best terminal web browsers) where you can use <, > as well, and ,, . for finer-grain scrolling (same keys on qwerty keyboards).

| improve this answer | |
  • This is not true. More is wrapping be default and there is no -S option. – Trismegistos Feb 21 '19 at 11:48
  • 1
    @Trismegistos, I'm not talking about more (for which there are dozens of implementations), but about most (by John E. Davis, I'm not aware of any other implementation). – Stéphane Chazelas Feb 21 '19 at 12:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.