r/vim • • Jun 14 '20

TIL: Conceal in Vim

Post image
608 Upvotes

49 comments sorted by

98

u/[deleted] Jun 14 '20

So, just like prettify-symbol-mode in Emacs, in Vim, you can use conceal in a custom syntax file to replace something with just a single symbol.

In the screenshot above, I replace [ ] and [x] into an actual checkbox symbols, which make Vim even more useful for me!

You can take a look at the syntax file here https://gist.github.com/huytd/668fc018b019fbc49fa1c09101363397

36

u/shpidoodle Jun 14 '20

The real question is, what font is that?

5

u/[deleted] Jun 15 '20

It's Roboto Mono, I also have a modified version of it here https://github.com/huytd/roboplex-font

6

u/whisty Jun 14 '20

It looks like Roboto Mono (take a look at the "l" for example). https://fonts.google.com/specimen/Roboto+Mono

It's definitely not Haskplex.

1

u/loneghost161 Jun 15 '20

Looks like source code pro to me

-4

u/Hazanami Jun 14 '20

Apparently it's Nerdfont.

20

u/shpidoodle Jun 14 '20

Nerd font is a type of font that supports certain font characters not available in normal fonts. There are hundreds of variants of Nerdfonts.

https://github.com/ryanoasis/nerd-fonts

22

u/haha-ok Jun 14 '20

I love conceal but one of my biggest vim annoyances is that it works really weirdly combined with soft-wrapping text. If not for this it would be really useful for hiding long URLs in markdown links etc.

4

u/adventurecoat Jun 20 '20

Yep, this really limits the utility of conceal. I wonder how emacs orgmode handles this (as it doesn’t seem to have this problem): perhaps orgmode uses something like text-properties instead? I’m actually 70% done with a project that stores links in an external file, keyed to the filename and line (means there can only be one link per line). When the buffer is changed, the flat-file “links db” is updated. This enables using short titles/labels for links that can still load the target url.

Like so many side projects, it kinda fizzled out. The solution began to feel too complicated for the benefit derived.

2

u/haha-ok Jun 20 '20

That sounds interesting.

I think the soft-wrapping problem is not conceptually difficult to solve: just wrap based on visible characters. It is just (as Bram commented): "it's going to be a lot of work".

1

u/parentis_shotgun Jun 14 '20

Yep, wanted this to be good for markdown editing, but the wrapping is too strange to look at.

14

u/[deleted] Jun 14 '20

I just don't understand conceal at all. I've wanted to use it multiple times but the feature is so obtuse to me.

24

u/Schnarfman nnoremap gr gT Jun 14 '20 edited Jun 14 '20

It be like that sometimes. Vim is a bunch of different systems cobbled together. If you mess around and write a syntax file yourself then you'll find it really intuitive after a while. Or you won't. But I think you will :P

Here's some code you can play with. Try it out:

" Gotta refresh everything whenever you source this
command! RefreshMySyntax :source ~/Desktop/concealTester.vim
syntax clear

"""""""""""""""""""""""""""""""" Conceallevel """"""""""""""""""""""""""""""""
"set conceallevel=0 " Nothing is hidden
"set conceallevel=1 " Hide stuff as concealchar, or as listchar.
"set conceallevel=2 " Hide stuff as concealchar, or completely.
"set conceallevel=3 " Hide completely.

"""""""""""""""""""""""""""""""" Syntax terms """"""""""""""""""""""""""""""""
syntax keyword ConcealedX kw_hide_x conceal concealchar=x
syntax keyword ConcealedY kw_hide_y conceal concealchar=y
syntax keyword ConcealedMe kw_hide_me conceal
syntax keyword NoConceal kw_normal

""""""""""""""""""""""""""""""""""" Colors """""""""""""""""""""""""""""""""""
highlight ConcealedX ctermfg=2
highlight ConcealedY ctermfg=3
highlight ConcealedMe ctermfg=4
highlight NoConceal ctermfg=5

Useful plugin for picking colors: https://github.com/guns/xterm-color-table.vim (Either install it, or simply look at the picture on the readme!)

7

u/[deleted] Jun 14 '20

Thanks for the impromptu guide! I'll try this out.

7

u/Schnarfman nnoremap gr gT Jun 14 '20

Omg you responded so fast, I totally edited my response to try and be a lil more helpful but I think I ended up changing the comment a little too much, then I forgot what I had originally. Hopefully it's still helpful haha.

You will get different results for conceallevel 1 and 2 when you have syntax keyword vs. something that can span across multiple words like syntax match or syntax region.

Please share your thoughts when you're done. I would love to hear what actions you took to achieve an ah-hah moment on it!

4

u/[deleted] Jun 14 '20

Will do friendo

4

u/when_raisins_attack Jun 14 '20

Here's a little tutorial on how to use conceal. Make sure you put your conceal rules not into your vimrc but into ~/.vim/after/syntax/<filetype>.vim.

If you don't like highlighting the replacement text, here's a Stack Exchange answer to fix that.

1

u/[deleted] Jun 14 '20

I add the code snippet from the author to .config/nvim/after/syntax/markdown.vim but It didn’t work. I use plasticboy/markdown-vim is that the problem. thanks

1

u/when_raisins_attack Jun 14 '20

It's probably best if you describe your issue at the Vim site of Stack Exchange. Describe what you expect and what you got instead (with screenshots if it makes sense). Also, don't forget to activate the concealing with setlocal conceallevel=2.

2

u/RedditTheBarbarian Jun 14 '20

This is great!

