I don't know how to achieve exactly what you want, only a part.
You want to hide some text inside ~/.vimrc
(for example).
First we need to know which highlight group handles the text you want to hide.
Add this mapping to your ~/.vimrc
:
map <F10> :echo "hi<" . synIDattr(synID(line("."),col("."),1),"name") . '> trans<'
\ . synIDattr(synID(line("."),col("."),0),"name") . "> lo<"
\ . synIDattr(synIDtrans(synID(line("."),col("."),1)),"name") . ">"<CR>
I've taken it from here.
Next put your cursor on some text you want to hide, for example a comment. Then hit F10
.
The name of the highlight group should appear on the command line.
On my system the output is :
hi<vimLineComment> trans<vimLineComment> lo<Comment>
I think it means that the highlight group for vim comments is vimLineComment
and that it's linked to Comment
.
So we want to hide the text that is handled by the highlight group vimLineComment
(or Comment
).
To achieve this, we could use the conceal feature added in vim 7.3 (:help conceal
).
:syntax match vimLineComment "^\".*" conceal containedin=ALL cchar=>
This line tells vim to search text whose highlight group is vimLineComment
and which matches the regex ^\".*
(a beginning of a line (^
) followed by a double quote (\"
) followed by any text (.*
)), and to hide and replace it everywhere it's found (containedin=ALL
) with the character >
(cchar=>
).
The conceal
syntax attribute will hide the text we want, unless the cursor is on it.
Next, we tell vim to completely hide the text unless it has a custom replacement character defined :
:set conceallevel=2
There are 4 possible values for this option with 4 different effects (see :help conceallevel
).
As we don't want to type those commands every time, we add an autocommand in our ~/.vimrc
:
augroup hide_text
autocmd!
autocmd FileType vim :set conceallevel=2 | :syntax match vimLineComment "^\".*" conceal containedin=ALL cchar=>
augroup END
Finally, we add a mapping to toggle the value of the conceallevel
option (between 2 and 0 ; 2 = hidden, 0 = shown ) :
nnoremap <leader>h :<c-u>if (&conceallevel==2) \| set conceallevel=0 \| else \| set conceallevel=2 \| endif<cr>
The result is that the text should be shown / hidden when we hit <leader>h
.
You can also change the color of the replacement character :
highlight Conceal ctermfg=<code color> guifg=#<code color>
And you can choose in which mode a comment will be displayed. For example, if you want it to be shown only when you're on its line in insert mode :
setlocal concealcursor=ncv
However I don't know how to collapse all those hidden lines like the folding feature does, sorry.
Edit : another possible solution.
You keep your folding settings to collapse the lines you don't want to see, and you hide the titles of the folds, so that they don't distract you.
function! FoldingTitle()
if (v:foldlevel>1)
let line = repeat(' ', &sw * (v:foldlevel-1)).'>'
else
let line = '|'
endif
return line
endfunction
set foldtext=FoldingTitle()
set fillchars+=fold:\
set foldcolumn=3 " optional
highlight FoldColumn ctermfg=<code color> guifg=#<code color> ctermbg=<code color> guibg=#<code color> "optional
highlight Folded ctermfg=<code color> guifg=#<code color> ctermbg=<code color> guibg=#<code color> "optional
In this code we customize the title of the folds by giving the value FoldingTitle()
to the foldtext
option.
It means that we can change how a fold will look through this function.
FoldingTitle()
just returns a symbol so that you know there's a fold (>
preceded by some repeated spaces, or |
depending on the foldlevel).
Then we define the space character as the character to use to fill the empty space on the lines :
set fillchars+=fold:\
Don't forget the space after the backslash.
Finally we define a column (whose width is 3) which will be shown at the side of the window and which will indicate open and closed folds. We also customize the color of the foldcolumn as well as the color of the titles of the folds. Each of these 3 lines is optional.
set foldcolumn=3 " optional
highlight FoldColumn ctermfg=<code color> guifg=#<code color> ctermbg=<code color> guibg=#<code color>
highlight Folded ctermfg=<code color> guifg=#<code color> ctermbg=<code color> guibg=#<code color>