Skip to content

Instantly share code, notes, and snippets.

@huytd
Created June 14, 2020 07:34
Show Gist options
  • Star 77 You must be signed in to star a gist
  • Fork 5 You must be signed in to fork a gist
  • Save huytd/668fc018b019fbc49fa1c09101363397 to your computer and use it in GitHub Desktop.
Save huytd/668fc018b019fbc49fa1c09101363397 to your computer and use it in GitHub Desktop.
A Todo list syntax in Vim, with an actual checkbox
" Vim syntax file
" Language: Todo
" Maintainer: Huy Tran
" Latest Revision: 14 June 2020
if exists("b:current_syntax")
finish
endif
" Custom conceal
syntax match todoCheckbox "\[\ \]" conceal cchar=
syntax match todoCheckbox "\[x\]" conceal cchar=
let b:current_syntax = "todo"
hi def link todoCheckbox Todo
hi Conceal guibg=NONE
setlocal cole=1
@huytd
Copy link
Author

huytd commented Jun 14, 2020

It's required to use a nerdfont to have the icons displayed correctly.

image

@mrassili
Copy link

@huytd what font is that exactly?

@hutchison
Copy link

@mrassili
Copy link

thanks!

@mawkler
Copy link

mawkler commented Jun 14, 2020

This seems to only work if the file does not have a defined syntax.

How could I get this to work for Markdown files?

@20jam
Copy link

20jam commented Jun 14, 2020

How could I get this to work for Markdown files? +1

@huytd
Copy link
Author

huytd commented Jun 14, 2020 via email

@20jam
Copy link

20jam commented Jun 14, 2020

Thank you, I've tried everything but just won't work. I look forward, thanks

@huytd
Copy link
Author

huytd commented Jun 14, 2020 via email

@20jam
Copy link

20jam commented Jun 14, 2020

Amazing thanks for the help

to make it work in lists

syntax match todoCheckbox "- \[\ \]" conceal cchar=
syntax match todoCheckbox "- \[x\]" conceal cchar=

@mawkler
Copy link

mawkler commented Jun 24, 2020

This doesn't work for lists in lists, since only the outermost list gets concealed. For example:

- [ ] foo
  - [x] bar
  - [ ] baz
- [ ] foz

I tried to solve it with the following:

syntax match todoCheckbox /\v\s*- \[\ \]/ conceal cchar=
syntax match todoCheckbox /\v\s*- \[x\]/ conceal cchar=

But that removes the indentation when concealing. What pattern do I need in my syntax match to match but not conceal the indentation? I tried to use regex capture groups (which I think is how you should do it) but couldn't get it working correctly.

@disanman
Copy link

disanman commented Feb 13, 2021

I solved the indentation problem using hs :

syntax match my_todo '\v(\s+)?-\s\[\s\]'hs=e-4 conceal cchar=
syntax match my_todo_done '\v(\s+)?-\s\[X\]'hs=e-4 conceal cchar=

More info can be found in :h syn-pattern-offset

@mawkler
Copy link

mawkler commented Feb 13, 2021

@disanman Thank you. However I seem to still have the same problem with the indentation getting eaten up by the conceal which causes all checkboxes to have the same indentation.

Never mind I used the wrong syntax match lol.

@disanman
Copy link

disanman commented Feb 15, 2021

@Melkster nice you found the issue :)
Check I also added support for VimWiki ToDo's state using some symbols I found:
Screenshot 2021-02-15 at 18 08 30

syntax match VimwikiListTodo '\v(\s+)?(-|\*)\s\[\s\]'hs=e-4 conceal cchar=
syntax match VimwikiListTodo '\v(\s+)?(-|\*)\s\[X\]'hs=e-4 conceal cchar=
syntax match VimwikiListTodo '\v(\s+)?(-|\*)\s\[-\]'hs=e-4 conceal cchar=
syntax match VimwikiListTodo '\v(\s+)?(-|\*)\s\[\.\]'hs=e-4 conceal cchar=
syntax match VimwikiListTodo '\v(\s+)?(-|\*)\s\[o\]'hs=e-4 conceal cchar=

@rohit-kumar-j
Copy link

rohit-kumar-j commented Sep 23, 2023

For users working in neovim, you can put this in your ftplugin/markdown.lua.

function _G.pcall_markdown_sugar()
  vim.cmd([[
    augroup markdown
      autocmd!
      au BufEnter *.md :syn match todoCheckbox '\v(\s+)?(-|\*)\s\[\s\]'hs=e-4 conceal cchar=
      au BufEnter *.md :syn match todoCheckbox '\v(\s+)?(-|\*)\s\[x\]'hs=e-4 conceal cchar=
      au BufEnter *.md :syn match todoCheckbox '\v(\s+)?(-|\*)\s\[-\]'hs=e-4 conceal cchar=󰅘
      au BufEnter *.md :syn match todoCheckbox '\v(\s+)?(-|\*)\s\[\.\]'hs=e-4 conceal cchar=⊡
      au BufEnter *.md :syn match todoCheckbox '\v(\s+)?(-|\*)\s\[o\]'hs=e-4 conceal cchar=⬕
      :hi def link todoCheckbox Todo
      :highlight Conceal guibg=NONE guifg=#00cf37
      :setlocal conceallevel=1
    augroup END
  ]])
end

pcall_markdown_sugar()

@mawkler
Copy link

mawkler commented Sep 25, 2023

@rohit-kumar-j I took the liberty of improving your code to use the available Neovim Lua APIs:

local function markdown_sugar()
  local augroup = vim.api.nvim_create_augroup('markdown', {})
  vim.api.nvim_create_autocmd('BufEnter', {
    pattern = '*.md',
    group = augroup,
    callback = function()
      vim.api.nvim_set_hl(0, 'Conceal', { bg = 'NONE', fg = '#00cf37' })
      vim.api.nvim_set_hl(0, 'todoCheckbox', { link = 'Todo' })
      vim.bo.conceallevel = 1

      vim.cmd [[
        syn match todoCheckbox '\v(\s+)?(-|\*)\s\[\s\]'hs=e-4 conceal cchar=
        syn match todoCheckbox '\v(\s+)?(-|\*)\s\[x\]'hs=e-4 conceal cchar=
        syn match todoCheckbox '\v(\s+)?(-|\*)\s\[-\]'hs=e-4 conceal cchar=󰅘
        syn match todoCheckbox '\v(\s+)?(-|\*)\s\[\.\]'hs=e-4 conceal cchar=⊡
        syn match todoCheckbox '\v(\s+)?(-|\*)\s\[o\]'hs=e-4 conceal cchar=⬕
      ]]
    end
  })
end

markdown_sugar()

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment