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?

6
  • If vimish-fold doesn't suit your needs, consider submitting a feature request to the maintainer. See: github.com/mrkkrp/vimish-fold
    – lawlist
    Commented Nov 6, 2016 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
    Commented Nov 6, 2016 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
    Commented Nov 6, 2016 at 5:31
  • Checkout this post if it answers your question. Commented Nov 6, 2016 at 5:49
  • @ComproPrasad -- that was the thread that inspired the author of vimish-fold to write the library about a year ago. :)
    – lawlist
    Commented Nov 6, 2016 at 5:50

2 Answers 2

5

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.

2
  • 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
    Commented Nov 7, 2016 at 6:18
  • 1
    So... Now I think that outline-mode really better than the origami-mode and all other...
    – sivakov512
    Commented Nov 7, 2016 at 7:06
0

I recommend trying outline-indent.el, an Emacs package that offers a minor mode for code folding and outlining based on indentation levels. The outline-indent.el package leverages the built-in outline-minor-mode, which is maintained by the Emacs developers and has less chance of being abandoned like origami.el.

The built-in outline-minor-mode, which outline-indent.el is based on, enables utilizing functions such as folding and moving indented blocks up or down, significantly enhancing the editing experience for indented text.

(The origami.el package is no longer actively maintained and has known bugs that can affect its reliability and performance.)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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