Introduction
In the day-to-day of a system manager or developer, you often need to review the content of text files that can be very extensive: logs, settings, source code or command outputs. Open these files with a traditional editor like vim or nano can be uncomfortable when you just want to read information without changing it. Here comes the less command, a Linux command line tool specifically designed for interactive and paged text display. Unlike more, less allows moving both forward and back, looking for patterns and marking positions, all without loading the entire file into memory. In this article we will explore its most useful features and how to make the most of your daily workflow.
What's less?
Less belongs to the GNU utility set and is included by default in almost all Linux distributions. His name comes from the phrase «less is more», referring to that it offers more functionalities than the classic command more, but keeping a resource consumption low. When less is invoked followed by a file name, the program loads the file into an internal buffer and displays a text screen (or window) according to the size of your terminal. From there you can move using the keyboard, do searches, jump to specific lines and even open several files simultaneously. The key to its efficiency is that it only keeps in memory the visible portion and some adjacent lines, which allows to work with files of several gigabytes without notice significant delays.
Basic use
To start using less, just type less name _ of _ file in the terminal. If the file does not exist, less will display an error message and return to the prompt. Once inside the interface, you will see the content of the file starting with the first line. In the lower left corner there is an indicator showing the percentage of the file you have seen and the current line number. You can get out of less at any time by pressing the q key, which returns to the command line without saving changes. If you want to see several files at once, simply pass them as arguments: less file 1.txt file 2.txt, and less will allow you to change between them with commands: n (next) and: p (previous).
Basic navigation
Within less, the motion keys are intuitive for those who have used other visors. The space bar moves a full screen down, while the b key back a full screen up. To move line by line, use the arrows up and down or the keys k and j, respectively, imitating the behavior of vi. If you want to go to the start of the file, click g; to go to the end, use G (capital). You can also jump to a specific line number by writing the number followed by g (e.g. 45g leads to line 45). These options make large file scanning fast and accurate, without counting lines manually.
Search within less
One of the most powerful features of less is the ability to search for text patterns while you see the file. To start a search forward, write / follow the expression you want to find and press Enter. Less will highlight all the coincidences and move the cursor to the first appearance after the current position. To look back, use? instead of /. Once you have a highlighted match, you can move on to the next one with n and back to the previous one with N. If the expression contains special spaces or characters, it is recommended to lock it between simple quotes or use the corresponding escape. In addition, less allows the search to express basic regular expressions if the -R option is activated, which further expands its utility to analyze log or code.
Marks and jumps
Less also allows to mark positions inside the file to return to them quickly. With the m key followed by any letter (e.g. ma) you establish a brand called a in the current line. You can then return to that brand by typing'a (simple quotation followed by the letter). This mechanism is useful when you need to compare distant sections of the same document or return to a point of interest after a search. Another way to jump is to use the commands: e to open a new file without coming out of less, and 😡 to get out and save a status file (less common). These characteristics turn less into a semi-interactive reading environment that adapts to more complex workflows.
Personalization and options
The less behavior can be adjusted by environment variables or command line flags. Some of the most used are -N, which shows the line numbers to the left of each screen; -S, which cuts the long lines instead of wrapping them, useful to see code or logs with very large lines; -i, which makes the searches insensitive to capital and lower case by default; and -X, which leaves the content of the screen in the terminal when it comes out, preventing it from being cleaned. You can combine these options in the invocation, for example: less -NSi archivo.log. In addition, the LESS environment variable allows to set default values; for example, export LESS = '-NSi' will cause all less invocation to inherit those options without having to specify them at a time.
Conclusion and alternatives
In short, less is an essential tool for any Linux user who needs to read large files quickly and safely. Its combination of bidirectional navigation, powerful search, brands and low memory consumption makes it more than more and many editors when the goal is only inspection. While there are alternatives such as most, pg or even graphic visors such as gedit or VS Code, none offers the same lightness and availability in environments without graphic environment. Domain less will allow you to work more efficiently in the terminal, saving time and reducing friction by using logs, configurations or any other type of text file. We invite you to practice the commands described and to adapt the options to your specific needs to become a more agile and productive user.


