| 1 |
.. -*- mode: rst -*- |
| 2 |
|
| 3 |
.. highlight:: shell |
| 4 |
|
| 5 |
============= |
| 6 |
Terminal Tips |
| 7 |
============= |
| 8 |
|
| 9 |
These are some tips how to get more out of Pygments on the terminal. |
| 10 |
|
| 11 |
|
| 12 |
How do I determine which shell I am using? |
| 13 |
------------------------------------------ |
| 14 |
Run this command:: |
| 15 |
|
| 16 |
$ echo $SHELL |
| 17 |
|
| 18 |
Mine outputs ``/usr/bin/fish``, which is the location of the executable. |
| 19 |
This means my computer is using the fish shell. ``neofetch`` (or a similar |
| 20 |
utility) can also provide that information. |
| 21 |
|
| 22 |
Most shells are POSIX compliant. |
| 23 |
|
| 24 |
|
| 25 |
Aliases (POSIX) |
| 26 |
--------------- |
| 27 |
|
| 28 |
POSIX compliant shells includes ``bash`` and ``zsh``, and most shells. |
| 29 |
|
| 30 |
Aliases allow you to call a function using a different name. In this case it |
| 31 |
calls :program:`pygmentize` by using a different name. The simplest is to make |
| 32 |
an alias called ``cath``:: |
| 33 |
|
| 34 |
$ alias cath="pygmentize" |
| 35 |
|
| 36 |
The only thing is that :program:`pygmentize` does not really work like ``cat``. |
| 37 |
It does allow you to use a command that is similar to one use for cat, |
| 38 |
displaying a file. |
| 39 |
|
| 40 |
Add additional flags can be added as desired. The monokai style works well for |
| 41 |
a dark background, so this is how you would add that option:: |
| 42 |
|
| 43 |
$ alias cath="pygmentize -O style=monokai" |
| 44 |
|
| 45 |
|
| 46 |
Here is a version of ``less`` with syntax highlighting. It is slightly more |
| 47 |
complicated, because it uses the environment variable ``LESSOPEN`` to preprocess |
| 48 |
the input file(s):: |
| 49 |
|
| 50 |
$ alias lessh='LESSOPEN="| pygmentize -O style=monokai %s" less -M -R ' |
| 51 |
|
| 52 |
These alias commands need to be added to your configuration in order to work |
| 53 |
when you open a new terminal (or restart the computer). |
| 54 |
|
| 55 |
Add above commands to your configuration file: |
| 56 |
|
| 57 |
* ``bash`` - edit either ``~/.bash_aliases`` (if it exists) or ``~/.bashrc`` file. |
| 58 |
* ``csh`` (C shell), edit the ``~/.cshrc`` file |
| 59 |
* ``ksh`` (Korn shell), edit the ``~/.kshrc`` file. |
| 60 |
* ``tcsh``, edit the ``~/.tcshrc`` file. |
| 61 |
* ``zsh``, edit the ``~/.zshrc`` file. |
| 62 |
|
| 63 |
In order to apply the changes to the current shell environment systems: run ``source`` on the filename just edited:: |
| 64 |
|
| 65 |
$ source [~/.filenamerc] |
| 66 |
|
| 67 |
|
| 68 |
Aliases (fish shell) |
| 69 |
-------------------- |
| 70 |
The above aliases for POSIX work just fine, but permanently saving an alias in |
| 71 |
fish shell is a little different. Functions are used to make permanent aliases. |
| 72 |
``funced [command]`` is used to create the function, followed by |
| 73 |
``funcsave [command]`` saves the function to the environment. |
| 74 |
|
| 75 |
|
| 76 |
Running the ``funced cath``, brings up the default editor, and add the |
| 77 |
following code in the function:: |
| 78 |
|
| 79 |
function cath |
| 80 |
# 'cath' alias will highlight source code as cat does. |
| 81 |
alias cath="pygmentize" |
| 82 |
end |
| 83 |
|
| 84 |
Here's what the ``cath`` function looks like with an additional argument:: |
| 85 |
|
| 86 |
function cath |
| 87 |
alias cath="pygmentize -O style=monokai" |
| 88 |
end |
| 89 |
|
| 90 |
Note: that just like the POSIX shell you may run ``alias cath="pygmentize -O style=monokai"`` |
| 91 |
on the command line to test out the alias beforehand. |
| 92 |
|
| 93 |
Test the ``cath`` function. To save the function run ``funcsave cath``. |
| 94 |
This saves the function for future sessions. |
| 95 |
|
| 96 |
|
| 97 |
``funced lessh``, which is slightly more |
| 98 |
complicated, because it uses the environment variable ``LESSOPEN`` to preprocess |
| 99 |
the input file(s):: |
| 100 |
|
| 101 |
function lessh |
| 102 |
LESSOPEN="| pygmentize -O style=native %s" less -M -R $argv |
| 103 |
end |
| 104 |
|
| 105 |
Test the ``lessh`` function. To save the function run ``funcsave lessh``. |