• Version 2.1.11
  • Published
  • 42.7 kB
  • 6 dependencies
  • ISC license


npm i cli-highlight
yarn add cli-highlight
pnpm add cli-highlight


Syntax highlighting in your terminal




const DEFAULT_THEME: Theme;
  • The default theme. It is possible to override just individual keys.


function fromJson

fromJson: (json: JsonTheme) => Theme;
  • Converts a [[JsonTheme]] with string values to a [[Theme]] with formatter functions. Used by [[parse]].

function highlight

highlight: (code: string, options?: HighlightOptions) => string;
  • Apply syntax highlighting to code with ASCII color codes. The language is automatically detected if not set.

    import {highlight} from 'cli-highlight';
    import * as fs from 'fs';
    fs.readFile('package.json', 'utf8', (err: any, json: string) => {

    Parameter code

    The code to highlight

    Parameter options

    Optional options

function listLanguages

listLanguages: () => string[];
  • Returns all supported languages

function parse

parse: (json: string) => Theme;
  • Parses a JSON string into a [[Theme]] with formatter functions.

    import * as fs from 'fs';
    import {parse, highlight} from 'cli-highlight';
    fs.readFile('mytheme.json', 'utf8', (err: any, json: string) => {
    if (err) throw err;
    const code = highlight('SELECT * FROM table', {theme: parse(json)});

function plain

plain: (codePart: string) => string;
  • Identity function for tokens that should not be styled (returns the input string as-is). See [[Theme]] for an example.

function stringify

stringify: (theme: Theme) => string;
  • Stringifies a [[Theme]] with formatter functions to a JSON string.

    import chalk = require('chalk');
    import {stringify} from 'cli-highlight';
    import * as fs from 'fs';
    const myTheme: Theme = {
    number: plain
    const json = stringify(myTheme);
    fs.writeFile('mytheme.json', json, (err: any) => {
    if (err) throw err;
    console.log('Theme saved');

function supportsLanguage

supportsLanguage: (name: string) => boolean;
  • Returns true if the language is supported

    Parameter name

    A language name, alias or file extension

function toJson

toJson: (theme: Theme) => JsonTheme;
  • Converts a [[Theme]] with formatter functions to a [[JsonTheme]] with string values. Used by [[stringify]].


interface HighlightOptions

interface HighlightOptions {}
  • Options passed to [[highlight]]

property ignoreIllegals

ignoreIllegals?: boolean;
  • When present and evaluates to a true value, forces highlighting to finish even in case of detecting illegal syntax for the language instead of throwing an exception.

property language

language?: string;
  • Can be a name, file extension, alias etc. If omitted, tries to auto-detect language.

property languageSubset

languageSubset?: string[];
  • Optional array of language names and aliases restricting detection to only those languages.

property theme

theme?: Theme;
  • Supply a custom theme where you override language tokens with own formatter functions. Every token that is not overriden falls back to the [[DEFAULT_THEME]]

interface JsonTheme

interface JsonTheme extends Tokens<Style | Style[]> {}
  • The schema of a JSON file defining a custom scheme. The key is a language token, while the value is a [chalk]( style.


    "keyword": ["red", "bold"],
    "addition": "green",
    "deletion": ["red", "strikethrough"],
    "number": "plain"

interface Theme

interface Theme extends Tokens<(codePart: string) => string> {}
  • Passed to [[highlight]] as the theme option. A theme is a map of language tokens to a function that takes in string value of the token and returns a new string with colorization applied (typically a [chalk]( style), but you can also provide your own formatting functions.


    import {Theme, plain} from 'cli-highlight';
    import chalk = require('chalk');
    const myTheme: Theme = {
    number: plain

property default

default?: (codePart: string) => string;
  • things not matched by any token

interface Tokens

interface Tokens<T> {}
  • A generic interface that holds all available language tokens.

property 'builtin-name'

'builtin-name'?: T;
  • s-expression name from the language standard library

property 'meta-keyword'

'meta-keyword'?: T;
  • keyword or built-in within meta construct

property 'meta-string'

'meta-string'?: T;
  • string within meta construct

property 'selector-attr'

'selector-attr'?: T;
  • [attr] selector in CSS

property 'selector-class'

'selector-class'?: T;
  • .class selector in CSS

property 'selector-id'

'selector-id'?: T;
  • #id selector in CSS

property 'selector-pseudo'

'selector-pseudo'?: T;
  • :pseudo selector in CSS

property 'selector-tag'

'selector-tag'?: T;
  • tag selector in CSS

property 'template-tag'

'template-tag'?: T;
  • tag of a template language

property 'template-variable'

'template-variable'?: T;
  • variable in a template language

property addition

addition?: T;
  • added or changed line in a diff

property attr

attr?: T;
  • name of an attribute with no language defined semantics (keys in JSON, setting names in .ini), also sub-attribute within another highlighted object, like XML tag

property attribute

attribute?: T;
  • name of an attribute followed by a structured value part, like CSS properties

property built_in

built_in?: T;
  • built-in or library object (constant, class, function)

property bullet

bullet?: T;
  • list item bullet in text markup

property class

class?: T;
  • class or class-level declaration (interfaces, traits, modules, etc)

property code

code?: T;
  • code block in text markup

property comment

comment?: T;
  • comment

property deletion

deletion?: T;
  • deleted line in a diff

property doctag

doctag?: T;
  • documentation markup within comments

property emphasis

emphasis?: T;
  • emphasis in text markup

property formula

formula?: T;
  • mathematical formula in text markup

property function

function?: T;
  • function or method declaration

property keyword

keyword?: T;
  • keyword in a regular Algol-style language

link?: T;
  • hyperlink in text markup

property literal

literal?: T;
  • special identifier for a built-in value ("true", "false", "null")

property meta

meta?: T;
  • flags, modifiers, annotations, processing instructions, preprocessor directive, etc

property name

name?: T;
  • name of an XML tag, the first word in an s-expression

property number

number?: T;
  • number, including units and modifiers, if any.

property params

params?: T;
  • block of function arguments (parameters) at the place of declaration

property quote

quote?: T;
  • quotation in text markup

property regexp

regexp?: T;
  • literal regular expression

property section

section?: T;
  • heading of a section in a config file, heading in text markup

property string

string?: T;
  • literal string, character

property strong

strong?: T;
  • strong emphasis in text markup

property subst

subst?: T;
  • parsed section inside a literal string

property symbol

symbol?: T;
  • symbolic constant, interned string, goto label

property tag

tag?: T;
  • XML/HTML tag

property title

title?: T;
  • name of a class or a function at the place of declaration

property type

type?: T;
  • user-defined type in a language with first-class syntactically significant types, like Haskell

property variable

variable?: T;
  • variable in a config or a template file, environment var expansion in a script

Type Aliases

type Style

type Style =
| 'reset'
| 'bold'
| 'dim'
| 'italic'
| 'underline'
| 'inverse'
| 'hidden'
| 'strikethrough'
| 'black'
| 'red'
| 'green'
| 'yellow'
| 'blue'
| 'magenta'
| 'cyan'
| 'white'
| 'gray'
| 'bgBlack'
| 'bgRed'
| 'bgGreen'
| 'bgYellow'
| 'bgBlue'
| 'bgMagenta'
| 'bgCyan'
| 'plain';
  • Possible styles that can be used on a token in a JSON theme. See the [chalk]( module for more information. plain means no styling.

Package Files (2)

Dependencies (6)

Dev Dependencies (19)

Peer Dependencies (0)

No peer dependencies.


To add a badge like this badgeto your package's README, use the codes available below.

You may also use to create a custom badge linking to

  • Markdown
  • HTML
    <a href=""><img src="" alt=""></a>