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â.