The Perl Toolchain Summit needs more sponsors. If your company depends on Perl, please support this very important event.

NAME

hl - terminal patterns highlighter

SYNOPSIS

hl [global-options] [--] [highlight-options [patterns] ...] [-] [files]

DESCRIPTION

hl reads text from list of files or standard input and prints it on terminal with specified patterns highlighted using ANSI color escape sequences. Patterns are intrinsically Perl-compatible regular expressions.

Global options are processed internally by hl whereas highlight options are passed into Term::Highlight module, therefore they should not mix. The first occurrence of an option which has not been recognized as global is regarded as beginning of highlight options. Highlight options can be explicitly separated from global options with option '--' (must not be confused with option '-' that separates list of files from highlight options).

Global options:

-s <snippet>

loads a snippet with specified name from file ~/.hlrc or ~/.hlrc_<snippet>. The white space between '-s' and the name of the snippet may be omitted. For example -sW loads a snippet with name 'W'. Multiple options -s with different snippet names are allowed.

-g (-grep)

prints only lines that match specified patterns.

-r

greps recursively, implies '-g'. If the file list is empty then grep starts in the current directory.

-a

greps inside hidden files and directories, implies '-r'. Names of hidden files and directories start with a dot symbol.

-gi (-gitignore)

ignores paths not tracked or ignored by git inside git repositories. Effective in recursive grep.

-f (-flist)

builds the file list from the trailing arguments automatically when option '-' is not specified.

-l

prints the list of files where matches were found, implies '-g'.

-c <pre[.post]>

where 'pre' and 'post' are numbers. Prints context lines around matches. If 'post' is not set then it is supposed to be equal to 'pre', implies '-g'.

-n

prints line numbers.

-u (-utf8)

enables matching of Unicode characters from UTF-8 encoded input. For instance matching against '\x{239C}' or 'функци[яи]' will not work without this option.

-b (-bin)

enables processing of binary files (not enabled by default).

-t (-term)

forces using ANSI color escape sequences even when output is not a terminal, this may be useful when output is piped to 'less -r' or alike.

-d (-debug)

turns on debug support (colors are printed out as symbolic sequences).

-h (-help)

prints usage and exits.

Highligh options:

-x[xx][.b]

highlights following patterns with color defined by number x[xx]. x[xx] is color id corresponding to an ANSI color escape sequence number and thus should range within [0..255]. b must be 0 or 1: .0 applies the color id to foreground, .1 - to background. .0 is default choice and may be omitted. If your terminal does not support 256 colors then valid color ids are [0..15]. Note: if your terminal is 256 colors capable then better use [16..255] colors! To see for how many colors your terminal has support use command tput colors.

-i

sets ignorecase search.

-ni

unsets ignorecase search.

-b

sets bold font.

-rfg

resets foreground color to default value.

-rb

resets bold font to normal.

-rbg

resets background color to default value.

-r

resets both background color and bold font.

-ra

resets all settings to default values.

Highlight options apply to following them regexp patterns or to the whole text if trailing highlight options are not followed by patterns.

It is possible to define common highlight options on session level. hl supports environment variable HL_INITSTRING which value will be prepended to any highlight options given in command line.

Highlight options separators:

--

explicitly separates global and highlight options.

-

separates global and highlight options from list of files to process. As soon as hl may read from standard input or use option '-f', using a list of files to process is not obligatory.

ENVIRONMENT VARIABLES

HL_INITSTRING

defines common highlight options which will be prepended to any highlight options given in command line. For example setting HL_INITSTRING='-21 -i' will make hl highlight patterns with color id 21 and ignore cases without explicit definition of highlight options in command line. Note: HL_INITSTRING must not contain global options!

HL_LOCATION

defines highlight options for file names and line numbers when they are printed. For example setting HL_LOCATION='-224 (?<=:)\d+$ -248' will make hl print file names with color id 248 and line numbers with color id 224.

HL_CTXDELIM

defines highlight options for context chunks delimiters (double dashes) when they are printed. For example setting HL_CTXDELIM=-248 will make hl print context chunks delimiters with color id 248.

EXAMPLES

ls | hl -b -46.1 -21 '\bw.*?\b'

reads output of command ls and highlights words that start with w in color id 21 using color id 46 for background and bold font.

FILES

~/.hlrc and ~/.hlrc_<snippet-name>

currently these files may contain only snippets that can be loaded with option '-s'. The format of the snippet line is

snippet name highlight_options

where snippet is a keyword, name is the name of the snippet and highlight_options contains highlight options possibly preceded by the global option '-u'. Here is an example of a snippet to highlight words that start with a capital letter.

snippet W -130 (?:^|[\s])[A-Z]\S+

Lines that do not match the snippet line pattern are ignored. Arguments of highlight_options are naturally split by white spaces. If you want to use white spaces inside patterns then put single quotes around the patterns. Single quote by itself must be escaped by a backslash. Too long lines can be split into multiple lines using backslashes.

Files with names containing specific snippet names are loaded before ~/.hlrc: they are supposed to declare a single snippet (perhaps with a few auxiliary snippets) to help hl start faster.

SEE ALSO

Term::Highlight(3), tput(1)

AUTHOR

Alexey Radkov <alexey.radkov@gmail.com>

COPYRIGHT AND LICENSE

Copyright (C) 2008-2023 by A. Radkov.

This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.