Vim Folds Make Your Code More Readable

NeuralNine · Beginner ·💻 AI-Assisted Coding ·2y ago

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 Visualizing Stock Data With Candlestick Charts in Python
Visualizing Stock Data With Candlestick Charts in Python
NeuralNine
2 Python Beginner Tutorial #1 - Installation and First Program
Python Beginner Tutorial #1 - Installation and First Program
NeuralNine
3 Python Beginner Tutorial #2 - Variables and Data Types
Python Beginner Tutorial #2 - Variables and Data Types
NeuralNine
4 Python Beginner Tutorial #3 - Operators and User Input
Python Beginner Tutorial #3 - Operators and User Input
NeuralNine
5 Python Beginner Tutorial #4 - If Statements and Conditions
Python Beginner Tutorial #4 - If Statements and Conditions
NeuralNine
6 Python Beginner Tutorial #5 - Loops
Python Beginner Tutorial #5 - Loops
NeuralNine
7 Python Beginner Tutorial #6 - Sequences and Collections
Python Beginner Tutorial #6 - Sequences and Collections
NeuralNine
8 Python Beginner Tutorial #7 - Functions
Python Beginner Tutorial #7 - Functions
NeuralNine
9 Python Beginner Tutorial #8 - Exception Handling
Python Beginner Tutorial #8 - Exception Handling
NeuralNine
10 Python Beginner Tutorial #9 - File Operations
Python Beginner Tutorial #9 - File Operations
NeuralNine
11 Python Beginner Tutorial #10 - String Functions
Python Beginner Tutorial #10 - String Functions
NeuralNine
12 Python Intermediate Tutorial #1 - Classes and Objects
Python Intermediate Tutorial #1 - Classes and Objects
NeuralNine
13 Python Intermediate Tutorial #2 - Inheritance
Python Intermediate Tutorial #2 - Inheritance
NeuralNine
14 Python Intermediate Tutorial #3 - Multithreading
Python Intermediate Tutorial #3 - Multithreading
NeuralNine
15 Python Intermediate Tutorial #4 - Synchronizing Threads
Python Intermediate Tutorial #4 - Synchronizing Threads
NeuralNine
16 Python Intermediate Tutorial #5 - Events and Daemon Threads
Python Intermediate Tutorial #5 - Events and Daemon Threads
NeuralNine
17 Python Intermediate Tutorial #6 - Queues
Python Intermediate Tutorial #6 - Queues
NeuralNine
18 Python Intermediate Tutorial #7 - Sockets and Network Programming
Python Intermediate Tutorial #7 - Sockets and Network Programming
NeuralNine
19 Python Intermediate Tutorial #8 - Database Programming
Python Intermediate Tutorial #8 - Database Programming
NeuralNine
20 Python Intermediate Tutorial #9 - Recursion
Python Intermediate Tutorial #9 - Recursion
NeuralNine
21 Python Intermediate Tutorial #10 - XML Processing
Python Intermediate Tutorial #10 - XML Processing
NeuralNine
22 Python Intermediate Tutorial #11 - Logging
Python Intermediate Tutorial #11 - Logging
NeuralNine
23 Python Data Science Tutorial #1 - Anaconda and PyCharm Setup
Python Data Science Tutorial #1 - Anaconda and PyCharm Setup
NeuralNine
24 Python Data Science Tutorial #2 - NumPy Arrays
Python Data Science Tutorial #2 - NumPy Arrays
NeuralNine
25 Python Data Science Tutorial #3 - Numpy Functions
Python Data Science Tutorial #3 - Numpy Functions
NeuralNine
26 Python Data Science Tutorial #4 - Plotting Functions With Matplotlib
Python Data Science Tutorial #4 - Plotting Functions With Matplotlib
NeuralNine
27 Python Data Science Tutorial #5 - Subplots and Multiple Windows
Python Data Science Tutorial #5 - Subplots and Multiple Windows
NeuralNine
28 Python Data Science Tutorial #6 - Matplotlib Styling
Python Data Science Tutorial #6 - Matplotlib Styling
NeuralNine
29 Python Data Science Tutorial #7 - Bar Charts with Matplotlib
Python Data Science Tutorial #7 - Bar Charts with Matplotlib
NeuralNine
30 Python Data Science Tutorial #8 - Pie Charts with Matplotlib
Python Data Science Tutorial #8 - Pie Charts with Matplotlib
NeuralNine
31 Python Data Science Tutorial #9 - Plotting Histograms with Matplotlib
Python Data Science Tutorial #9 - Plotting Histograms with Matplotlib
NeuralNine
32 Python Data Science Tutorial #10 - Scatter Plots with Matplotlib
Python Data Science Tutorial #10 - Scatter Plots with Matplotlib
NeuralNine
33 Python Data Science Tutorial #11 - 3D Plotting with Matplotlib
Python Data Science Tutorial #11 - 3D Plotting with Matplotlib
NeuralNine
34 Python Data Science Tutorial #12 - Pandas Series
Python Data Science Tutorial #12 - Pandas Series
NeuralNine
35 Python Data Science Tutorial #13 - Pandas Data Frames
Python Data Science Tutorial #13 - Pandas Data Frames
NeuralNine
36 Python Data Science Tutorial #14 - Pandas Statistics
Python Data Science Tutorial #14 - Pandas Statistics
NeuralNine
37 Python Data Science Tutorial #15 - Pandas Sorting and Functions
Python Data Science Tutorial #15 - Pandas Sorting and Functions
NeuralNine
38 Python Data Science Tutorial #16 - Pandas Merging Data Frames
Python Data Science Tutorial #16 - Pandas Merging Data Frames
NeuralNine
39 Python Data Science Tutorial #17 - Pandas Queries
Python Data Science Tutorial #17 - Pandas Queries
NeuralNine
40 Python Machine Learning Tutorial #1 - What is Machine Learning?
Python Machine Learning Tutorial #1 - What is Machine Learning?
NeuralNine
41 Python Machine Learning Tutorial #2 - Linear Regression
Python Machine Learning Tutorial #2 - Linear Regression
NeuralNine
42 Python Machine Learning Tutorial #3 - K-Nearest Neighbors Classification
Python Machine Learning Tutorial #3 - K-Nearest Neighbors Classification
NeuralNine
43 Python Machine Learning #4 - Support Vector Machines
Python Machine Learning #4 - Support Vector Machines
NeuralNine
44 Python Machine Learning Tutorial #5 - Decision Trees and Random Forest Classification
Python Machine Learning Tutorial #5 - Decision Trees and Random Forest Classification
NeuralNine
45 Python Machine Learning Tutorial #6 - K-Means Clustering
Python Machine Learning Tutorial #6 - K-Means Clustering
NeuralNine
46 Python Machine Learning Tutorial #7 - Neural Networks
Python Machine Learning Tutorial #7 - Neural Networks
NeuralNine
47 Python Machine Learning Tutorial #8 - Handwritten Digit Recognition with Tensorflow
Python Machine Learning Tutorial #8 - Handwritten Digit Recognition with Tensorflow
NeuralNine
48 Generating Poetic Texts with Recurrent Neural Networks in Python
Generating Poetic Texts with Recurrent Neural Networks in Python
NeuralNine
49 Stock Portfolio Visualization with Matplotlib in Python
Stock Portfolio Visualization with Matplotlib in Python
NeuralNine
50 Analyzing Coronavirus with Python (COVID-19)
Analyzing Coronavirus with Python (COVID-19)
NeuralNine
51 Making Text Images Readable Again with Python and OpenCV
Making Text Images Readable Again with Python and OpenCV
NeuralNine
52 Neural Networks Simply Explained (Theory)
Neural Networks Simply Explained (Theory)
NeuralNine
53 Motion Filtering with OpenCV in Python
Motion Filtering with OpenCV in Python
NeuralNine
54 Top 5 Programming Languages To Learn in 2020
Top 5 Programming Languages To Learn in 2020
NeuralNine
55 Simple TCP Chat Room in Python
Simple TCP Chat Room in Python
NeuralNine
56 Image Classification with Neural Networks in Python
Image Classification with Neural Networks in Python
NeuralNine
57 Edge Detection with OpenCV in Python
Edge Detection with OpenCV in Python
NeuralNine
58 S&P 500 Web Scraping with Python
S&P 500 Web Scraping with Python
NeuralNine
59 Simple Sentiment Text Analysis in Python
Simple Sentiment Text Analysis in Python
NeuralNine
60 Introduction - Algorithms & Data Structures #1
Introduction - Algorithms & Data Structures #1
NeuralNine

