I'm trying to fold some PHP code, and I have included a cut-down version of some of the code I'm trying to fold at the end of the post.

I've tried the following vim commands, but only the ex commands below work. The non-ex commands don't work and produce a white-on-red message in the status line saying: I can't find any folds.

:help folding
:help fold-commands
:help foldmethod

:set foldmethod=syntax

zi - toggle folding

zj - move to top of next fold
zk - move to bottom of previous fold

za - toggle current fold open and closed
zo - open current fold
zc - close current fold

zA - toggle all current folds at the current cursor position
zO - open all current folds at the current cursor position
zC - close all current folds at the current cursor position

How can I get vim to find the folds (e.g. I want to fold code between { and }, or between ( and ).

Here is some sample code (just to show that it is syntactically correct and hence the vim commands should work):

function getTree() {

  return array(
    "node1" => array(
      "node11" => array(
        "node111" => "leaf111",
        "node112" => "leaf112",
        "node113" => "leaf113",
          "node12" => array(
        "node121" => "leaf121",
        "node122" => "leaf122",
        "node123" => "leaf123",
      "node13" => array(
        "node131" => "leaf131",
        "node132" => "leaf132",
        "node133" => "leaf133",
    "node2" => array(
      "node21" => array(
        "node211" => "leaf211",
        "node212" => "leaf212",
        "node213" => "leaf213",
          "node22" => array(
        "node221" => "leaf221",
        "node222" => "leaf222",
        "node223" => "leaf223",
      "node23" => array(
        "node231" => "leaf231",
        "node232" => "leaf232",
        "node233" => "leaf233",
    "node3" => array(
      "node31" => array(
        "node311" => "leaf311",
        "node312" => "leaf312",
        "node313" => "leaf313",
          "node32" => array(
        "node321" => "leaf321",
        "node322" => "leaf322",
        "node323" => "leaf323",
      "node33" => array(
        "node331" => "leaf331",
        "node332" => "leaf332",
        "node333" => "leaf333",


Vim doesn't come with PHP syntax folding built-in. However, if all of your code is properly indented (as your example is), you can use a different fold method:

:set foldmethod=indent
| improve this answer | |

phpfolding.vim provides this. The advantage of this over :set foldmethod=indent is that it's "smarter" because it looks at the actual PHP syntax, and not just the indentation. From the README:

  • It remembers fold settings. If you add functions and execute the script again, your opened folds will not be closed.
  • It will not be confused by brackets in comment blocks or string literals.
  • The folding of class properties with their PhpDoc comments.
  • The folding of all class properties into one fold.
  • Folding the original marker style folds too.
  • An "**" postfixing the fold indicates PhpDoc is inside (configurable).
  • An "**#@+" postfixing the fold indicates PhpDocBlock is inside (configurable).
  • Empty lines postfixing the folds can be configured to be included in the fold.
  • Nested folds are supported (functions inside functions, etc.)
  • Folding private, public, protected class variables + multi-line param.
  • Foloding the class is now an option disabled by default.

Install it by downloading the .vim script, and putting it in ~/.vim/ftplugin/php/ (or use your plugin manager, if you use one). It will fold automatically, which you can disable with let g:DisableAutoPHPFolding = 1.

| improve this answer | |
  • I will try this plugin. I'm sure the extra features it provides will be useful since sometimes when writing code there are syntax errors. Thanks. – John Sonderson Feb 6 '15 at 17:16

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.