# Vim Folding Not Working

Code folding lets you collapse sections of code, making large files easier to navigate. But when folding doesn't work or behaves unexpectedly, it can be frustrating. Let me show you how to configure and fix Vim folding.

Understanding Fold Methods

Vim supports six fold methods:

MethodDescription
manualYou create folds yourself
indentFolds based on indentation
markerFolds based on markers in text (like {{{ and }}})
syntaxFolds based on syntax highlighting
exprFolds based on custom expression
diffFolds unchanged text in diff mode

Check your current method:

vim
:set foldmethod?

Enable Syntax Folding

Most language-specific folding uses the syntax method:

vim
:set foldmethod=syntax

But syntax folding requires syntax definitions that include fold rules. Check if your language supports it:

vim
:syntax list

Look for fold in the syntax definitions.

If syntax folding doesn't create folds, the syntax file might not define fold regions. Add filetype-specific settings:

```vim " Python folding autocmd FileType python setlocal foldmethod=indent

" JavaScript/TypeScript autocmd FileType javascript,typescript setlocal foldmethod=syntax

" JSON autocmd FileType json setlocal foldmethod=syntax ```

Indent Folding

Indent folding is simple and works for many languages:

vim
:set foldmethod=indent
:set foldlevel=0

Configure how many spaces trigger a fold:

vim
:set foldnestmax=10    " Maximum nesting level
:set foldminlines=1    " Minimum lines per fold

Marker Folding

Use markers to define folds:

vim
:set foldmethod=marker
:set foldmarker={{{,}}}

Now add markers in your code:

python
def main():  # {{{
    print("Hello")
# }}}

Manual Folding

Create folds manually:

```vim " Select a range in visual mode, then: zf " Create fold

" Or from command mode: :10,20fold ```

Delete folds:

vim
zd    " Delete fold under cursor
zD    " Delete all folds recursively
zE    " Delete all folds in window

Fold Navigation Commands

KeyAction
zoOpen fold
zOOpen all folds recursively
zcClose fold
zCClose all folds recursively
zaToggle fold
zAToggle all folds recursively
zrReduce folding (open more)
zROpen all folds
zmMore folding (close more)
zMClose all folds
zjMove to next fold start
zkMove to previous fold end

Folds Close Unexpectedly

If folds close when you don't want them to:

vim
:set foldopen=all    " Commands that open folds
:set foldclose=      " Commands that close folds

By default, moving into a fold opens it. To change:

vim
:set foldopen-=search
:set foldopen-=undo

Folds Not Persisting

Folds disappear when you close Vim. Save them:

vim
" Enable fold persistence
set viewoptions=folds
augroup FoldPersistence
    autocmd!
    autocmd BufWinLeave *.py mkview
    autocmd BufWinEnter *.py silent loadview
augroup END

Or manually:

vim
:mkview    " Save current view including folds
:loadview  " Restore saved view

Save view with a number for multiple views:

vim
:mkview 1
:loadview 1

View Directory Not Created

Views need a storage directory:

vim
set viewdir=~/.vim/views

Create it:

bash
mkdir -p ~/.vim/views

For Neovim:

vim
set viewdir=~/.local/share/nvim/views

Expression Folding

For custom folding logic:

```vim set foldmethod=expr set foldexpr=MyFoldExpr()

function! MyFoldExpr() let line = getline(v:lnum) if line =~ '^\s*$' return '-1' " Don't fold blank lines endif let indent = indent(v:lnum) return indent / &shiftwidth endfunction ```

Folds Show Unexpected Content

By default, fold text shows the first line. Customize it:

```vim set foldtext=MyFoldText()

function! MyFoldText() let line = getline(v:foldstart) let count = v:foldend - v:foldstart + 1 return line . ' [' . count . ' lines]' endfunction ```

Plugin-Based Folding

Better folding through plugins:

vim-anyfold

vim
Plug 'pseewald/vim-anyfold'
let g:anyfold_activate = 1
let g:anyfold_fold_comments = 1

FastFold

vim
Plug 'konfekt/fastfold'
let g:fastfold_foldmethod_override = {
    'markdown': ['syntax'],
    'python': ['indent'],
}

Folding for Specific Languages

Python

vim
autocmd FileType python setlocal foldmethod=indent
autocmd FileType python setlocal foldlevel=1

Markdown

```vim autocmd FileType markdown setlocal foldmethod=expr autocmd FileType markdown setlocal foldexpr=MarkdownFoldExpr()

function! MarkdownFoldExpr() let line = getline(v:lnum) if line =~ '^#\+' return '>1' endif return '=' endfunction ```

JSON

vim
autocmd FileType json setlocal foldmethod=syntax

Fold Column Display

Show a column indicating folds:

vim
:set foldcolumn=4    " Width of fold column

The column shows + for closed folds, - for open folds, and | for nested folds.

Quick Fold Settings

```vim " Basic fold setup set foldmethod=indent set foldlevelstart=1 " Start with first level open set foldnestmax=10 set foldenable

" Don't close folds when cursor leaves set foldclose=

" Open folds when searching set foldopen+=search

" Show fold column set foldcolumn=2 ```

Troubleshooting Checklist

  1. 1.Check fold method: :set foldmethod?
  2. 2.Verify syntax defines folds: :syntax list
  3. 3.Check fold level: :set foldlevel?
  4. 4.Ensure foldenable is on: :set foldenable?
  5. 5.Create view directory: mkdir -p ~/.vim/views
  6. 6.Check view persistence settings: :set viewoptions?
  7. 7.Verify fold commands work: zo, zc, za

Quick Reference

```vim " Check fold settings :set foldmethod? foldlevel? foldenable?

" Enable folding set foldenable

" Set method set foldmethod=syntax " or indent, marker, manual, expr

" Navigation zo " Open fold zc " Close fold za " Toggle fold zM " Close all zR " Open all

" Persistence mkview loadview

" Custom fold text set foldtext=CustomFoldText() ```

With proper configuration, Vim folding becomes a powerful navigation tool for managing large files.