2

I'm using Neovim and want to use listchars to replace certain Unicode code points, such as U+3000 (Ideographic Space) with U+2B1C (White Large Square).

vim.opt.listchars = {
  tab = '→ ',
  -- space = '•',
  trail = '·',
  -- extends = '>',
  -- precedes = '<',
  -- eol = '⏎',
  nbsp = ' ',
  -- ['U+3000'] = '⬜' -- 👈 Ineffective
}

Is there a way to achieve this or make it easier to recognize?

1 Answer 1

1

Currently, it seems no way to arbitrarily replace any Unicode character using listchars (issues:22017)

You can consider using nvim_create_autocmd to target specific events {BufRead, ...} to change the color of specific content to make it more noticeable

vim.api.nvim_create_autocmd(
  { "BufRead", "BufNewFile" },
  {
    group = vim.api.nvim_create_augroup("HighlightFullWidthSpace", {}),
    pattern = "*",
    callback = function()
      local groupNameCJKSpace = "CJKFullWidthSpace"
      vim.fn.matchadd(groupNameCJKSpace, ' ') --  Create group mapping: Match the special symbol U+3000
        -- vim.fn.matchadd(groupNameCJKSpace, 'A') -- If you want to apply this color to other content, you can add multiple matchadd

        -- Set highlighting for this group
        vim.api.nvim_set_hl(0, groupNameCJKSpace, {
        bg = "#a6a6a6", -- Background color
        fg = 'white',   -- Foreground color
        -- You can also add other attributes, such as:
        -- bold = true,
        -- italic = true,
        -- underline = true
      })

      -- (Another example below)
      local groupNameTODO = "myTODO"
      vim.fn.matchadd(groupNameTODO, 'TODO .*')
      vim.api.nvim_set_hl(0, groupNameTODO, { fg = "#8bb33d", italic = true })
    end
  }
)

<p style="background-color:#a6a6a6;color:white">#a6a6a6</span>
<p style="color:#8bb33d"><i>#8bb33d italic</i></span>

enter image description here

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.