Less Pager: Enable Scroll Wheel And Keep Clear Screen
Hey there, fellow Linux enthusiasts! Ever felt like the less
pager is a bit too eager to clear your screen when you just want to scroll? You're not alone! Many of us have wrestled with this, wanting the smooth scrolling experience we're used to, while also keeping that handy screen clearing functionality. In this article, we're diving deep into how to customize less behavior so it both scrolls with your mouse wheel and clears the screen, achieving that perfect balance of convenience and control.
Understanding the Less Pager and Its Quirks
Before we jump into the fix, let's quickly understand what makes less
tick. less
is a powerful pager program, a command-line utility that displays the contents of a file (or the output of a command) one screenful at a time. It's your go-to tool for viewing large text files in the terminal, allowing you to navigate forwards and backwards, search for patterns, and more. One of its key features is the ability to clear the screen before displaying new content, providing a clean slate for each page. This is controlled by the -X
flag, which, when present in the $LESS
environment variable, prevents less
from clearing the screen.
Now, the default behavior of less
in many systems is to not scroll with the mouse wheel unless you remove the -X
flag. This is where the dilemma arises: remove -X
for scrolling, and you lose the screen clearing; keep -X
, and your scroll wheel is rendered useless. But fear not! We can have our cake and eat it too.
The Challenge: Balancing Scroll and Clear
The core challenge here is that the -X
flag, while preventing screen clearing, also interferes with mouse wheel scrolling in some terminal emulators. This is because the terminal emulator interprets scroll wheel events as commands to move up or down a line, and less
, with the -X
flag, doesn't handle these events in the way we'd expect for scrolling. We need a solution that allows less
to respond to scroll wheel events while still clearing the screen when we navigate to a new page or quit less
.
The Solution: A Multi-pronged Approach
So, how do we achieve this magical combination of scrolling and clearing? There isn't a single, universal answer, as the best approach depends on your specific setup – your terminal emulator, your shell, and your personal preferences. However, we can explore several techniques that, when combined, can deliver the desired result.
1. Taming the $LESS
Environment Variable: The Foundation
The first step is to carefully manage the $LESS
environment variable. This variable dictates the default options passed to less
. To see your current $LESS
settings, simply type echo $LESS
in your terminal. You'll likely see something like -R
or -R -M
, or perhaps -X
lurking in there. To enable mouse wheel scrolling, the -X
flag needs to go. But before we remove it completely, let's consider a more nuanced approach.
Instead of directly modifying $LESS
in your shell's configuration file (like .bashrc
or .zshrc
), which would affect all less
invocations, we can use an alias or a function to control when -X
is used. This gives us fine-grained control over less
's behavior.
Alias Approach
Here's how you can create an alias that invokes less
without the -X
flag for scrolling, while still allowing other commands or scripts to use less
with its default settings:
alias lessscroll='LESS=$(echo "$LESS" | sed "s/-X//g") less'
Let's break this down:
alias lessscroll='...'
: This defines an alias namedlessscroll
. When you typelessscroll
, the command within the quotes will be executed.LESS=$(echo "$LESS" | sed "s/-X//g")
: This is the crucial part. It temporarily modifies the$LESS
variable for this specific invocation.echo "$LESS"
outputs the current value of$LESS
.sed "s/-X//g"
uses thesed
stream editor to remove all occurrences of-X
from the string. The result is then assigned back to theLESS
variable, but only for this command.less
: Finally, this invokes theless
command with the modified$LESS
variable.
With this alias in place, you can use lessscroll
to view files with mouse wheel scrolling, while regular less
commands will still clear the screen. Add this line to your shell's configuration file (e.g., .bashrc
, .zshrc
) to make the alias permanent.
Function Approach
Alternatively, you can use a function to achieve the same result. Functions offer more flexibility than aliases, allowing for more complex logic.
scroll_less() {
local less_opts=$(echo "$LESS" | sed "s/-X//g")
LESS="$less_opts" less "$@"
}
This function does the following:
scroll_less() { ... }
: Defines a function namedscroll_less
.local less_opts=$(echo "$LESS" | sed "s/-X//g")
: Creates a local variableless_opts
and assigns it the value of$LESS
with-X
removed, similar to the alias approach.LESS="$less_opts" less "$@"
: Sets the$LESS
variable to the modified value for this function's scope and then invokesless
with all the arguments passed to the function ("$@"
).
You can use this function just like the alias: scroll_less your_file.txt
. Add this function definition to your shell's configuration file to make it persistent.
2. Terminal Emulator Configuration: The Key to Scroll Handling
While modifying $LESS
is essential, it's often not enough. The terminal emulator plays a crucial role in how scroll wheel events are interpreted and passed to applications like less
. Many terminal emulators have settings that control scrollback behavior and how scroll wheel events are handled.
For example, in GNOME Terminal, you might need to adjust the