- Mastering Vim
- Ruslan Osipov
- 292字
- 2025-04-04 15:36:06
Folding Python code
Since we're working with Python code throughout this book, let's play with some folds in our code. First, you'll need to change a setting called foldmethod to indent in your .vimrc file:
set foldmethod=indent
This will tell Vim to fold based on indentation (there are multiple ways to work with folds; see the following section, Types of folds, for more information).
Open animal_farm.py and you will see portions of our file folded away:

Navigate your cursor to one of the folded lines. Hitting zo will open the current fold:
Whenever your cursor is over a potential fold (an indented chunk of code in this example), zc will close the fold.
You can also use za to toggle folds (open closed folds and close open folds).
You can open and close all folds in the file at the same time using zR and zM, respectively.