Skip to content

Terminal lessh command #2131

Closed
Closed

Description

@micahcochran

I am using pygments to do source highlighting on the command line. I created an alias for lessh, so less that does source code highlighting. Sure, I could use a text editor like nano or vim, but I don't want there to be a chance that I'll edit the source file.

Bash Shell

I use a dark terminal environment, which works for "native" style. Choose whichever style you prefer.

This uses the less environment variable LESSOPEN to pre-process the input file(s):

alias lessh='LESSOPEN="| pygmentize -f 256 -O style=native %s" less -M -R '

Default style version:

alias lessh='LESSOPEN="| pygmentize %s" less -M -R '

You could set the LESSOPEN variable globally and use less, but this will pipe every single file through pygmentize. That will make less run slower because it will always run files through pygmentize.

Fish Shell

The fish shell equivalents is to create functions, funced lessh is used to create the function. Test it out. Then, save it to the fish shell with the funcsave lessh.

function lessh
       LESSOPEN="| pygmentize -f 256 -O style=native %s" less -M -R $argv
end

Note: I also made cath, which is basically alias cath="pygmentize". Which doesn't really work like cat, so it does not concatenate the files and highlight. It just outputs a file to the screen highlighted. I should choose a different alias for this.

I thought someone else might be able to benefit from this. Could this be added to the documentation as a way to use pygments? (GNU Source-highlight documented a something similar, which I was inspired by.)

Activity

  1. amitkummer commented on May 8, 2022

    @amitkummer
    Contributor

    I like the idea, and can confirm the lessh alias also works on ZSH, as I have added it to my personal setup :).

    I can create a PR, just would like to have approval from one of the maintainers first, if adding this to the documentation sounds like a good idea.

  2. jeanas commented on Aug 18, 2022

    @jeanas
    Contributor

    I can create a PR, just would like to have approval from one of the maintainers first, if adding this to the documentation sounds like a good idea.

    SGTM.

  3. added a commit that references this issue on May 7, 2023
  4. micahcochran commented on May 7, 2023

    @micahcochran
    ContributorAuthor

    Closing. Fixed by merging PR #2425.

  5. added this to the 2.16.0 milestone on Aug 5, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions