Terminal Tips

These are some tips how to get more out of Pygments on the terminal.

How do I determine which shell I am using?

Run this command:

$echo$SHELL

Mine outputs/usr/bin/fish, which is the location of the executable.This means my computer is using the fish shell.neofetch (or a similarutility) can also provide that information.

Most shells are POSIX compliant.

Aliases (POSIX)

POSIX compliant shells includesbash andzsh, and most shells.

Aliases allow you to call a function using a different name. In this case itcallspygmentize by using a different name. The simplest is to makean alias calledcath:

$aliascath="pygmentize"

The only thing is thatpygmentize does not really work likecat.It does allow you to use a command that is similar to one use for cat,displaying a file.

Add additional flags can be added as desired. The monokai style works well fora dark background, so this is how you would add that option:

$aliascath="pygmentize -O style=monokai"

Here is a version ofless with syntax highlighting. It is slightly morecomplicated, because it uses the environment variableLESSOPEN to preprocessthe input file(s):

$aliaslessh='LESSOPEN="| pygmentize -O style=monokai %s" less -M -R '

These alias commands need to be added to your configuration in order to workwhen you open a new terminal (or restart the computer).

Add above commands to your configuration file:

  • bash - edit either~/.bash_aliases (if it exists) or~/.bashrc file.

  • csh (C shell), edit the~/.cshrc file

  • ksh (Korn shell), edit the~/.kshrc file.

  • tcsh, edit the~/.tcshrc file.

  • zsh, edit the~/.zshrc file.

In order to apply the changes to the current shell environment systems: runsource on the filename just edited:

$source[~/.filenamerc]

Aliases (fish shell)

The above aliases for POSIX work just fine, but permanently saving an alias infish shell is a little different. Functions are used to make permanent aliases.funced[command] is used to create the function, followed byfuncsave[command] saves the function to the environment.

Running thefuncedcath, brings up the default editor, and add thefollowing code in the function:

functioncath# 'cath' alias will highlight source code as cat does.aliascath="pygmentize"end

Here’s what thecath function looks like with an additional argument:

functioncathaliascath="pygmentize -O style=monokai"end

Note: that just like the POSIX shell you may runaliascath="pygmentize-Ostyle=monokai"on the command line to test out the alias beforehand.

Test thecath function. To save the function runfuncsavecath.This saves the function for future sessions.

funcedlessh, which is slightly morecomplicated, because it uses the environment variableLESSOPEN to preprocessthe input file(s):

functionlesshLESSOPEN="| pygmentize -O style=native %s"less-M-R$argvend

Test thelessh function. To save the function runfuncsavelessh.