Vim folding for PHP


Vim provides nice syntax folding for Ruby and C (well, that is what I've tried) out of the box, but what about PHP? It doesnt have this feature! Am I missing something?

1/19/2012 1:18:07 PM

Accepted Answer

Apparently my VIM didn't run :syntax enable.

Doing :syntax enable fixed the problem, but I also added :syntax on to .vimrc

4/27/2009 12:45:42 PM

:syntax enable (or :syntax on) work because both those options also turn on filetype detection. The filetype has to be detected before folding or highlighting work.

If you're developing in PHP you probably want to add these three lines to your .vimrc

set nocompatible          " Because filetype detection doesn't work well in compatible mode
filetype plugin indent on " Turns on filetype detection, filetype plugins, and filetype indenting all of which add nice extra features to whatever language you're using
syntax enable             " Turns on filetype detection if not already on, and then applies filetype-specific highlighting.

Then you can put your let g:php_folding=2 and set foldmethod=syntax in your ~/.vim/after/ftplugin/php.vim file.

This will keep your .vimrc file clean, help organize all your settings, and the foldmethod=syntax will only affect php files (If you want to set syntax as your default fold method for all filestypes, leave that line in your .vimrc file)

For more detailed information read these help files:

:help filetype
:help usr_05.txt
:help usr_43.txt

Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow