1

I have the following on my vimrc file to handle custom folding:

" ++ Folding                                                                    
 set foldenable                      " enable folding                            
 set foldmethod=syntax               " global folding method                
 set foldlevelstart=1                " folds with level 1 aren't closed 

augroup FileTypeVim                                                                                                                                                 
   autocmd!                                                                    
   autocmd FileType vim setlocal foldmethod=expr foldexpr=VimrcFold()          
augroup END

When I open my vimrc it works perfectly fine. However, if I re-source my vimrc the foldmethod is set to syntax (global) instead of expr (specific for filetype vim). What do I have to specify in order to keep the vimrc with expr foldmethod? I suspect it has to be with BufType rather than FileType but I am not sure how to deal with that.

2 Answers 2

1

You can do that with a modeline and an autocommand. E.g.

The modeline :

" vim: set sw=4 ts=4 sts=4 et tw=78 foldmarker={,} foldlevel=0 foldmethod=marker spell:

With this modeline I can fold my vimrc based on {} pairs.

The autocommands :

augroup FileTypeVim
   autocmd!
   " Source your vimrc on save
   autocmd! BufWritePost .vimrc source %
   " Apply modeline option after re-openning the vimrc file (that is after sourcing it)
   autocmd! BufWritePost .vimrc set modeline | doautocmd BufRead
augroup END

References :

1
  • I tried both solutions but this one worked as I wanted. I am not a huge fan of modelines but for my own vimrc seems useful. I think something in my vimrc is preventing christian's solution to work, otherwise I'd choose his as the accepted answer. Anyways, thank you all for the help.
    – Sergio
    Aug 27, 2015 at 13:33
2

When resourcing your .vimrc you will overwrite your current foldmethod setting and reset it to "syntax". The autocommand won't trigger, since by that time, the filetype has already been set.

One way around it, would be to check your current foldmethod and only set it, if it set to manual, e.g. like this:

if &l:fdm is# 'manual'
    let &fdm='syntax'
 endif

This checks the local foldmethod setting for the current buffer and only sets the global option, if the local value is at the default value of 'manual' (assuming it had not been set).

4
  • I would add a check that the current file is actually the vimrc file, otherwise it will apply that on every file.
    – nobe4
    Aug 27, 2015 at 7:13
  • When sourcing the vimrc the current file should be the vimrc, right? Aug 27, 2015 at 8:13
  • Yes, but the vimrc file is sourced everytime you open vim, so let say you open it with a document that contain some inline configuration (e.g. a modeline) it will override it. You can make the condition change as little as expand('%') == '.vimrc' (on unix) and it will override the config only for the vimrc file.
    – nobe4
    Aug 27, 2015 at 8:17
  • I don't understand your problem. modelines are executed later than the .vimrc so it shouldn't matter, also this happens only on startup or when manually sourcing the file, which is rather seldom, so this will not have a performance impact. Aug 27, 2015 at 8:47

Your Answer

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

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