Linux highlight 语法高亮工具

使用 highlight 可以带语法高亮地把源代码转换成 HTML, XHTML, RTF, LaTeX, TeX, BBCode, SVG, XTERM 格式的文件,或者ANSI转义序列,目前支持大约 160 种语言。一个常见的应用场景是把它当做带语法高亮的 cat 命令。

安装

Arch linux:

sudo pacman -S highlight

Ubuntu Or Debain:

sudo apt-get install highlight

RedHat 系(CentOS, RedHat, Fedora):

sudo yum install highlight

MacOS:

brew install highlight

常见用法

先创建一个 bash 脚本作为演示文件 /tmp/test

#!/bin/bash

echo "Hello highlight"

打印文件内容:

highlight /tmp/test

带行号打印:

highlight -n /tmp/test

更多用法使用 man hightlight 查看


主题

highlight 支持多种语法高亮风格的主题,需要的话可以到官网这里下载源码包(tar.bz2) 。解压后可以看到里边有个 themes 文件夹,可以在里边找找有没有自己想要的主题。themes 文件夹下还有个 base16 的文件夹单独放置 base16 配色的主题。

这里我们使用 base16 配色的 gruvbox-dark-soft.theme 文件作为演示。

创建 /tmp/gruvbox-dark-soft.theme 文件,内容如下:

--[[                                      2018/06/17 | Highlight v3.43 | Lua 5.3
================================================================================
Scheme:   Base16 "Gruvbox dark, soft"
Author:   Dawid Kurek (dawikur@gmail.com), morhetz (https://github.com/morhetz/gruvbox)
License:  MIT License
Source:   https://github.com/dawikur/base16-gruvbox-scheme
================================================================================
Converted from YAML by Tristano Ajmone:
    <tajmone@gmail.com>
    https://github.com/tajmone
    https://gitlab.com/tajmone
--------------------------------------------------------------------------------
For more info on Chris Kempson's Base16, see:
    http://chriskempson.com/projects/base16
------------------------------------------------------------------------------]]
Description = "Base16 Gruvbox dark, soft"
Categories = {"dark", "base16", "lsp"}

--------------------------------------------------------------------------------
base00 = "#32302f" --> Default Background
base01 = "#3c3836" --> Lighter Background (Used for status bars)
base02 = "#504945" --> Selection Background
base03 = "#665c54" --> Comments, Invisibles, Line Highlighting
base04 = "#bdae93" --> Dark Foreground (Used for status bars)
base05 = "#d5c4a1" --> Default Foreground, Caret, Delimiters, Operators
base06 = "#ebdbb2" --> Light Foreground (Not often used)
base07 = "#fbf1c7" --> Light Background (Not often used)
base08 = "#fb4934" --> Variables, XML Tags, Markup Link Text, Markup Lists, Diff Deleted
base09 = "#fe8019" --> Integers, Boolean, Constants, XML Attributes, Markup Link Url
base0A = "#fabd2f" --> Classes, Markup Bold, Search Text Background
base0B = "#b8bb26" --> Strings, Inherited Class, Markup Code, Diff Inserted
base0C = "#8ec07c" --> Support, Regular Expressions, Escape Characters, Markup Quotes
base0D = "#83a598" --> Functions, Methods, Attribute IDs, Headings
base0E = "#d3869b" --> Keywords, Storage, Selector, Markup Italic, Diff Changed
base0F = "#d65d0e" --> Deprecated, Opening/Closing Embedded Language Tags, e.g. <?php ?>
--------------------------------------------------------------------------------
Canvas         = { Colour = base00, Italic = false, Bold = false }

Default        = { Colour = base05, Italic = false, Bold = false }

Number         = { Colour = base09, Italic = false, Bold = false }

Operator       = { Colour = base05, Italic = false, Bold = false }

String         = { Colour = base0B, Italic = false, Bold = false }

Escape         = { Colour = base0C, Italic = false, Bold = false }
StringPreProc  = Escape

Interpolation  = { Colour = base09, Italic = false, Bold = false }

LineComment    = { Colour = base03, Italic = true,  Bold = false } --> Italic
BlockComment   = LineComment

LineNum        = { Colour = base04, Italic = false, Bold = false }

PreProcessor   = { Colour = base0F, Italic = false, Bold = false }

Keywords = {
  { Colour = base0E, Italic = false, Bold = true  }, --> kwd 1
  { Colour = base0D, Italic = false, Bold = true  }, --> kwd 2
  { Colour = base08, Italic = false, Bold = true  }, --> kwd 3
  { Colour = base0A, Italic = false, Bold = true  }, --> kwd 4
  { Colour = base0F, Italic = false, Bold = true  }, --> kwd 5
  { Colour = base09, Italic = false, Bold = true  }, --> kwd 6
}

-- new LSP based elements:

SemanticTokenTypes = {
  { Type = 'type', Style = Keywords[2] },
  { Type = 'class', Style =  { Colour = base0A } },
  { Type = 'struct', Style = { Colour = base0A } },
  { Type = 'interface', Style = { Colour = base06 } },
  { Type = 'parameter', Style = Keywords[6] },
  { Type = 'variable', Style = Keywords[5] },
  { Type = 'enumMember', Style = Keywords[5] },
  { Type = 'function', Style = Keywords[4] },
  { Type = 'method', Style = Keywords[4] },
  { Type = 'keyword', Style =  Keywords[1]},
  { Type = 'number', Style = Number },
  { Type = 'regexp', Style = String },
  { Type = 'operator', Style = Operator },
}

--[[----------------------------------------------------------------------------
The MIT License

Copyright (c) 2018 Tristano Ajmone
Copyright (c) Dawid Kurek (dawikur@gmail.com), 
morhetz (https://github.com/morhetz/gruvbox)

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
------------------------------------------------------------------------------]]

使用主题:

highlight --config-file=/tmp/gruvbox-dark-soft.theme /tmp/test

注意这里的主题文件要使用绝对路径,像是~/这样的相对路径是不行的。


设置别名

可以设置别名 hl~/.zshrc 或者 ~/.bashrc 中,方便使用,如

# ~/.zshrc

# 注意替换主题文件位置
alias hl="highlight --config-file=/home/ian/.themes/highlight/gruvbox-dark-soft.theme"

后面就可以直接这样子使用了:

hl /tmp/test
点赞

发表评论

您的电子邮箱地址不会被公开。 必填项已用*标注