Skip to content

Instantly share code, notes, and snippets.

@BretFisher
Last active February 6, 2024 14:41
Show Gist options
  • Save BretFisher/6f688dde0122399efdca5a9d26100437 to your computer and use it in GitHub Desktop.
Save BretFisher/6f688dde0122399efdca5a9d26100437 to your computer and use it in GitHub Desktop.
On macOS: Install pygmentize and alias pcat for shell code syntax highlighting
# first install pygmentize to the mac OS X or macOS system with the built-in python
sudo easy_install Pygments
# then add alias to your ~/.bash_profile or ~/.bashrc or ~/.zshrc etc.
alias pcat='pygmentize -f terminal256 -O style=native -g'
@BretFisher
Copy link
Author

Note that there's a lot you could argue about the use of sudo easy_install above. One might say you should use pip over easy_install (which isn't on macOS by default) or use brew install python to get the latest python and then stop using sudo to install python packages. All good things, but beyond the scope of what I'm trying to solve here.

Suffice to say, if you have your own way of using python, use that to install Pygments :)

Copy link

ghost commented Oct 26, 2017

Thanks, your pcat command is great stuff.

But I should show people how to install Pygments with Python3 (it's faster than Python2, which means faster bootup and faster colorizing, in other words it takes less time to run the pygmentize command).

First, install python2 and python3 properly via Homebrew as follows:

brew install python3
brew install python
brew unlink python && brew link --overwrite python # make sure python links to python2

pip2 install --upgrade pip setuptools # upgrade to latest pip2
pip3 install --upgrade pip setuptools # upgrade to latest pip3

# never use the generic “python” and “pip”.
# always use “python[2/3]” and “pip[2/3]”.

Then, to install Pygments, I just ran:

pip3 install pygments

# to upgrade later:
pip3 install pygments --upgrade

Now the pygmentize command is available. Voila. :-)

Thanks again for your great pygmentize options alias! 👍 It got me started with pygmentize!

Also a note for others, regarding this command:

alias pcat='pygmentize -f terminal256 -O style=native -g'

pygmentize -L styles lists all styles, if you don't like the style=native theme used above. Personally, I use:

alias pcat='pygmentize -f terminal256 -O style=monokai -g'

As for the command itself, it's correctly setup (I checked via pygmentize -h). There's really nothing else that needs configuring. It's saying: "Output to 256-color terminal, use the Native color-style/theme, and Guess what format the source code is based on file extension."


Regarding the styles, most of them are not good for terminal (due to things like black text on a black background). But here's a list of the best ones for terminal (and the absolute best ones are monokai and native):

Styles:
~~~~~~~
* friendly:
    A modern style based on the VIM pyte theme.
* autumn:
    A colorful style, inspired by the terminal highlighting style.
* manni:
    A colorful style, inspired by the terminal highlighting style.
* monokai:
    This style mimics the Monokai color scheme.
* pastie:
    Style similar to the pastie default style.
* native:
    Pygments version of the "native" vim theme.
* rrt:
    Minimalistic "rrt" theme, based on Zap and Emacs defaults.
* rainbow_dash:
    A bright and colorful syntax highlighting theme.

Copy link

ghost commented Oct 26, 2017

As for why I changed the style choice from native to monokai? Well... here's why. Monokai extends the syntactic highlighting to make things even clearer than the native theme.

native:

native

native2

monokai:

monokai

monokai2

@varun-c-amagi
Copy link

I randomly came across this gist. But man, this is just fantastic!!!!!!!!!
Thanks a lot for the idea and alias.

@DhavalDalal
Copy link

Thanks indeed...this is great!!

@ianizaguirre
Copy link

This is awesome! Thank you

@binakot
Copy link

binakot commented Sep 1, 2018

Found this one while learning your course about Docker!
Thanks for the course and this gist too!

@thiagogenez
Copy link

Found this one while learning your course about Docker too! -- Thank you, much appreciated!

@elchyan-picsart
Copy link

Yeah me too Docker course :))

@LordAmit
Copy link

Was looking for something like this. Thanks!

@mcouthon
Copy link

Delicious.

@HamidOsouli-zz
Copy link

Thanks, It's great :)

@BretFisher
Copy link
Author

I've stopped using pcat years ago, and now use bat: https://github.com/sharkdp/bat

@touseefmurtaza93
Copy link

I was about to install the Pygments but now going to give a shot to bat

@CuongNgMan
Copy link

I've stopped using pcat years ago, and now use bat: https://github.com/sharkdp/bat

Greate advice. Thanks.

@slevinas
Copy link

thanks a lot!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment