Less is more

Searching through a file within the less pager

It is not uncommon when working from the command line to want to view a text file that is too long to fit on the screen. For those instances we can use a pager like less.

$ less some_rather_large_file

Navigating through the file is pretty straight forward. Press the enter or “j” key to scroll the document down one line and press the “k” key to scroll up one line. Press the space bar or “f” to scroll down one “page”, or screen, and press “b” to scroll up. To scroll by half a screen, press “d” (down) and “u” (up). Press “q” to exit less, and press “h” for help.

To search for text within your document press the slash key, “/”, then enter the text you’re looking for. This will search from the current cursor position forward toward the end of the document. Simply type the slash and enter to repeat your search. All instances of the search string that are currently visible on screen will be highlighted and the cursor will move to the next one. To search from the current location backward toward the beginning of the document, type a question mark, “?”, followed by the search text. Enter the question mark alone to repeat the previous search.

By default searches in less are case sensitive. If you want to search for text regardless of case, enter -i. This will switch less into case-insensitive mode until you enter -i again or exit less.

Interestingly, case insensitive searches only work if you enter your search text in all lowercase letters. If even one uppercase letter appears in your search string less will treat it as case sensitive.

Be seeing you.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s