Skip to content

Feature request: Option to align search results to page boundaries #702

Description

@kaz-utashiro

Summary

I'd like to propose an option to align search results to page boundaries. This is useful when using less as a pager for page-oriented output, where each screenful represents a logical unit.

Use Case

Some tools generate output designed to be viewed one screen at a time, where each page forms a coherent visual unit. With standard search behavior:

  1. Search results appear at arbitrary positions on the screen
  2. The n command finds the next match, which may be on the same logical page

An option to snap search results to page boundaries would make navigation more intuitive for such content.

Example: nup command

nup is a tool I developed that formats output in multi-column layouts. It currently uses less -F +Gg as the default pager:

Image
nup man grep

This displays the grep manual in a multi-column format (the number of columns is automatically adjusted to fit the screen width), where each screen represents one "page" of formatted content. With the proposed option:

NUP_PAGER="less -SF +Gg --align-page -j1" nup man grep

users can search and navigate page-by-page through the formatted output, with each search result displayed at a page boundary.

Proposed Behavior

  • When enabled and a search match is found, the display snaps to the page boundary containing the match
  • The n command skips to the next page containing a match
  • Works well with -j1 to position the page start at the top of the screen

Implementation

I have a working implementation available at:
https://github.com/kaz-utashiro/less/tree/align-page

The changes are minimal (~44 lines across 3 files) and leverage the existing -a option mechanism.

Note: The current implementation calculates page boundaries based on screen height and line numbers. It does not take --form-feed into account. While it would be possible to allow specifying a custom line count for alignment, the current implementation keeps it simple by aligning to the screen height.

The option name (--align-page) and whether to assign a short option are just suggestions—I leave those decisions to your judgment.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions