Search code examples
vimvim-syntax-highlighting

Replace concealed characters with a space


I use the conceal feature of vim when editing LaTeX files. One example of this feature in action is the concealing of textit macros, causing

\textit{Hi there}

to be displayed as

Hi there

. This is generally great, but it does mean that characters are no longer displayed in their proper column. What I would really want is for all the concealed characters to be replaced by spaces, rather than just being removed from the line, so that column numbers are preserved. The result in the above case would be:

        Hi there 

Can this be done?


Solution

  • You can only specify a single conceal-replacement character (cchar) at a time. So

    syn match C1 "\\textit{" conceal cchar= 
    "                                      ^------ single space after equal sign
    

    will make the text:

    \textit{Hi there}
    

    look like:

     Hi there}
    ^--- space before "Hi"
    

    If you want to replace every character of \\textit with a space, you will have to split the concealment in parts (again, a space after every equal sign):

    syn match C1 contained "\\" conceal cchar=                           
    syn match C2 contained "t" conceal cchar=                            
    syn match C3 contained "e" conceal cchar=                            
    syn match C4 contained "x" conceal cchar=                            
    syn match C5 contained "t" conceal cchar=                            
    syn match C6 contained "i" conceal cchar=                            
    syn match C7 contained "t" conceal cchar=                            
    syn match C8 contained "{" conceal cchar=                            
    syn match C9 contained "}" conceal                          
    syn match Conceal "\\textit{\|}" contains=C1,C2,C3,C4,C5,C6,C7,C8,C9        
    set conceallevel=2                                                   
    set concealcursor=vic