├── .gitignore ├── LICENSE ├── README.mdown ├── doc ├── tags └── zipper.txt ├── plugin └── zipper.vim └── vim-zipper-gif.gif /.gitignore: -------------------------------------------------------------------------------- 1 | test_html.html 2 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Copyright (c) 2016-2024 Sam Schlinkert 2 | 3 | Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: 4 | 5 | The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. 6 | 7 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 8 | -------------------------------------------------------------------------------- /README.mdown: -------------------------------------------------------------------------------- 1 | # vim-zipper 2 | 3 | Quickly fold and unfold code by indent. 4 | 5 | ![vim-zipper-gif](vim-zipper-gif.gif) 6 | 7 | Here's a [blog post](http://sts10.github.io/blog/2016/03/20/vim-zipper-my-first-vim-plugin/) 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! 8 | 9 | ## Installation 10 | 11 | Should work with [Pathogen](https://github.com/tpope/vim-pathogen), [vim-plug](https://github.com/junegunn/vim-plug), [Vundle](https://github.com/VundleVim/Vundle.vim), etc. like other plugins. 12 | 13 | I use [vim-plug](https://github.com/junegunn/vim-plug), so installation of vim-zipper looks like this in my [vimrc](https://github.com/sts10/terminal_and_vim_settings/blob/master/vimrc): 14 | 15 | ``` 16 | Plug 'sts10/vim-zipper' 17 | ``` 18 | 19 | Then don't forget to run `:PlugInstall` 20 | 21 | 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). 22 | 23 | ## What This Plugin Does 24 | 25 | 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. 26 | 27 | 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. 28 | 29 | ### Do You Need This Plugin? 30 | 31 | 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](https://www.reddit.com/r/vim/comments/4jm160/help_fix_this_fold_mapping/d383k1s) as clues, you might be able to build your own remapping that works better for you than what this plugin supplies. 32 | 33 | ## Vim-Zipper's Default Mappings 34 | 35 | If you don't have `|` (``) or `\` (` ZipClosed 39 | vmap ZipClosedVisual 40 | 41 | nmap ZipOpenNext 42 | nmap ZipOpenPrev 43 | vmap ZipOpenVisual 44 | ``` 45 | 46 | If you have `|` or `\` already mapped (for example, if you haven't mapped your `` 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 `` to something other than `\` and restart Vim. 47 | 48 | Personally I map Leader to Space with the following line in [my vimrc](https://github.com/sts10/terminal_and_vim_settings/blob/master/vimrc): `let mapleader = "\"`. 49 | 50 | I chose `\` as ZipOpen and `|` as ZipClose because they look like a zipper opening and closing. 51 | 52 | ## Things to Know 53 | 54 | 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. 55 | 56 | It also looks like you can override this on a filetype basis in your `vimrc` with an autocmd, such as: 57 | 58 | ``` 59 | autocmd FileType python setlocal foldmethod=syntax 60 | autocmd FileType vim setlocal foldmethod=marker 61 | ``` 62 | 63 | ## Special Thanks / Inspiration 64 | 65 | A good amount of this plugin was adapted from [this Stack Overflow answer](http://stackoverflow.com/a/9407015/3160994) from user [ib.](http://stackoverflow.com/users/254635/ib). Also thanks [Tim Pope](https://github.com/tpope)'s [Commentary plugin](https://github.com/tpope/vim-commentary), where I copped some plugin tricks. 66 | 67 | Pull requests encouraged! Also feel free to [hit me up on Twitter](https://twitter.com/sts10). 68 | -------------------------------------------------------------------------------- /doc/tags: -------------------------------------------------------------------------------- 1 | Bslash zipper.txt /*Bslash* 2 | C-Bslash zipper.txt /*C-Bslash* 3 | bar zipper.txt /*bar* 4 | zipper.txt zipper.txt /*zipper.txt* 5 | -------------------------------------------------------------------------------- /doc/zipper.txt: -------------------------------------------------------------------------------- 1 | *zipper.txt* Quickly Fold and Unfold 2 | 3 | Author: Sam Schlinkert 4 | 5 | Quickly fold and unfold multiple lines of code. Should work 6 | in Visual mode as well as Normal mode. 7 | 8 | *bar* 9 | | Create/close fold. Similar to zc. 10 | 11 | *Bslash* 12 | \ Go to next closed fold and open it 13 | 14 | *C-Bslash* 15 | Go to previous closed fold and open it 16 | 17 | 18 | vim:tw=78:et:ft=help:norl: 19 | -------------------------------------------------------------------------------- /plugin/zipper.vim: -------------------------------------------------------------------------------- 1 | 2 | if &foldmethod ==# 'manual' 3 | set foldenable 4 | set foldmethod=indent 5 | endif 6 | 7 | function! NextClosedFold(dir) 8 | if !(foldclosed(line('.')) > 0) 9 | let cmd = 'norm!z' . a:dir 10 | let view = winsaveview() 11 | let [l0, l, openf] = [0, view.lnum, 1] 12 | while l != l0 && openf 13 | exe cmd 14 | let [l0, l] = [l, line('.')] 15 | let openf = foldclosed(l) < 0 16 | endwhile 17 | if openf 18 | call winrestview(view) 19 | endif 20 | endif 21 | endfunction 22 | 23 | function! CloseFold() 24 | normal zc 25 | endfunction 26 | 27 | nnoremap ZipClosed :call CloseFold() 28 | nnoremap ZipOpenNext :call NextClosedFold('j')zo 29 | nnoremap ZipOpenPrev :call NextClosedFold('k')zo 30 | 31 | vnoremap ZipOpenVisual :'<,'>normal zo 32 | vnoremap ZipClosedVisual :'<,'>normal zc 33 | 34 | if !hasmapto('ZipClosed') || maparg('', 'n') ==# '' 35 | nmap ZipClosed 36 | vmap ZipClosedVisual 37 | endif 38 | 39 | if !hasmapto('ZipOpenNext') || maparg('', 'n') ==# '' 40 | nmap ZipOpenNext 41 | nmap ZipOpenPrev 42 | vmap ZipOpenVisual 43 | endif 44 | 45 | -------------------------------------------------------------------------------- /vim-zipper-gif.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sts10/vim-zipper/b5f93edd02cd9e8584769aab6f4b0b4d0964fb19/vim-zipper-gif.gif --------------------------------------------------------------------------------