2

How can I implement folding behaviour like a vim? In vim I has this code:

set foldenable          " enable folding
set foldmethod=indent   " fold sections by indentation
set foldcolumn=1        " display folding column
set foldlevel=0         " first folding level is open

I need close all fold levels 'level by level'. When I unfold some 'folding level', included fold levels must be closed.

Maybe I can implement this with HideShow plugin or another variant?

  • If vimish-fold doesn't suit your needs, consider submitting a feature request to the maintainer. See: github.com/mrkkrp/vimish-fold – lawlist Nov 6 '16 at 5:23
  • @lawlist can you show vimish-fold conf example? As far as I know vimish-fold allow to fold only selected regions of text. Or not? – sivakov512 Nov 6 '16 at 5:26
  • Never used it sorry, but I know it was written specifically for the vim folks about a year ago. Other than the built-in hide-show and standard ability to hide text with the invisible property, that's the extent of my knowledge. Perhaps another forum participant can help further. – lawlist Nov 6 '16 at 5:31
  • Checkout this post if it answers your question. – Compro Prasad Nov 6 '16 at 5:49
  • @ComproPrasad -- that was the thread that inspired the author of vimish-fold to write the library about a year ago. :) – lawlist Nov 6 '16 at 5:50
4

There are a lot of different implementations of folding for emacs. Some are specific to a particular programming language or syntax, others are more generic. Of the generic ones, it sounds like yafolding is exactly what you're asking for. I tried it out, and while it feels somewhat slow, it does work.

Another I tried is called origami, and I liked the results a lot better. It goes by indentation if that's all it knows about your file, but if it's one of a dozen or so supported languages then it uses syntax markers like curly braces to do the folding. It was also much quicker to fold an entire file. On the other hand, it apparently comes with no pre-configured keybindings, so to use it you'll have to set those up yourself (it does provide a keymap to add them to).

There are a number of others you might check out; run M-x list-packages and then search for "fold".

Many people also use outline-minor-mode to do folding and organization of code. It works off of embedded headline markers in comments rather than the structure of the code, but that's handy for many things as well. It also has the advantage of being built-in.

  • Thank you! origami works well. Maybe you know how fold class methods in python code? For example,use major-mode folding and folding by indentation simultaneously? And...maybe you can answer on this question emacs.stackexchange.com/questions/28371/… ? :) – sivakov512 Nov 7 '16 at 6:18
  • 1
    So... Now I think that outline-mode really better than the origami-mode and all other... – sivakov512 Nov 7 '16 at 7:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.