"less" with syntax highlighting?


I have a quick question and would really appreciate any input/suggestions. When I edit multiple source files, I often use one terminal for my “main” editor (which can also show multiple sub-windows), while also opening another terminals for looking at different source codes. For the latter purpose, I often use the “less” command, which is convenient for quickly looking at relevant parts of a given file. However, the “less” command I use does not highlight syntax, so keywords etc are not very visible on the terminals (as compared to the main editor). This is particularly so for the % symbol, which I put special color on the main editor (via customization).

So I am wondering if there are some ways to achieve syntax highlighting for Fortran sources with “less” (or a similar “very-light-weight” view tool)?

Thanks very much in advance!

I have not used it myself, but Pygments appears to do what you ask for, and Fortran is one of the languages for which lexer support is provided.

A search for “less with syntax highlighting” turned up many links, some of which may be worth looking into.

1 Like

I use bat for this purpose. Works great! GitHub - sharkdp/bat: A cat(1) clone with wings.


bat is very good. Just remember to use --style plain if you plan to copy-paste more than single line from it.


“vim -R” opens file in read-only mode and is a great viewing tool. You don’t really need to know, how to use vim to be able to scroll around in a viewed file, but it does help to know how to exit vim :slight_smile: How to Exit Vim – Vim Save and Quit Command Tutorial

1 Like

Thanks very much for various info! I’ve just installed & tried them, and the results are like the following (on my old mac mini, OSX10.11, so the results may vary for newer OS). (And sorry for a very long post… please go to the last part for my summary.)

pygments (https://pygments.org/)

I’ve installed this via brew install pygments then got the command pygmentize. This command accepts a source code and outputs a colored text to standard output:

$ pygmentize test.f90              !! displays a colored code
$ pygmentize -l fortran test.f90   !! specifies "lexar" explicitly

The output itself is very nice, but one problem (for me) is that it behaves like cat, i.e., the program exits when it reaches the end of file. So I tried a pipe like

$ pygmentize test.f90  | less    # or "more"

but this displays the original code + raw ASCII texts for colors, which is not suited for human… Interestingly, piping with cat does not have this problem and show the colored code successfully

$ pygmentize test.f90  | cat

but the command exits at the EOF… (please see below for one possible workaround).

“bat” command

Because bat was already installed via homebrew (maybe auto-installed by other packages?), I’ve tried the following:

$ bat test.f90

This displays the original code with line numbers, but for some reason it does not show any color for my .f90 code… To check, I also tried other languages (like C) and it does show color. So I’m wondering do I possibly need some setting for .f90 files? (I also checked bat --list-languages, but it does not have f90 in the support list.)

vim -R

I’ve just tried this as vim -R test.f90, but again this does not show color for some unknown reason… Indeed, even the usual vim test.f90 does not show color either, so I am afraid that my setting on Vim or other environment stuff may have something wrong :melting_face:

it does help to know how to exit vim :slight_smile:

Thanks for the advice, and do not worry because I learned how to exit Vim before (one of the most famous questions on programming :sweat_smile:)

Even more recently, there seem to be a lot of newer approaches developed for this purpose (including the “the hardware way”).

A search for “less with syntax highlighting” turned up many links, some of which may be worth looking into.

Thanks, I will try this “again”. Indeed, more than 10(?) years ago, I searched the net for this and tried some 3rd-party tool (I forgot the name of it, though). It does show color successfully, but it behaved like cat (i.e., the program exits at EOF)… So I gave up about it at that time. I guess I might find some newer tools that behave more like less.

After playing with the above commands for a while, I happened to try this combination

$ pygmentize test.f90 | bat

which worked exactly as I want!! Even the search of variable/routine name with a slash (/) key also works (like “less”). So I will make a bash command for this for convenient use. Thanks very much!

1 Like

Check which version you had installed. According to their changelog fortran support was added in version 0.15.1. bat/CHANGELOG.md at 520081a92c73652653c4f2b40e9527dd36b234bb · sharkdp/bat · GitHub

1 Like

bat --version on my old mac (OSX10.11) gives 0.6.1, so no wonder why it does not work for f90… Now I tried brew upgrade bat, but the compilation of dependent libraries (here rust) failed. So, I switched to the latest macOS (Monterey), then everything worked fine out of the box (like “less with color”) :grin:
(Indeed, I would like to migrate to Monterey asap, but not finished yet…)

Because bat has a lot of options (like different themes), I will check them for more customization. Thanks!

1 Like