A couple tips based on my implementation...

To make sure highlighting is turned off in the terminal (which I use by default):

highlight Conceal ctermbg=NONE ctermfg=NONE guibg=NONE guifg=NONE

(note: NONE must be in all caps or else you'll get an error)

Also, to enable this feature by extending other file type syntax, like *.md (Markdown), for example, add the following code to ~/.vim/after/syntax/markdown.vim:

" Vim syntax file
" Add checkboxes to *.md files
" source: https://gist.github.com/huytd/668fc018b019fbc49fa1c09101363397

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

hi def link todoCheckbox Todo

highlight Conceal ctermbg=NONE ctermfg=NONE guibg=NONE guifg=NONE

setlocal cole=1

1

u/avdyushin Jun 16 '20 edited Jun 16 '20

Spent hours to make it work with existing markdown syntax.

It's correct to put under after/syntax/markdown.vim but instead of syntax match it should call matchadd to extend existing:

call matchadd('Conceal', '\[\ \]', 0, 11, {'conceal': ''})
call matchadd('Conceal', '\[x\]', 0, 12, {'conceal': ''})

1

u/RedditTheBarbarian Jun 16 '20

Weird. My solution worked on my machine, but whatever works! Might be a difference in version.

1

u/Akinsho Jun 17 '20

I was having a similar problem and this seems to have worked for me except now conceal characters for other words in the line have been replaced with the empty symbol. Have you had the same issue? any ideas?

1

u/avdyushin Jun 18 '20

Yes, turning conceal level on hides code blocks symbols and simplify links, so it depends on markdown itself.

1

u/RedditMetJoost Jun 14 '20

I love conceal, especially when editing things like LaTeX or markdown. But I still find conceal to be to slow when also using a colorscheme.

1

u/mayor123asdf Jun 14 '20

Oh my god I still use [ ] and [x] on my to do list! this is hella useful! How do you toggle them? the only thing crosses my mind is to do search and replace on line

4

u/rampidamp Jun 14 '20 edited Jun 14 '20

Create a mapping for yourself: nnoremap <leader>t V:s/\(\s*\)[ ]/\1[x]/<esc>. (There might be mistakes in there, I didn't test it - am on mobile - also it's just for checking, not unchecking)

EDIT: add escaping for ()

EDIT2: this is the correct mapping: nnoremap <leader>t :s/\(\s*\)\[ \]/\1\[x\]/<cr>

For unchecking: nnoremap <leader>T :s/\(\s*\)\[x\]/\1\[ \]/<cr>

1

u/rampidamp Jun 14 '20

Or, you can use vim-todo which handles the ToDo-filetype

1

u/nano_remix Jun 14 '20

What vim-todo

2

u/rampidamp Jun 14 '20

https://github.com/aserebryakov/vim-todo-lists

Sorry, I mis-remembered the name...

2

u/nano_remix Jun 14 '20

Awesome. Thank you mate 🙏

→ More replies (1)

4

u/BobWhitelock Jun 14 '20

This plugin is quite useful for working with bullets and checkboxes and includes maps for toggling them (amongst other things): https://github.com/dkarter/bullets.vim

2

u/haha-ok Jun 14 '20

If you want a plugin just for toggling there is https://github.com/jkramer/vim-checkbox.

1

u/[deleted] Jun 14 '20

Would be cool to combine this with vim-orgmode

1

u/gnagnone Jun 14 '20

it would be great to combine this, or any of the other plugins people is talking about in this thread, with vimwiki

1

u/rafaeln Jun 14 '20

Very nice my friend. I've just copied it.

1

u/isaifmd Jun 17 '20

I learnt about it while trying to use markdown in vim.

1

u/Xx_RKJ_xX Sep 22 '23 edited Sep 23 '23

Based on the gist above, for users working in neovim in lua, you can put the following in 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()

PS: There is no question like this in r/neovim, so I'm adding this here. I'm not sure how to translate this to pure vim. However, the commands should be somewhat equivalent.

0

u/[deleted] Jun 14 '20

[deleted]

4

u/[deleted] Jun 14 '20

0

u/[deleted] Jun 14 '20

[deleted]

1

u/[deleted] Jun 14 '20

Interesting, in which tasks do you use vimwiki?

2

u/[deleted] Jun 14 '20

I'm not the guy you asked, but vimwiki uses [ ],[X] with it's todo stuff. But it also has a progressive checkmark that looks like:

[ ] -- 0% complete

[.] -- 1-33% complete

[o] -- 34-66% complete

[O] -- 67-99% complete

[X] -- 100% complete

I could see that looking better. Not really useful, but it would be nice to be prettier I suppose.

1

u/[deleted] Jun 14 '20 edited Jun 14 '20

Do you use it for programming purpose? To keep some information or something like that? I recently started to use nvim to Python coding. I like to learn anything productive ways of coding in VIM environment

2

u/[deleted] Jun 14 '20

Oh, sorry totally missed the point of your question. I use vimwiki for just about all my work (math teacher). Not a programmer, but my lecture notes and any handouts are all Latex, organized by date/subject in vimwiki with links to the raw latex and the finished pdfs. I keep my todo lists on it, and have been trying to make use of the diary with regularity (not really work related, more just life stuff).

I suppose you could use it to link to different projects you're working on. If you're just programming and don't really have a use for a wiki organizing tool, it won't do much for you. Still a pretty nice plugin.

There are people who use vimwiki to write/publish their websites as well. It has easy exporting for that.

1

u/[deleted] Jun 14 '20

Thanks.