less Command Cheat Sheet: Quick Reference Guide

Written by: Linuxopsys   |   Last updated: July 13, 2023

If you want to view large files in Linux without the need for opening a full-fledged text editor, you have several commands to do that. The less command is one of the commands.

This guide provides a comprehensive cheat sheet for the less command - helps you navigate files in both forward and backward directions using simple keystrokes.

Basic command

The less command syntax is very straightforward it takes optional options that modify its behavior and a file name to be examined. Look at this example which opens a file named logs.txt with the less command:

less /var/logs/logs.txt

To quit less you just have to press the ‘q’ key or ZZ on your keyboard

Navigation Keys

With your text file opened, less provides you with different keys to navigate back and forth through the text files. The navigations can be categorized into different categories for example screen navigation, line navigation, scrolling, marking, and so on. Here are the navigation commands in less.

Screen navigation

  • Press the CTRL+F or space key on your keyboard to navigate forward by a full screen.
  • Press the CTRL+B or b key to navigate backward by a full screen.
  • Press the CTRL+D or d keys to move forward half screen.
  • And finally, you can press the CTRL+U or u key to move backward half screen.

Line navigation

The less command has only two commands to navigate between lines.

  • j key - for moving down by a line
  • k key - for moving up by a line.

Like in the vim editor, these keys can be preceded by N where N is any integer that instructs less how many N lines to move either forward or backward. For example, if you want to move 10 lines down you would use the command 10j.

Searching

To start searching forward for a pattern press the forward slash (/) followed by the term you want to search. To search for the word ‘Linux’, for example, you would type:

/Linux

To search backward for a pattern press a question mark (?) followed by the term you want to search. Here is an example of backward searching the term Linux in our file:

?Linux

By default, less searching is case-sensitive. To enable case-insensitive when searching run the less command with the -i option:

less -i filename

To navigate between matched search occurrences press:

  • n key to move to the next occurrence ( this also moves backward for the next occurrence when doing a backward search)
  • N key to move to the previous occurrence in a backward direction (this moves forward for the next occurrence when doing a backward search}

Marking

The less command allows you to mark specific positions in a file by pressing the m key followed by any lowercase letter. For example, to mark the current line with the label a, you would press ‘ma’. To jump to the mark press the ‘ (apostrophe) followed by the label of the position. For example, to jump to a position marked with the label you press ‘a.

Other navigations

  • Up arrow key -similar to the k key this will move up by one line.
  • Down arrow key - similar to the j k, this will move down by one line.
  • Right arrow key - this will scroll the page horizontally to the right. This works if you execute the less command with the -S option which chops long lines instead of wrapping them
  • Left arrow key - this will scroll the page horizontally to the left. Also works if you execute the less command with the -S option.
  • G -  Pressing G allows you to go to the end of the file.
  • g -  The g key allows you to get back to the beginning of the file.

Other commands

  • F: Similar to the ‘tail -f’ command, this command allows less to follow the end of the file as more lines are added. This is handy when you want to monitor the contents of a file in real-time, for example, log files that constantly get updated with new logs.
  • h: This command is very helpful if you want to quickly need reference or assistance with the less command. Pressing h will bring up a help menu showing available options and commands.
  • v: This command opens the current file in the default editor. When you want to perform edits or modifications to a file, this command comes in handy.
  • = or Ctrl+G: Press = or Ctrl+G if you want to obtain information about the current file, such as its name and the line number you are currently on.

Download less command cheat sheet - PDF

SHARE

Comments

Please add comments below to provide the author your ideas, appreciation and feedback.

Leave a Reply

Leave a Comment