Quickly fold and unfold code by indent.
Here's a blog post on the creation of this plugin, if you're into that sort of thing! This is my first Vim plugin, which means (a) it might be a bit rough around the edges/corner cases, and (b) there's plenty to improve via pull request!
Should work with Pathogen, vim-plug, Vundle, etc. like other plugins.
I use vim-plug, so installation of vim-zipper looks like this in my vimrc:
Plug 'sts10/vim-zipper'
Then don't forget to run :PlugInstall
Note that, for me at least, this plugin works best with foldmethod=indent
. Thus it's probably best if you just go ahead and set foldmethod=indent
in your vimrc (though the plugin does this for you).
If you don't have a foldmethod
set, or it is set to manual
, zipper runs set foldenable
and sets foldmethod
to indent
. More on that below.
Then it gives you two basic commands to map: ZipOpen and ZipClosed. ZipClosed is basically just zc
-- nothing special as of this version. But ZipOpen is a bit smarter-- it finds the next or previous closed fold and opens that fold. In tandem these commands allow you to quickly close and open folds without moving your cursor.
Vim comes with folding built in by default-- 6 methods of it as of 7.4-- with a variety of commands to manipulate them. The ideal fold setup for you may already be built in to Vim, and if that's the case you wouldn't need vim-zipper. Check out :help folding
and :help fold-commands
to learn more. Though for example, zj
and zk
seem particularly useful, though they don't perform the exact same functions as this plugin. But given zj
, zk
, the other fold-commands, and this Reddit comment as clues, you might be able to build your own remapping that works better for you than what this plugin supplies.
If you don't have |
(<bar>
) or \
(<Bslash
) mapped already, here is what vim-zipper will map for you:
nmap <bar> <Plug>ZipClosed
vmap <bar> <Plug>ZipClosedVisual
nmap <Bslash> <Plug>ZipOpenNext
nmap <C-Bslash> <Plug>ZipOpenPrev
vmap <Bslash> <Plug>ZipOpenVisual
If you have |
or \
already mapped (for example, if you haven't mapped your <Leader>
to something other than \
), vim-zipper will not override those mapping. Instead you'll have to map vim-zipper's commands yourself or map your <Leader>
to something other than \
and restart Vim.
Personally I map Leader to Space with the following line in my vimrc: let mapleader = "\<Space>"
.
I chose \
as ZipOpen and |
as ZipClose because they look like a zipper opening and closing.
As mentioned above, this current version of zipper checks if your foldmethod
is set to manual
and if it is, it runs set foldenable
and set foldmethod=indent
. Setting foldmethod
to anything other than manual
or ''
in your vimrc should override this.
It also looks like you can override this on a filetype basis in your vimrc
with an autocmd, such as:
autocmd FileType python setlocal foldmethod=syntax
autocmd FileType vim setlocal foldmethod=marker
A good amount of this plugin was adapted from this Stack Overflow answer from user ib.. Also thanks Tim Pope's Commentary plugin, where I copped some plugin tricks.
Pull requests encouraged! Also feel free to hit me up on Twitter.