Let’s begin by generating a new html type of file. You can also create any text file as “zf” can be applied to any type of file. The purpose is to just fold or unfold the piece of code or text. Use the “touch” query in the shell to create a new html file and add some html codes or tags within it.
Use the “vim” command to open the html file within the Vim editor.
Set Manual Fold
The html file has been opened within Vim in its Normal Mode. The “Esc” key from the keyboard can be utilized to get the normal mode from any other mode, i.e., visual mode, insert mode, etc. Use the below command on the command area followed by “:”.
Fold with ZF
Open the Visual mode by pressing “v” while working on the Normal Mode. Use “Ctrl+v” to open the Visual Block mode. Within the block mode, place your cursor to the position from where you want to fold the text or code. Start pressing “j” to cover the number of lines that need to be folded. Also, press the next key to cover the lines fully to fold them up. Now, press small “zf” to fold the highlighted lines from the code below within the Visual Block mode.
The output would be like something shown in the image below. You can see that lines 2 and 3 have been folded using the “zf” command. There is another way to use the zf command by declaring line numbers and the character “j” to fold the total lines of your choice. Let say you want to fold only 2 lines beginning from the 2nd line. Then you place the cursor on the 1st character of line 1 and press “zf2j”. The output below shows the fold of two lines.
Another way to fold the piece of code lines from any file within Vim is using the range fold command. Within this command, you have to mention the line number and the negative number to deduct the number of lines in the upward direction. Let’s say we want to fold a single line in the upward direction from line number 3. Then, we have to mention “-1” as several lines to be folded and number “3” as a line number to be folded as well with the “fold” keyword. The way to use the command has been revealed in the screenshot attached underneath.
After adding the command and pressing the Enter key, we have got the below output of folding two lines, including line 3 and its preceded line, i.e., line 2.
Unfold All Using zR
The zR command can be used to unfold all the folds that are being found in your file. Let’s fold all the 6 lines of the new.html file using the Vim “zf” command. So, put your cursor on character 1 of line 1 while in the Visual mode and press “j”. The area will be highlighted. Press “zf” to fold it. The first 2 lines will be folded.
Put the cursor on line 3 and press “j”. It will select another piece of code. Press “zf” to fold this chunk, and your output will be as below.
Now, replicate the similar process with the remaining lines of codes to get them folded. As all the lines of code are folded, you have to use the “zR” command to unfold all of them in a single step. So, within the Visual mode, press “zf” and all the folds will be unfolded as below.
Unfold Using zo
Let say you have folded all chunks of 8 code within different folds, as shown below. Make sure you have folded every 2 lines one after another and not at once. Now, we want to unfold the last folded chunk of code within the Visual mode of Vim. So, put your cursor on the last chunk, i.e., the “+” sign, as revealed in the image below. Now, press the “zo” command from your keyboard.
Now, you have been shown with the last folded chunk of code as shown in the image, i.e., the 7th line of the new.html file has been unfolded. The rest of the folds remain unchanged.
Close All Folds Using zM
You can also fold again all the already unfolded or opened folds within the Visual mode of Vim. To do that, you have to use the “zM” command on the visual mode after opening some folds. Your output will be something like below, showing a single fold.
Unfold All using zO
If you want to just open or unfold all the folds of code that are being created one by one, use the command “zO” to do that. Place the cursor on the “+” sign all press “zO”. It will unfold all the folds that are within it.
Conclusion
This tutorial covers all the basic commands to fold the piece of code of some file within the Vim editor. We have discussed the commands to fold the piece of code with zf and also unfold using some of the “z” commands. We hope this article will be quite helpful for you for “zf”.