Linux

Less is More: Understanding Pagers in Linux

Zachary Carciu • • 7 min read

Less is More: Understanding Pagers in Linux

When you need to read files from the terminal, having a good understanding of the tools available makes all the difference. Pagers are specialized programs designed to display text one screen at a time, giving you control over navigation and search. While less and more are the most common pagers, understanding their differences and capabilities will significantly improve your command-line productivity.


Step-by-Step Instructions

Getting Started with Less

  • Open your terminal and try viewing a long file:

    less /var/log/syslog
    

    If you don’t have a long file, create one for practice:

    seq 1 1000 > numbers.txt
    less numbers.txt
    
  • Navigate through the file using these essential commands:

    • Space or f: Move forward one screen
    • b: Move backward one screen
    • j or ↓: Move down one line
    • k or ↑: Move up one line
    • g: Go to the beginning of the file
    • G: Go to the end of the file
  • Search within the file:

    • /pattern: Search forward for “pattern”
    • ?pattern: Search backward for “pattern”
    • n: Go to next search result
    • N: Go to previous search result
  • Advanced navigation:

    • 10G: Go to line 10
    • 50%: Go to 50% through the file
    • m[a-z]: Mark current position with a letter
    • ‘[a-z]: Return to marked position
  • Exit less by pressing q

Using More Command

  • View a file with more:

    more numbers.txt
    
  • Basic navigation in more:

    • Space: Move forward one screen
    • Enter: Move forward one line
    • /pattern: Search forward (no backward search)
    • q: Quit

Setting Your Default Pager

  • Set less as your default pager by adding to your shell profile:

    echo 'export PAGER=less' >> ~/.bashrc
    source ~/.bashrc
    
  • Configure less with useful options:

    echo 'export LESS="-R -S -# 10"' >> ~/.bashrc
    

    This enables raw control characters (-R), chops long lines (-S), and sets horizontal scroll to 10 characters (-#).

  • Test your pager setting with programs like psql:

    psql -c "SELECT * FROM large_table;" | head -100 | $PAGER
    

Explanation

Pagers are essential tools for viewing text content that doesn’t fit on a single screen. They provide controlled navigation through large files, command outputs, and documentation.

Historical Context

The more command came first, introduced in 1978 as part of the Berkeley Software Distribution (BSD). It was designed to display text one screen at a time, solving the problem of long outputs scrolling past too quickly to read. The name “more” comes from the prompt “more?” that appeared at the bottom of each screen.

Less was created later in 1985 by Mark Nudelman as an improved alternative to more. The name “less” is a play on the phrase “less is more,” implying that despite having fewer limitations, it’s actually more powerful.

Key Differences

Less advantages:

  • Bidirectional scrolling (can go backward)
  • More powerful search capabilities
  • Line-by-line navigation
  • Doesn’t load entire file into memory
  • Extensive customization options
  • Better handling of binary files

More characteristics:

  • Forward-only navigation
  • Simpler interface
  • Loads entire file before display
  • Universal availability (even on minimal systems)
  • Fewer features but more predictable behavior

Setting Environment Variables

Many programs respect the PAGER environment variable to determine which pager to use:

  • psql (PostgreSQL client): Uses PAGER for query results
  • git: Uses PAGER for log, diff, and other long outputs
  • man: Uses PAGER for manual pages
  • systemctl: Uses PAGER for status and log outputs

Common PAGER configurations:

export PAGER=less                    # Use less as default
export PAGER="less -F -X"           # Quit if one screen, don't clear screen
export PAGER="less -R"              # Handle colors properly
export PAGER="more"                 # Use more instead

Advanced Tips

  1. Customizing Less with LESSOPEN: You can configure less to process files before displaying them:

    export LESSOPEN="| pygmentize -g %s"  # Syntax highlighting
    export LESSOPEN="| lesspipe %s"       # Handle archives and binary files
    
  2. Using Less as a Git Pager: Configure git to use less with specific options:

    git config --global core.pager "less -F -X"
    git config --global pager.branch false  # Don't page short outputs
    
  3. Monitoring Files with Less: Use the +F option to follow files like tail -f:

    less +F /var/log/syslog
    

    Press Ctrl+C to stop following, then F to resume.

  1. Less with Multiple Files: View multiple files and switch between them:

    less file1.txt file2.txt file3.txt
    
    • :n: Go to next file
    • :p: Go to previous file
    • :e filename: Examine a new file
  2. Performance Considerations: For extremely large files, less is more efficient because it doesn’t load the entire file into memory. More loads everything upfront, which can be slower and use more memory.

  3. Shell Integration: Create aliases for common pager usage:

    alias ll='ls -la | less'
    alias psg='ps aux | grep'
    alias logs='sudo journalctl -f | less +F'
    

By understanding these pager tools and their capabilities, you can navigate large text files and command outputs much more efficiently, making your Linux terminal experience significantly more productive.


Frequently Asked Questions

When should I use less vs more?

Use less for interactive file viewing, searching, and navigation. Use more when you need maximum compatibility or are working on minimal systems where less might not be available.

How do I make less display line numbers?

Use the -N option when starting less:

less -N filename

Or press -N while inside less to toggle line numbers.

Can I edit files directly in less?

Less is read-only, but you can press v to open the current file in your default editor (set by $EDITOR environment variable).

How do I handle files with long lines in less?

Use the -S option to chop long lines instead of wrapping them:

less -S filename

Then use left/right arrow keys to scroll horizontally.

What if less or more aren’t available?

On minimal systems, you might need to use cat with pipes to head or tail, or install the pagers:

# Debian/Ubuntu
sudo apt-get install less

# RedHat/CentOS
sudo yum install less

How do I exit less quickly without pressing q?

You can configure less to auto-exit for short content using the -F option:

export LESS="-F"

This makes less quit immediately if the content fits on one screen.


Conclusion

Mastering Linux pagers is essential for efficient command-line work. While more provides basic functionality and universal compatibility, less offers superior navigation, searching, and customization options. By setting up your PAGER environment variable and learning the key commands, you’ll be able to handle large files and command outputs with ease. Remember that less truly is more when it comes to functionality, but having both tools in your toolkit ensures you’re prepared for any Linux environment.


Recommended Resources: