为neovim优化语法高亮

为neovim优化语法高亮

neovim和vim在我用起来都有一个问题:代码高亮很烂

于是我找到了一个插件:nvim-treesitter(后面发现semantic highlight也挺不错的),优化我的neovim的代码高亮:

优化前

image-20230116150238030

优化后

image-20230116150048234

如何安装

我目前没有使用任何插件管理工具,直接用的git submodule

nvim-treesitter

由于这个插件最先版要求是neovim的0.8以上版本,而我只有0.6,所以装好之后需要切换到0.5-compat分支,这个分支的代码对neovim版本要求低。

安装对语言的支持

只安装插件是不够的,需要安装对各种语言的支持,可以通过命令

TSInstall + 语言 的命令来安装对不同语言的支持

也可以通过命令TSInstallInfo 命令来查看支持哪些语言,现在已经安装了哪些语言。

image-20230116150804791

开启语法高亮功能

nvim-treesitter支持语法高亮,代码格式化,代码折叠等功能,这里只演示开启语法高亮

只需要在init.vim 中加入一段代码

lua <<EOF
require'nvim-treesitter.configs'.setup {
  highlight = {
    enable = true,
    custom_captures = {
      -- Highlight the @foo.bar capture group with the "Identifier" highlight group.
      ["foo.bar"] = "Identifier",
    },
    -- Setting this to true will run `:h syntax` and tree-sitter at the same time.
    -- Set this to `true` if you depend on 'syntax' being enabled (like for indentation).
    -- Using this option may slow down your editor, and you may see some duplicate highlights.
    -- Instead of true it can also be a list of languages
    additional_vim_regex_highlighting = false,
  },
}
EOF

就可以开启语法高亮。

posted @ 2023-01-16 15:57  博客是个啥?  阅读(1398)  评论(0编辑  收藏  举报