# "less" with syntax highlighting?

**URL:** <https://fortran-lang.discourse.group/t/less-with-syntax-highlighting/3191>\
**Category:** Uncategorized\
**Created:** [April 9, 2022, 3:39am UTC](https://fortran-lang.discourse.group/t/less-with-syntax-highlighting/3191 "2022-04-09T03:39:31Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![septc](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/septc/32/77_2.png) [@septc](https://fortran-lang.discourse.group/u/septc)\
**Post date:** [April 9, 2022, 3:39am UTC](https://fortran-lang.discourse.group/t/less-with-syntax-highlighting/3191/1 "2022-04-09T03:39:31Z")

</div>

Hello,

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!

---

<div class="post-metadata">

**Author:** ![mecej4](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/mecej4/32/855_2.png) [@mecej4](https://fortran-lang.discourse.group/u/mecej4)\
**Post date:** [April 9, 2022, 4:28am UTC](https://fortran-lang.discourse.group/t/less-with-syntax-highlighting/3191/2 "2022-04-09T04:28:07Z")

</div>

I have not used it myself, but [Pygments](https://github.com/pygments/pygments/tree/master/pygments/lexers) 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.

---

<div class="post-metadata">

**Author:** ![plevold](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/plevold/32/754_2.png) [@plevold](https://fortran-lang.discourse.group/u/plevold)\
**Post date:** [April 9, 2022, 5:43am UTC](https://fortran-lang.discourse.group/t/less-with-syntax-highlighting/3191/3 "2022-04-09T05:43:46Z")

</div>

I use `bat` for this purpose. Works great! [GitHub - sharkdp/bat: A cat(1) clone with wings.](https://github.com/sharkdp/bat)

---

<div class="post-metadata">

**Author:** ![msz59](https://avatars.discourse-cdn.com/v4/letter/m/3d9bf3/32.png) [@msz59](https://fortran-lang.discourse.group/u/msz59)\
**Post date:** [April 9, 2022, 10:02am UTC](https://fortran-lang.discourse.group/t/less-with-syntax-highlighting/3191/4 "2022-04-09T10:02:57Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![eugene\_epshteyn](https://avatars.discourse-cdn.com/v4/letter/e/8797f3/32.png) [@eugene\_epshteyn](https://fortran-lang.discourse.group/u/eugene_epshteyn)\
**Post date:** [April 10, 2022, 4:14am UTC](https://fortran-lang.discourse.group/t/less-with-syntax-highlighting/3191/5 "2022-04-10T04:14:45Z")

</div>

“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 🙂 [How to Exit Vim – Vim Save and Quit Command Tutorial](https://www.freecodecamp.org/news/how-to-exit-vim/)

---

<div class="post-metadata">

**Author:** ![septc](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/septc/32/77_2.png) [@septc](https://fortran-lang.discourse.group/u/septc)\
**Post date:** [April 10, 2022, 1:12pm UTC](https://fortran-lang.discourse.group/t/less-with-syntax-highlighting/3191/6 "2022-04-10T13:12:33Z")

</div>

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/](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:

```auto
$ 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

```auto
$ 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

```auto
$ 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:

```auto
$ 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 🫠

> it does help to know how to exit vim 🙂

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

> **[Stack Overflow: Helping One Million Developers Exit Vim - Stack Overflow](https://stackoverflow.blog/2017/05/23/stack-overflow-helping-one-million-developers-exit-vim/)**

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

> **[GitHub - hakluke/how-to-exit-vim: Below are some simple methods for exiting vim.](https://github.com/hakluke/how-to-exit-vim)**
>
> Below are some simple methods for exiting vim. Contribute to hakluke/how-to-exit-vim development by creating an account on GitHub.

> 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

```auto
$ 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!

---

<div class="post-metadata">

**Author:** ![plevold](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/plevold/32/754_2.png) [@plevold](https://fortran-lang.discourse.group/u/plevold)\
**Post date:** [April 10, 2022, 2:18pm UTC](https://fortran-lang.discourse.group/t/less-with-syntax-highlighting/3191/7 "2022-04-10T14:18:42Z")

</div>

> [@septc](#):
>
> This displays the original code with line numbers, but for some reason it does not show any color for my .f90 code…

Check which version you had installed. According to their changelog fortran support was added in version 0.15.1. [https://github.com/sharkdp/bat/blob/520081a92c73652653c4f2b40e9527dd36b234bb/CHANGELOG.md](https://github.com/sharkdp/bat/blob/520081a92c73652653c4f2b40e9527dd36b234bb/CHANGELOG.md)

---

<div class="post-metadata">

**Author:** ![septc](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/septc/32/77_2.png) [@septc](https://fortran-lang.discourse.group/u/septc)\
**Post date:** [April 10, 2022, 4:35pm UTC](https://fortran-lang.discourse.group/t/less-with-syntax-highlighting/3191/8 "2022-04-10T16:35:32Z")

</div>

`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”) 😁  
(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!
