JavaScript Folding on Vim

Set up this native feature to improve your code reading and navigation.

image

Folding is the code editor’s ability to let you hide/show some parts of the text, allowing you to focus on something specific and navigate thru your code structure with more ease.

Vim folding can be set up to group text (fold) based on different concepts:

image

We’ll be specific and set folding to work on JavaScript files using the fold-syntax option, and applying some configurations.

When reading a JavaScript file, vim will automatically fold by syntax, grouping functions and blocks of code, and it will fold recursively (nested folds) too.


How to Set Up:

Open your .vimrc file and add the following lines:

image

"-- FOLDING --  
set foldmethod=syntax "syntax highlighting items specify folds  
set foldcolumn=1 "defines 1 col at window left, to indicate folding  
let javaScript_fold=1 "activate folding by JS syntax  
set foldlevelstart=99 "start file with all folds opened

With the above configuration you’re:

  • Enabling folding based on JavaScript syntax.
  • Setting up fold indication on the left side of your window, it helps you identify fold’s status and level.
  • Telling vim to keep all folds opened when you start the file.

(To know more about the options for each one of these settings, type :h <setting name>).


How to use it:

The actions are based on the command z associated with the action option:

  • zc — close the fold (where your cursor is positioned)
  • zM —close all folds on current buffer
  • zo — open the fold (where your cursor is positioned)
  • zR — open all folds on current buffer
  • zj — cursor is moved to next fold
  • zk — cursor is moved to previous fold

image

On the left side of the window, you can see whenver a fold is opened/closed and it’s folding level.

That’s it! You’re ready to dive on a big JavaScript codebase and navigate with ease!


  • Vim Documentation ( :help )
  • This tip was adapted from content found on “Mastering Vim Quickly” Book (and screencasts) by Jovica Ilic. It’s an awesome book, make sure you subscribe to the newsletter to get free Vim tips and book excerpts on your email.

(👆 This is an affiliate link. If you got any value from this post and are also interested in buying the MVQ book/screencasts, please consider visiting the affiliate link and you’ll be gifting me a coffee ☕ or a beer 🍺.)


Footnotes:

  • Follow me on Twitter to get more posts like this and other quick tips in your feed.
  • alldrops.info is the TRUE and FOREVER FREE home for the previously used medium.com/vim-drops publication. At alldrops.info you’ll always find all posts without Medium.com restrictions.
  • If you have any doubts or tips about this article, I’d appreciate knowing and discussing it via email.
  • Do you have any other Vim tips? Would you like to publish that in this blog? Please send an email to vim drops.
  • As English is not my native language, I apologize for the errors. Corrections are welcome.
  • Contact: vim [@] alldrops [.] info.

Read more on vim drops: