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 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


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
   " 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 :

  • 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
    Commented Aug 27, 2015 at 13:33

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'

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).

  • I would add a check that the current file is actually the vimrc file, otherwise it will apply that on every file.
    – nobe4
    Commented Aug 27, 2015 at 7:13
  • When sourcing the vimrc the current file should be the vimrc, right? Commented 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
    Commented 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. Commented Aug 27, 2015 at 8:47

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.