Viewing Colored Source Code with Pygments
Explore how to use the Pygments Python library to view source code with syntax highlighting in the command-line interface. Learn to install Pygments, apply different color styles, create aliases to simplify commands, and pipe colored output into navigation tools like less for improved code readability and management.
We'll cover the following...
Setting up Pygments
Commands like cat and less are great for viewing the contents of a file, but when we’re looking at code, syntax coloring makes things easier to see.
The Pygments library for Python can scan code and produce colored output. When we install it globally, we get a pygmentize command to print a file to our screen with color.
Let’s install Pygments using the pip3 command:
pip3 install Pygments
Once installed, we use it to read a file. Let’s create the following Ruby file named person.rb in our current directory:
class Person
attr_accessor :first_name, :last_name
def initialize(first_name, last_name)
@first_name = first_name
@last_name = last_name
end
def full_name
"#{self.first_name} #{self.last_name}"
end
end
...