25

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?

19

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)
    

    Then run lesskey, which converts the human-readable ~/.lesskey into a binary file ~/.less that less reads when it starts.

19

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

  • 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 at 11:39
  • 1
    @Trismegistos Yes, you can set the environment variable in your ~/.bashrc with export LESS="-# 1" – wisbucky Apr 9 at 21:54
  • 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 at 21:56
8

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

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

  • This is not true. More is wrapping be default and there is no -S option. – Trismegistos Feb 21 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 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.