keepcalculator

06 Numbers and Powers

Gnu less Keyboard Shortcuts: Full List

Card No. 06·40319 · Updated · Standard formula, shown below

The calculator on this card is switched off while it is checked. The formula is below, and the worked example shows how to do the sum by hand.

GNU Less is a terminal pager that lets you scroll through text files, and its keyboard shortcuts are the key to moving efficiently through long documents. This article lists the essential Less shortcuts for navigation, searching, and file control, so you can read and inspect files faster without opening a full editor.

Formula

The number of screens needed to display a file is the total number of lines divided by the number of lines that fit on one screen, rounded up: Screens = ceil(Total lines / Lines per screen). The last screen starts at line Total lines – (Total lines mod Lines per screen) + 1 (if the division is exact, the last screen is the final full screen). These formulas are useful for estimating how many times you’ll press the spacebar to reach the end of a file.

Worked Example

Using the default values: a file has 1000 lines and your terminal shows 50 lines per screen. Screens = ceil(1000 / 50) = 20. The last screen starts at line 1000 – (1000 mod 50) + 1 = 1000 – 0 + 1 = 1001, which is beyond the last line, meaning the final screen begins at line 951 (since 1000 mod 50 = 0, the last full screen is lines 951–1000). The calculator gives 20 screens and 1001 as the start line of the (non-existent) next screen, which is correct for the formula.

What is GNU Less?

Less is a terminal pager, a program that displays text files one screen at a time. It was written by Mark Nudelman in the 1980s as a more capable alternative to the older more command. Unlike a text editor, Less doesn’t modify the file; it only views it. It’s available on virtually every Unix-like system (Linux, macOS, BSD) and can be installed on Windows via tools like Git Bash or WSL. The name is a pun on “less is more.”

Less supports both forward and backward navigation, something more lacked. It also allows searching, bookmarking, and even following a file as it grows (like tail -f). Because it’s lightweight and starts instantly, it’s the default pager for man pages and many command-line tools.

Basic Navigation Shortcuts

These are the most commonly used keys. They work in the default mode (not in the editor mode invoked with v).

  • Spacebar or f: scroll forward one screen.
  • b: scroll backward one screen.
  • Enter or e: scroll forward one line.
  • y: scroll backward one line.
  • d: scroll forward half a screen.
  • u: scroll backward half a screen.
  • g: go to the beginning of the file.
  • G: go to the end of the file.
  • 50g: go to line 50 (type a number then g).
  • 50%: go to 50% of the file (type a percentage then %).

These keys work in both directions, which is a major advantage over more. You can also use the arrow keys, Page Up/Down, and Home/End on most systems.

Search and Find Shortcuts

Searching in Less is similar to Vim. You can search forward or backward, and Less highlights matches.

  • /pattern: search forward for pattern (e.g., /error).
  • ?pattern: search backward for pattern.
  • n: repeat the search in the same direction.
  • N: repeat the search in the opposite direction.
  • &pattern: display only lines that match the pattern (filter). Press & again to clear.

Search patterns are case-sensitive by default. To make them case-insensitive, start with -I when launching Less (e.g., less -I file.txt) or type -I inside Less. You can also use ^ and $ to anchor to line start/end, and .* for any characters.

File and Session Control

Less can open multiple files and manage them without leaving the program.

  • :n: go to the next file in the argument list.
  • :p: go to the previous file.
  • 😡: go to the first file.
  • :e file: open a new file inside Less.
  • v: open the current file in the system editor (usually Vim).
  • F: follow mode, like tail -f; press Ctrl-C to stop.
  • h: show the help screen (a summary of all commands).
  • q: quit Less.

When viewing multiple files, Less shows a prompt like (file 2 of 3) at the bottom. The :n and :p commands are essential for quickly jumping between logs or config files.

Marking and Jumping

Less lets you set marks (like bookmarks) to return to specific lines.

  • m followed by a letter (e.g., ma): set a mark at the current line.
  • ‘ (single quote) followed by the same letter: jump to that mark.
  • ” (two single quotes): jump back to the previous position.

This is handy when you’re reading a long file and need to check something earlier, then return to where you were. Marks are per-session and are lost when you quit.

Common Problems and Misconceptions

Problem: Arrow keys don’t work over SSH. Some terminals or SSH clients don’t send the correct escape sequences. Use j and k to move up/down one line, and Space/b for pages.

Problem: Search is case-sensitive when you don’t want it to be. Use -I to toggle case-insensitive search. You can also type /pattern for word boundaries.

Misconception: Less can edit files. It cannot; it’s a pager. To edit, press v to open the file in the default editor.

Problem: Less doesn’t show colors. By default, Less doesn’t interpret ANSI color codes. Use less -R to display raw colors, or less -r to pass them through (though this can cause display issues).

Problem: The file is binary or has very long lines. Use less -S to chop long lines instead of wrapping, and less -b to handle binary files safely.

Alternatives and Comparisons

Less is not the only pager. Here are common alternatives:

  • more: Older, simpler, only forward navigation. Still available on most systems.
  • most: Supports multiple windows and cut/paste, but less common.
  • Vim: A full editor that can also view files, but heavier and has a steeper learning curve.
  • bat: A modern clone with syntax highlighting and line numbers, but it’s an external package.

For most tasks, Less is the best balance of speed and features. Its shortcuts are also similar to Vim, so learning them helps if you later use Vim.

Frequently Asked Questions

How do I search for a word in Less? Type /word and press Enter. Use n to go to the next match, N for the previous. To search backward, use ?word.

How do I exit Less? Press q. That’s it. You can also press Q in some versions, but q is universal.

How do I scroll up in Less? Press b to go back one screen, or y for one line. You can also use the arrow keys or Page Up.

How do I go to a specific line number in Less? Type the line number followed by g (e.g., 100g). To go to a percentage, type the percentage followed by % (e.g., 50%).

Why doesn’t Less show colors? By default, Less doesn’t interpret ANSI color codes. Use less -R to display colors, or set the LESS environment variable to -R.

Can I use Less to view multiple files at once? Yes, pass multiple files as arguments (e.g., less file1.txt file2.txt). Use :n and :p to switch between them.

Related

Formula used
in t | Total lines | - | 1000 | 1 | 1000000
in s | Lines per screen | - | 50 | 1 | 500
out screens | Total screens | - | ceil(t / s) | 0
out last | Last screen start line | - | t - (t mod s) + 1 | 0

Each line is one input (in, sel, list) or one result (out: label, unit, formula, decimals). How the calculators work

Method: the standard formula for this calculation, rounded as each result states. See how the calculators work or report an error.

More cards in this drawer

The whole Numbers and Powers drawer →