如何在Neovim中使用vim.bo.syntax和StrikeoutMatch

huangapple go评论66阅读模式
英文:

How to use vim.bo.syntax with StrikeoutMatch in Neovim

问题

将以下内容从代码中翻译成中文:

  1. "txt strikeout ~~ (background black) ~~" 翻译为: "txt 删除线 (背景黑色)"
  2. "au BufRead,BufNewFile .txt syntax match StrikeoutMatch /~~.~~/" 翻译为: "在 BufRead、BufNewFile 事件中匹配所有 .txt 文件的语法规则 StrikeoutMatch,规则为 /~~.~~/"
  3. "hi def StrikeoutColor ctermbg=016 ctermfg=black" 翻译为: "定义 StrikeoutColor,背景颜色为 016,前景颜色为黑色"
  4. "hi link StrikeoutMatch StrikeoutColor" 翻译为: "将 StrikeoutMatch 链接到 StrikeoutColor"

请注意,上述翻译只包括代码部分,不包括问题中的其他内容。

英文:

How to convert this:

" txt strikeout ~~ (background black) ~~
au BufRead,BufNewFile *.txt   syntax match StrikeoutMatch /\~\~.*\~\~/
hi def  StrikeoutColor   ctermbg=016 ctermfg=black
hi link StrikeoutMatch StrikeoutColor

To neovim lua using probably vim.api.nvim_create_autocmd, I would like to prevent using:

vim.cmd([[
  augroup StrikeoutMatch
    autocmd!
    autocmd BufRead,BufNewFile *.txt syntax match StrikeoutMatch /\~\~.*\~\~/
  augroup END
]])

答案1

得分: 1

请参考Neovim API中的"Autocmd Functions"来使用vim.api.nvim_create_autocmdvim.api.nvim_set_hl来设置高亮组。

要设置您的模式/正则表达式,您可以使用VIM函数matchadd通过vim.fn.matchadd

您可以将您的代码转换成Lua:

vim.api.nvim_create_autocmd({'BufRead', 'BufNewFile'}, {
  group = vim.api.nvim_create_augroup('StrikeoutMatch', { clear = true }),
  pattern = { '*.txt' },
  callback = function()
    vim.fn.matchadd('StrikeoutMatch', '\\~\\~.*\\~\\~')
    vim.api.nvim_set_hl(0, 'StrikeoutColor', { bg=016, fg='Black' })
    vim.api.nvim_set_hl(0, 'StrikeoutMatch', { link='StrikeoutColor' })
  end,
})
英文:

See "Autocmd Functions" in Neovim API to use vim.api.nvim_create_autocmd and vim.api.nvim_set_hl to set Highlight groups.

To set your pattern/regexp, you could use VIM function matchadd via vim.fn.matchadd.

You could convert your code in Lua:

vim.api.nvim_create_autocmd({'BufRead', 'BufNewFile'}, {
  group = vim.api.nvim_create_augroup('StrikeoutMatch', { clear = true }),
  pattern = { '*.txt' },
  callback = function()
    vim.fn.matchadd('StrikeoutMatch', '\\~\\~.*\\~\\~')
    vim.api.nvim_set_hl(0, 'StrikeoutColor', { bg=016, fg='Black' })
    vim.api.nvim_set_hl(0, 'StrikeoutMatch', { link='StrikeoutColor' })
  end,
})

huangapple
  • 本文由 发表于 2023年7月3日 19:54:30
  • 转载请务必保留本文链接:https://go.coder-hub.com/76604494.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定