Using less to view text files at the Linux command line

The less utility makes quick work of viewing text files in the terminal.
315 readers like this.
open source button on keyboard

Opensource.com

If there's one thing you're sure to find on a Linux system, it's text files. A lot of them. Readme files, configuration files, documents, and more.

Most of the time, you probably open text files using a text editor. But there is a faster and, I think, better way of reading text files. That's using a utility called less. Standard kit with all Linux distributions (at least the ones I've used), less is a command-line textfile viewer with some useful features.

Don't let the fact that it's a command-line tool scare you. less is very easy to use and has a very shallow learning curve.

Let's take a look at some of the things that you can do with less.

Getting started

Crack open a terminal window and navigate to a directory containing one or more text files that you want to view. Then run the command less filename, where filename is the name of the file you want to view.

Less getting started

opensource.com

The file takes over your terminal window, and you'll notice a colon (:) at the bottom of the window. The colon is where you can type any of the internal commands you use with less. More on these in a moment.

Moving around

Chances are that the text file you're perusing is more than a couple of lines long; it's probably a page or more. With less, you can move forward in the file in a few ways:

  • Move down a page by pressing the spacebar or the PgDn key
  • Move down one line at a time by pressing the Down arrow key

less also allows you to move backward in a file. To do that, press the PgUp key (to move up a page at a time) or the Up arrow key (to move up one line at a time).

Finding text

If you have a large text file or are trying to find a specific piece of text, you can do that easily in less. To find a word or phrase, press / on your keyboard and type what you want to find.

Less finding text

opensource.com

Note that the search function in less is case-sensitive. Typing "the silence" isn't the same as typing "The Silence."

less also highlights the words or phrases you search for. That's a nice touch that makes it easier for you to scan the text.

Less highlights found text

opensource.com

You can press n on your keyboard to find the next instance of the word or phrase. Press p on your keyboard to find the previous instance.

Getting out of there

Once you get to the end of a text file and you're done viewing it, how do you exit less? That's easy. Just press q on your keyboard. (You can also press q at any time to leave the program.)

As I mentioned at the beginning of this post, less is easy to use. Once you use it, you'll wonder how you ever did without it.

That idiot Scott Nesbitt ...
I'm a long-time user of free/open source software, and write various things for both fun and profit. I don't take myself all that seriously and I do all of my own stunts.

9 Comments

Hello!
I like it!
You can also go to a concrete line with:
g+num
where num it's the line to want to go.

've phun!

Great article, Scott! My favorite hidden gem is pressing "v" to edit the file in vi.

Also important Less commands to know are:
G = Last line in file
g = First line in file

Does anyone know if it's possible to get syntax highlighting with color like you get in Vim?

Out of the box, less doesn't support syntax highlighting. You can try this. I haven't given it a look, but it might work ...

In reply to by JJ

Thanks Scott. It set me off of to research this more thoroughly and I found this simple elegant solution. You don't have to install anything extra as it is already by default on most machines. As `vim` is installed by default on most machines, it includes a macro to run `vim` like `less`
Some of the options are to create an alias:
`alias vless='vim -u /usr/share/vim/vim74/macros/less.vim'`

or create a symbolic link:
`ln -s /usr/share/vim/vim74/macros/less.sh ~/bin/vless`

Then you just run `vless myfile.py`

In reply to by ScottNesbitt

Where has this been all my life? (Thanks, Scott!)

I've read the slogan before:

"less is MORE than more;
"more is LESS than less".

But have you ever tried out MOST? Yes, there is another paging tool called most, and it has a few intriguing options too. Debian (and Debian-based distributions) offer most through the standard Debian repositories.

Great article!

A small correction: 'p' is short for 'percent': Pressing just 'p' will jump to the start of the file, and pressing a number and then 'p' will jump that percent into the file - e.g. pressing '30p' will jump to the line that is about 30% into the file.

Jumping to the Previous match for a search is done with 'N'.

I'm not sure why anyone would ever invoke less directly. As a system pager, it's ok but when view is available, why not use it instead? You get the read-only safety of less but with all the features of vim. Edits are just a bang away.

Creative Commons LicenseThis work is licensed under a Creative Commons Attribution-Share Alike 4.0 International License.