Learn how to use Vim folds to improve code readability and organization, making your coding workflow more efficient. This tutorial by NeuralNine covers the basics of Vim folds and how to apply them to your coding projects. By mastering Vim folds, you can enhance your productivity and write more readable code.

Key Takeaways
  1. Install Vim and configure it for folding
  2. Understand the different fold commands in Vim
  3. Create and manage folds in your code
  4. Use Vim folds to organize and navigate your codebase
  5. Customize Vim folds to fit your coding style
💡 Vim folds can significantly improve code readability by allowing you to collapse and expand sections of code, making it easier to navigate and understand complex codebases.

Related Reads

📰
Cursor Pricing 2026: Free vs Pro vs Ultra — Which Plan?
Learn how to choose the right Cursor plan for your coding needs and budget, and discover how this Agentic AI coding tool can boost your productivity
Dev.to AI
📰
enable Consistent AI Coding with Persistent Context Layers
Learn how persistent context layers can improve AI coding consistency and reliability
Dev.to AI
📰
LeetCode Isn’t Dead. Your Interview Prep Strategy Is.
Update your interview prep strategy to focus on practical skills and real-world problem-solving, as LeetCode-style interviews are evolving
Medium · Programming
📰
Build a UGC video moderation pipeline with FFmpeg + NudeNet
Learn to build a UGC video moderation pipeline using FFmpeg and NudeNet to ensure safe and respectful user-generated content
Dev.to · Mason K
Up next
Copilot Cowork: Setup, Skills, Plugins & Pricing
Matt Tutorials
Watch →