Vim Folds Make Your Code More Readable
Key Takeaways
Vim folds for code readability, NeuralNine tutorial
Full Transcript
what is going on guys welcome back in this video today we're going to learn how to work with folding and folds in Vim so let us get right into [Music] it all right so we're going to learn about folds in this video today and folds are essentially a Vim feature that allows us to hide and show certain sections of the code of the text that we're editing to collapse and expand them to open and close them to get a better overview of the thing that we're working with so a text file a code file whatever and we're going to get right into it with an example I'm going to open up a terminal here on my desktop and I'm going to use neovim to create a new file let's call it test.txt now this is a core Vim feature so you can use VI you can use Vim you can use neovim it doesn't matter you don't need to install anything uh other than Vim itself if it's not installed on your system and what we can do now is we can just write a couple of things here so hello world let me just zoom in a little bit so you guys can see that hello world this is a line this is also a line so you can just write some random text here like this and the idea now is that I might want to have certain sections that I want to collapse and expand so maybe I want to say that all this text here that I cannot understand is one section and I want to now collapse that section into a fold so I want to fold it up and what I can do for that is I can just just press ZF so the Z key and the F key ZF folds this together into a fold and now I can open this fold and I can close this fold very simply with zo for open and zc for close so zo o for open z c for close and this works also if I'm in the middle of it so here zc closes and Z opens that's the basic idea of a fold and I can use this now to of course um do this on multiple uh in multiple positions here and this can lead to having multiple folds and just a nice overview of the structure of the document so I can close this then I can say maybe this is another fold so again ZF to close this and you can see four lines seven lines and in between of course I can also have text that does not belong to a fold and if I just try to close it here doesn't work it tells me here no fold found if I try to do zc um and what we can also do is we can say Za and Za is basically just a toggling so it's like zc and zo but you just have one key bind for that so if I say Za here and if I say Za again it just opens and closes it all the time that's the basic idea so this is the general idea of the fold um now first of all let's talk about the methods that we use for folding here in this case right now we're using a manual fold so we're folding all the stuff manually we're defining the faults manually and this is the default method so you can just go ahead and say colon set fault method and you will see that the default method is manual um what you can do now is you can also choose a different fold method and for this I'm going to show you an example that I prepared here um I have an examples directory and if I go into it you will see I have a python file and a C file and if I open of the Python file you can see I have some code in here just some random stuff and what I can do now is I can set default methods to a specific fault method that's based on indentation so instead of saying set fold method equals manual I'm going to say set fold method equals indent and what this does is you can see that my python functions here which are based on indentation are collapsed now so there are collapse into fults and you can see that we even have fults inside of faults so I can close this I can close this I can open this I can open this I can open this and so on by the way on my system it also works with a space so with a white space you can open the folds you cannot close them but yeah this is also something that you can do um and the interesting thing here is I can close this one and close this one and if I want to open up all of them so all the folds and all the folds within the folds I can just say Z and capital O to open up also the faults inside of the fault so again if I close all of this with Z Capital C which also closes uh the the lower levels if I just say z o so Z lowercase o uh oh in this case it didn't close it let me close this again if I just say zo this will close um this will open only the one that I'm focusing on if I now close this again and I say Z capital O it also opens the child folds that's the basic idea here um and I think that Z Capital C should actually close everything yeah okay so I think you need to do it one level below so if I'm down here and I say Z Capital C it basically closes all of them so if I just open it in a normal way you can see that I have to open up both levels again um what I can also do is I can use Z capital M so Z capital M to close all the folds and I can use Z capital r to open all the folds so I can say Z capital r everything's open Z capital M everything's closed again so this is also a nice thing that we can do um so let me just as a summary here write it down so zc is for closing Z Capital C is for closing all closing all then we have zo which is for opening zo with a capital o which is for opening all then we have Za which is for toggling and then we also have this is one I didn't show we have Z Capital a uh which toggles the whole thing so this is also toggling all and then we have uh [Music] ZM and ZR where m is closing closing all maybe all with capital letters and opening all with capital letters meaning across the whole document so this is the indentation example now I also have a file called uh syntax C and the reason I called it that is because the method here fault method has to be set to syntax and this basically considers all these curly brackets here if I do that you can see I can expand this I can expand this again I can expand this again again and so on and again of course I can say Z Capital C to close everything and uh yeah that's the basic idea here this is the different fault method here syntax now one thing that you will notice and this is important now um if I go into my test file so if I go back to the desktop and I go into my test file you will see something which um is a problem maybe so if I go ahead now I Define this fold here uh I can open it I can close it and whatnot if I now go and write and quit and I go back into the file the fold is no longer here and I can also not close it I cannot open it nothing is here the fold is gone so the fold is something that is not safed in the file by default so this is just a text file of course it doesn't have any information about the fold so what I need to do to actually save the fold is I need to use a command in Vim that stores the fold information somewhere somewhere else so for example let's go ahead and make a fold like this maybe also make a fold like this and now what I want to do is I want to do the following command MK view MK view for make view this is going to store everything now I can leave I can go back into the document it's not here yet but I can say colon load to load the fold that I had before so again here we have the fults now one important thing which I didn't mention up until this point uh you can also delete faults so we cannot just open them you can also delete them so if you don't want to have this fault all you have to do is you have to say z d so z d like this and then the fold is no longer there and I can do the same thing here and then I can do MK view again to save it and then if I load this again and I try to load I don't have any fults as you can see so this is basically basically what you can do with Vim folds it's just a nice way to structure your code your text into different sections that you can collapse and expand and it is done in a very convenient way with key combinations so that's it for today's video I hope you enjoyed it and hope you learned something if so let me know by hitting a like button and leaving a comment in the comment section down below and of course don't forget to subscribe to this Channel and hit the notification Bell to not miss a single future video for free other than that thank you much for watching see you in the next video and bye that
Original Description
Today we learn about Vim folds and how you can use them to get a better overview of your code.
◾◾◾◾◾◾◾◾◾◾◾◾◾◾◾◾◾
📚 Programming Books & Merch 📚
🐍 The Python Bible Book: https://www.neuralnine.com/books/
💻 The Algorithm Bible Book: https://www.neuralnine.com/books/
👕 Programming Merch: https://www.neuralnine.com/shop
💼 Services 💼
💻 Freelancing & Tutoring: https://www.neuralnine.com/services
🌐 Social Media & Contact 🌐
📱 Website: https://www.neuralnine.com/
📷 Instagram: https://www.instagram.com/neuralnine
🐦 Twitter: https://twitter.com/neuralnine
🤵 LinkedIn: https://www.linkedin.com/company/neuralnine/
📁 GitHub: https://github.com/NeuralNine
🎙 Discord: https://discord.gg/JU4xr8U3dm
Watch on YouTube ↗
(saves to browser)
Sign in to unlock AI tutor explanation · ⚡30
Playlist
Uploads from NeuralNine · NeuralNine · 0 of 60
← Previous
Next →
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
Visualizing Stock Data With Candlestick Charts in Python
NeuralNine
Python Beginner Tutorial #1 - Installation and First Program
NeuralNine
Python Beginner Tutorial #2 - Variables and Data Types
NeuralNine
Python Beginner Tutorial #3 - Operators and User Input
NeuralNine
Python Beginner Tutorial #4 - If Statements and Conditions
NeuralNine
Python Beginner Tutorial #5 - Loops
NeuralNine
Python Beginner Tutorial #6 - Sequences and Collections
NeuralNine
Python Beginner Tutorial #7 - Functions
NeuralNine
Python Beginner Tutorial #8 - Exception Handling
NeuralNine
Python Beginner Tutorial #9 - File Operations
NeuralNine
Python Beginner Tutorial #10 - String Functions
NeuralNine
Python Intermediate Tutorial #1 - Classes and Objects
NeuralNine
Python Intermediate Tutorial #2 - Inheritance
NeuralNine
Python Intermediate Tutorial #3 - Multithreading
NeuralNine
Python Intermediate Tutorial #4 - Synchronizing Threads
NeuralNine
Python Intermediate Tutorial #5 - Events and Daemon Threads
NeuralNine
Python Intermediate Tutorial #6 - Queues
NeuralNine
Python Intermediate Tutorial #7 - Sockets and Network Programming
NeuralNine
Python Intermediate Tutorial #8 - Database Programming
NeuralNine
Python Intermediate Tutorial #9 - Recursion
NeuralNine
Python Intermediate Tutorial #10 - XML Processing
NeuralNine
Python Intermediate Tutorial #11 - Logging
NeuralNine
Python Data Science Tutorial #1 - Anaconda and PyCharm Setup
NeuralNine
Python Data Science Tutorial #2 - NumPy Arrays
NeuralNine
Python Data Science Tutorial #3 - Numpy Functions
NeuralNine
Python Data Science Tutorial #4 - Plotting Functions With Matplotlib
NeuralNine
Python Data Science Tutorial #5 - Subplots and Multiple Windows
NeuralNine
Python Data Science Tutorial #6 - Matplotlib Styling
NeuralNine
Python Data Science Tutorial #7 - Bar Charts with Matplotlib
NeuralNine
Python Data Science Tutorial #8 - Pie Charts with Matplotlib
NeuralNine
Python Data Science Tutorial #9 - Plotting Histograms with Matplotlib
NeuralNine
Python Data Science Tutorial #10 - Scatter Plots with Matplotlib
NeuralNine
Python Data Science Tutorial #11 - 3D Plotting with Matplotlib
NeuralNine
Python Data Science Tutorial #12 - Pandas Series
NeuralNine
Python Data Science Tutorial #13 - Pandas Data Frames
NeuralNine
Python Data Science Tutorial #14 - Pandas Statistics
NeuralNine
Python Data Science Tutorial #15 - Pandas Sorting and Functions
NeuralNine
Python Data Science Tutorial #16 - Pandas Merging Data Frames
NeuralNine
Python Data Science Tutorial #17 - Pandas Queries
NeuralNine
Python Machine Learning Tutorial #1 - What is Machine Learning?
NeuralNine
Python Machine Learning Tutorial #2 - Linear Regression
NeuralNine
Python Machine Learning Tutorial #3 - K-Nearest Neighbors Classification
NeuralNine
Python Machine Learning #4 - Support Vector Machines
NeuralNine
Python Machine Learning Tutorial #5 - Decision Trees and Random Forest Classification
NeuralNine
Python Machine Learning Tutorial #6 - K-Means Clustering
NeuralNine
Python Machine Learning Tutorial #7 - Neural Networks
NeuralNine
Python Machine Learning Tutorial #8 - Handwritten Digit Recognition with Tensorflow
NeuralNine
Generating Poetic Texts with Recurrent Neural Networks in Python
NeuralNine
Stock Portfolio Visualization with Matplotlib in Python
NeuralNine
Analyzing Coronavirus with Python (COVID-19)
NeuralNine
Making Text Images Readable Again with Python and OpenCV
NeuralNine
Neural Networks Simply Explained (Theory)
NeuralNine
Motion Filtering with OpenCV in Python
NeuralNine
Top 5 Programming Languages To Learn in 2020
NeuralNine
Simple TCP Chat Room in Python
NeuralNine
Image Classification with Neural Networks in Python
NeuralNine
Edge Detection with OpenCV in Python
NeuralNine
S&P 500 Web Scraping with Python
NeuralNine
Simple Sentiment Text Analysis in Python
NeuralNine
Introduction - Algorithms & Data Structures #1
NeuralNine
More on: AI Pair Programming
View skill →Related Reads
🎓
Tutor Explanation
DeepCamp AI