8

I would like to be able to hide certain lines in my buffer from showing up at all (and being able to toggle seeing them). I found similar questions, but they don't solve my problem. I don't want to use folds for this.

What do I want exactly?

When I open a document, I want Vim to not read in commented lines. It should behave as if these lines do not exist at all in the document, with only one exception: I need a visual indicator ( e.g. in an additional column at the side) that tells me here is a comment which is not displayed.

Upon pressing a certain key combination, I want all comments to show up (i.e. normal Vim behaviour).

I want to be able to modify the document with comments not displayed. Upon save, the whole document is saved with comments (they are always saved 1 line above the text which was under them in the beginning).

Why do I want this?

It destracts less and looks visually more pleasing. Take the .vimrc for example - you can have all settings nicely below each other. If you want to change something, or are unsure why you set it this way, just toggle your comments.

  • Hope you don't mind my asking... Why are you specifically excluding folds? – Roflo Nov 19 '15 at 16:52
  • 1
    Of course I don't :) I excluded folds cause I know how to collapse comments with folds - but folded comments still distract, hinder the reading flow and are not really visually appealing. – cbaumhardt Nov 19 '15 at 17:14
  • I want a lot of things too, but I found out it's usually better to stick/adapt to what I need ;) – VanLaser Nov 19 '15 at 17:40
  • @cbaumhardt you could fold it and use a custom 'foldtext' setting/function to hide everything – Christian Brabandt Nov 19 '15 at 21:28
7

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>
  • 1
    This is really great, I did not know about the conceal feature. With your solution the lines are still in there, but are either completely empty or have just one char - so I have way less distraction looking at my code. I am still hoping there is a way to not show specific lines at all, but this solution is also good (way better than folding for me). – cbaumhardt Nov 19 '15 at 18:29
  • Wow, conceal is really great, you can even conceal only parts of a line (vertical folding): vim.wikia.com/wiki/Patch_to_conceal_parts_of_lines – cbaumhardt Nov 19 '15 at 18:47
  • @cbaumhardt I've edited my answer to include another proposition, that maybe could interest you. – saginaw Nov 19 '15 at 19:58
  • The proposal in your edit is not really an option, cause I want to be able to still use my normal folding. But when I do so, the Folded elements for my normal folds need color, so the only option is to just fill the foldtext for the lines I want to hide with spaces. However, I will still see the underlined part as well as the different background for the Folded objects - and I can't change that, as I need them for my normal folds to work. – cbaumhardt Nov 20 '15 at 1:23
-1

any possible solution is use a minimalist fold bar i used because i don't find the same solution, below i put a list of commands for make a minimalist fold bar that you can use for hidden comments, in each command i write what do it :

set foldtext='+' "for appear a + in text reference for fold

hi Folded ctermbg=NONE "change color fold bar for nothing

:set fillchars=vert:\|,fold:\ "(put a space) no put a ----- in the fold bar

optional use for disappear comments

:hi! link Comment Ignore

return use

:hi! link Comment Comment

  • I am sorry but I can't understand your post. I suggest to write more round sentences in it. – peterh Jun 10 '18 at 17:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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