May 24, 2021 Vim
If you've never used code folding in Vim, you don't know what you're missing. R
ead
:help usr_28
time using it in your daily work.
Once you've reached the point where you remember your finger, you can move on to this chapter.
Vim supports six different types of folding that determine how to collapse your text.
You manually create the fold and the fold will be stored in memory by Vim. When you turn off Vim, they'll disappear, and the next time you edit the file, you'll have to recreate it.
This is handy when you combine it with some custom mapping to create collapses. In this book, we won't do it, but it will help when you want to.
Vim collapses your code based on specific character combinations.
These characters are usually placed in
// {{{
as // , but in some languages you can use the language's own syntax instead, such as
{
. . and
}
.
Just for your editor, spliting your code with comments looks a bit ugly, but the benefit is that you can customize specific folds. This type is a great choice if you want to organize a large file in a particular way.
Use this particular type of collapse when using diff files. We won't discuss it because Vim will automatically use it.
This allows you to decide where to collapse using a custom Vimscript. I t's the most powerful way, but it also requires the heaviest work. We'll talk about it in the next chapter.
Vim uses your code to shrink in and fold. Code of the same indentation level is folded into a piece, and empty lines are folded together into the surrounding lines.
This is the easiest way because your code has been indented; This will be the first way we can fold The Potion code.
Let's take a look at the Potion instance code again:
factorial = (n):
total = 1
n to 1 (i):
total *= i.
total.
10 times (i):
i string print
'! is: ' print
factorial (i) string print
"\n" print.
The function body and the loop body are indented. This means that we can achieve some basic indentation without much effort.
Before we begin,
total *= i
so that we have a multi-line internal block for testing. Y
ou'll learn why we need to do this while you're doing the exercises, but trust me for the time being.
Now the file looks like this:
factorial = (n):
total = 1
n to 1 (i):
# Multiply the running total.
total *= i.
total.
10 times (i):
i string print
'! is: ' print
factorial (i) string print
"\n" print.
Create a
ftplugin
folder under the repository of your Potion plug-in, and then create a
potion
Finally, create
potion
file in the
folding.vim
folder.
Don't forget that every time Vim sets a
filetype
potion
it executes the code in this file.
(because it's in a
potion
It's obviously a good idea to put all the folding-related code in the same file, which can help us maintain the many features of our plug-ins.
Add the following line to this file:
setlocal foldmethod=indent
Turn off Vim and turn
factoria.pn
Try
zR
zM
and za.
za
A line of Vimscript code can bring some useful folding! That's so cool!
You may notice that the lines inside the inner loop of the
factorial
function cannot be collapsed, even though they shrink into .
Why is this happening?
In fact, when
indent
Vim ignores lines that
#
the character .
This is useful when editing C files (at which point
#
a precompiled instruction), but it doesn't make much sense when editing other files.
ftplugin/potion/folding.vim
to fix the problem:
setlocal foldmethod=indent
setlocal foldignore=
Close and reopen the
factorial.pn
and now the inner block collapses normally.
Read
:help foldmethod
.
Read
:help fold-manual
.
Read
:help fold-marker
and
:help foldmarker
.
Read
:help fold-indent
.
Read
:help fdl
and
:help foldlevelstart
.
Read
:help foldminlines
.
Read
:help foldignore
.