`
If you find these tutorials useful, Please consider making a donation.

TheFrugalComputerLinuxLogo.png
TheFrugalComputerGuy.png
Vim 9
Folding Text







Video 26 - Folding Text

In this video, we look at folding text. Folding text is just a way of hiding text when you don't need to see it.

To see which foldmethod is being used you can type :set foldmethod or the shortcut :set fdm

We using the manual foldmethod on this video.

In this video, I create a fold by selecting text using the upper case "V" to put us in visual line mode, using j or k, I select the text, then I create the fold by pressing lower case "z" then lower case "f".

To open a fold you can type lower case "z" then lower case "o"

You can close a fold by typing lower case "z" lower case "c" from any line in the folded text.

The folds will need to be saved in the directory .vim/views, and that directory may need to be created.
Placing these lines in your .vimrc file will save the folds in the .vim/views directory:
autocmd BufWinLeave *.* mkview
autocmd BufWinEnter *.* silent loadview

Folds can be deleted pressing lower case "z" then lower case "d"





 






Video 27 - Folding Text pt2 Creating Folds

We look at more ways to create folds. We see that:

zf10j - will fold the current line and the 10 lines below it.
zk10j - will fold the current line and the 10 lines above it

:148,158fold - will fold lines 148 to 158
:148,158 fold - shows you can add a space (but you don't need to)
:148,158 fo - fo is a shortcut for fold

:,+10 fold - will fold from where the line the cursor is on to 10 lines below it
:,-10 fold - will fold from where the line the cursor is on to 10 lines above it

zfa{ - will fold the text between the two curly bracess {}
va{zf - va{ will visually select the text between curly braces {} and zf will fold the selection





 






Video 28 - Folding Text pt3 - Open Close all Folds

Summery of what we see in this video:

zn = Open all the folds in the document
zN = Set all folds as they were before

:86,136 foldopen - open all folds in this range
:86,136 foldclose - close all folds in this range

:% foldopen - opens all the folds in the document
:% foldclose - close all the folds in the document

zj - Jump to the nex fold in this docuemnt
zk - jump to the prevous fold in this document

[z - jump to top line of this fold
]z - jump to the bottom line of this fold

zE - Eliminate all folds in the entire document





 






Video 29 - Folding Text pt4 - Nested Folds

We nest a fold, inside fold, inside a fold.

In this video we use:

zo - open a single fold
zc - close a single fold

zO - open the current fold and all the folds nested inside this fold
zC - close the current fold and all the folds this fold is nested inside of

zA - toggle between zO and zC





 






Video 30 - Folding Text pt5 - foldmethod=indent

In this video, we reformat our file by removing our current indentation, then auto-format it as a perl script

We then change the foldmethod to indent and see:

zo - opens a single fold
zc - closes a single fold
zO - opens all folds in a single nested fold
zC - closes all folds in a single nested fold

zr - reduce all folds one level (same as zo but for all folds)
zR - reduce all folds all the way (same as zO for all folds)
zm - fold up (minus) a nested fold level for all folds (same as zc for all folds)
zM - fold up (minus) all nested folds levels for all folds (same a zC for all folds)

Foldmethods are:
Manual - folds must be defined by entering commands (like zf)
indent - creates a fold for each indent
Syntax - create a fold by the syntax of a programming language
Expr - folds are created by a user-defined expression (like a regex / regular expression)
Marker - Create folds for a start and end characters you define
Diff - used to fold unchanged text when viewing differences (automatically set in diff mode)