3

I use Vim version 8.2.4919 without 3rd party plug-ins on Ubuntu 22.04.2 LTS running via WSL2.

I want to have italic syntax highlighting for words encapsulated in underscores in comments of my cpp files.

Following is the content of my .vimrc that does that:

"italic syntax highlighting
autocmd FileType cpp,hpp syntax match Italic "\v<_[a-zA-Z0-9_]*_>" containedin=cComment,cCommentL contained
autocmd FileType cpp,hpp syntax match ConcealUnderscore /_/ containedin=Italic contained conceal
highlight Italic cterm=italic gui=italic ctermfg=lightcyan guifg=lightcyan

setlocal conceallevel=2
setlocal concealcursor=nc

This works fine as long as I open my cpp/hpp files from terminal.

But as soon as I open a directory with vim and use netrw (v171) to open a source file, the underscores don't get concealed.

Opened from terminal:

opened from terminal

Opened from netrw:

opened from netrw

What changes do I need to do to my .vimrc/cpp.vim to conceal underscores when opening files from netrw?

I already tried:

  • Creating a ~/.vim/syntax/cpp.vim and doing the highlighting there (I will use this file instead of .vimrc if possible)
  • autocmd BufReadPost * syntax match ConcealUnderscore /_/ containedin=Italic contained conceal
  • autocmd BufEnter * if &ft == 'cpp' || &ft == 'hpp' | syntax match ConcealUnderscore /_/ containedin=Italic contained conceal | endif
  • autocmd BufReadPost * :syntax sync fromstart

I expected: underscores to be concealed when opening files from netrw

4
  • When you open the file with netrw what is the value of conceallevel? What is the result of the following command: :set conceallevel? Commented Oct 12, 2023 at 14:06
  • Totally offtopic but while std::vector expands in size as needed, I don't think it ever releases memory unless you make it do so.
    – Friedrich
    Commented Oct 12, 2023 at 14:41
  • _M_guaranteed_capacity is shrunk in shrink_to_fit() and swap(). Commented Oct 12, 2023 at 14:52
  • I agree. What you write translates roughly to "memory expands or shrinks as needed to adapt to the stored elements" which I would understand as a kind of an automatism. In fact, the automatism exists for expansion but not for shrinking. Maybe I'm being nitpicky again so let's forget about it. You asked about conceal and Vivian wrote a great answer.
    – Friedrich
    Commented Oct 12, 2023 at 18:35

1 Answer 1

1

I would do:

"italic syntax highlighting
autocmd FileType cpp,hpp syntax match Italic "\v<_[a-zA-Z0-9_]*_>" containedin=cComment,cCommentL contained
autocmd FileType cpp,hpp syntax match ConcealUnderscore /_/ containedin=Italic contained conceal
highlight Italic cterm=italic gui=italic ctermfg=lightcyan guifg=lightcyan

autocmd FileType cpp,hpp setlocal conceallevel=2 concealcursor=nc

The conceallevel of the netrw buffer is 0 and so is by default the conceallevel of the buffer open from it. That is why you have to ensure it is set correctly.

1
  • 1
    Perhaps that’s a netrw bug? It should undo conceal changes when loading other buffers (probably via b:undo_ftplugin)
    – D. Ben Knoble
    Commented Oct 12, 2023 at 16:40

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.