More on less

Tags: bash, tips

This post will cover some of the useful features in less that will let you look at any text-based document like a wizard!!

Scroll through page-wise

As you may already know, you can use <space> to move one window forward in the document, but did you know there are ways to go back window-wise? Here is a table of some of the useful ways you can scroll through the document more efficiently (NOTE: ^ means press the <control> key with the following key):

Key(s) What it does How to remember
d or ^d Scroll forward half a window Go down half a window
u or ^u Scroll backward half a window Go up half a window
<space>, f, or ^f Scroll forward one window Go forward a window
b or ^b Scroll backward one window Go back a window

Search for keywords

Wouldn’t it be nice if you could look for certain words or patterns in the document you have it open with less?

Well, guess what - you can!!

Key(s) What it does How to remember
/ + search pattern Search forward through the document N/A
? + search pattern Search backward through the document Pressing <shift> key will reverse the command
n Go to the next line with your pattern Go to next search pattern
N Go to the previous line with your pattern Pressing <shift> key will reverse the command

Show only lines with certain pattern

It is also possible to display only the lines that contain the matching pattern:

Key(s) What it does How to remember
& + search pattern Display only the lines with matching pattern N/A

Jump around the document

Other than searching for patterns in the document, there are few ways you can jump around the document (e.g. to the beginning or to the end of the document).

Key(s) What it does How to remember
g Jump to the beginning of the document N/A
G Jump to the end of the document Pressing <shift> key will reverse the command
'' (two single quotes) Jump to the line you were at N/A (see below explanation of marks)

You can also save (or mark) specific line(s) and jump to that line at will:

Key(s) What it does How to remember
m + <letter> Save the position into <letter> of your choice Mark this position
' + <letter> Jump to the position saved in <letter> N/A

The single quote ' is a special mark, where it will record the position you were at just before you jumped, so by pressing '', it will jump back to where you were just then.

Pass options while in less

If you had forgotten to pass a command line option to less before you started it, it’s not too late for you to invoke it. You can add options to current less instance by typing - first, and then the option you wanted.

For example, if you want to wrap/unwrap long lines, you can press -S in less and press <enter>.

Show help

All of these are available in the help menu of less, which you can access by pressing h

Key(s) What it does How to remember
h Open the help page Help!!