# Code folding for emacs?

**URL:** <https://discourse.julialang.org/t/code-folding-for-emacs/41421>\
**Category:** Tooling\
**Created:** [June 15, 2020, 3:48am UTC](https://discourse.julialang.org/t/code-folding-for-emacs/41421 "2020-06-15T03:48:52Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![Perrin\_Meyer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/perrin_meyer/32/15686_2.png) [@Perrin\_Meyer](https://discourse.julialang.org/u/Perrin_Meyer)\
**Post date:** [June 15, 2020, 3:48am UTC](https://discourse.julialang.org/t/code-folding-for-emacs/41421/1 "2020-06-15T03:48:52Z")

</div>

I’m using julia-mode.el, but I’m by no means an emacs wizard

Is there a way to do code folding? I.e. I place the cursor at the start of a function, press some key combo, and it becomes one line… and then another key combo brings it back?

I tried looking at outline-minor-mode and hideshow mode, on the emacs wiki, but could not figure them out… (maybe I should just bite the bullet and learn LISP, sigh).

thanks

---

<div class="post-metadata">

**Author:** ![jagot](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jagot/32/12217_2.png) [@jagot](https://discourse.julialang.org/u/jagot)\
**Post date:** [June 15, 2020, 6:52am UTC](https://discourse.julialang.org/t/code-folding-for-emacs/41421/2 "2020-06-15T06:52:20Z")

</div>

I’m using `outshine`, like this:

```plaintext
(use-package outshine
  :init
  (progn
    (add-hook 'outline-minor-mode-hook 'outshine-mode)
    (add-hook 'c-mode-common-hook 'outline-minor-mode)
    (add-hook 'sh-mode-common-hook 'outline-minor-mode)
    (add-hook 'julia-mode-hook 'outline-minor-mode)))

```

It does not let me fold functions, but I can add small Org-like headers in comments, like this:

```julia
# * First section

first_function(x) = 30x

# * Second section

second_function(x) = 80x

# ** Subsection

third_function(x,y) = cos(x)*sin(y)

```

and then I can hide an entire section by setting the cursor on the line of the header and pressing `<Tab>` (or `C-i`).

 ![image](https://global.discourse-cdn.com/julialang/original/3X/5/b/5b386596e436b2378d1c49bca3179273131b7d32.png)

---

<div class="post-metadata">

**Author:** ![gcalderone](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gcalderone/32/1539_2.png) [@gcalderone](https://discourse.julialang.org/u/gcalderone)\
**Post date:** [June 15, 2020, 8:14am UTC](https://discourse.julialang.org/t/code-folding-for-emacs/41421/3 "2020-06-15T08:14:09Z")

</div>

Just add the following to your `.emacs` file:

```julia
(global-set-key (kbd "<C-f5>") 'set-selective-display-dlw)

(defun set-selective-display-dlw (&optional level)
"Fold text indented same of more than the cursor.
If level is set, set the indent level to LEVEL.
If 'selective-display' is already set to LEVEL, clicking
F5 again will unset 'selective-display' by setting it to 0."
  (interactive "P")
  (if (eq selective-display (1+ (current-column)))
      (set-selective-display 0)
    (set-selective-display (or level (1+ (current-column))))))

```

Then put the cursor at the beginning of a block (any block: functions, loops, etc…) and press Ctrl+F5 to toggle folding at that level.

Further info here: [elisp - How to achieve code folding effects in Emacs? - Stack Overflow](https://stackoverflow.com/questions/1085170/how-to-achieve-code-folding-effects-in-emacs#1211325)

---

<div class="post-metadata">

**Author:** ![jagot](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jagot/32/12217_2.png) [@jagot](https://discourse.julialang.org/u/jagot)\
**Post date:** [June 15, 2020, 8:53am UTC](https://discourse.julialang.org/t/code-folding-for-emacs/41421/4 "2020-06-15T08:53:17Z")

</div>

That’s very nice!

I’d like to add that the rationale they quote in the StackOverflow post you linked is why I never bothered with proper function folding; using e.g. [ivy](https://github.com/abo-abo/swiper) it’s very easy to find your way around a large file. However, sometimes I do wish to hide a large block of the file, and that’s when I use the `outshine` trick above; basically, I order my functions by theme and then it’s very easy to navigate the file on a high level as well.

---

<div class="post-metadata">

**Author:** ![Perrin\_Meyer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/perrin_meyer/32/15686_2.png) [@Perrin\_Meyer](https://discourse.julialang.org/u/Perrin_Meyer)\
**Post date:** [June 17, 2020, 10:23pm UTC](https://discourse.julialang.org/t/code-folding-for-emacs/41421/5 "2020-06-17T22:23:40Z")

</div>

Thats awesome! thanks!

However, for me it seams to collapse all the functions at the same time…(which makes sense, since its indent based). Is there a way to only have it collapse the current function? I.e. could Ctrl+, mark a region, and then press CTRL-F6 or something? Even cooler if it could recognize julia End statements…

---

<div class="post-metadata">

**Author:** ![gcalderone](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gcalderone/32/1539_2.png) [@gcalderone](https://discourse.julialang.org/u/gcalderone)\
**Post date:** [June 18, 2020, 8:03am UTC](https://discourse.julialang.org/t/code-folding-for-emacs/41421/6 "2020-06-18T08:03:11Z")

</div>

Maybe this is what you’re looking for:  
[https://github.com/magnars/fold-this.el](https://github.com/magnars/fold-this.el)

but you’ll need to select the region you wish to fold, i.e. it can’t (AFAIK) fold a block up to the `end` statement. I used it for a while, then turned back to the indent-based folding.

The reason is that a block depth is related to a “scope” (in all case I could think of, not just Julia source code). In order to focus on a “scope” I typically want to hide the “minor details” (i.e. deeper blocks). At the same time I don’t need text on the same scope to be hidden since it would typically be outside my scrolling window (unless the file is very, very short…). Hence an **indent-based folding** fits perfectly my workflow.

On the other hand, a **region-based folding** would possibly lead to an excessive number of _fold-this_, _unfold-that_ activities, that I wish to avoid. Just my 2 cents… 😉

---

<div class="post-metadata">

**Author:** ![Perrin\_Meyer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/perrin_meyer/32/15686_2.png) [@Perrin\_Meyer](https://discourse.julialang.org/u/Perrin_Meyer)\
**Post date:** [June 18, 2020, 5:56pm UTC](https://discourse.julialang.org/t/code-folding-for-emacs/41421/7 "2020-06-18T17:56:11Z")

</div>

thanks! thats a very simple and useful emacs extension.

it looks like (by same author)  
[https://github.com/magnars/expand-region.el](https://github.com/magnars/expand-region.el)  
is more advanced, and people have already added Ruby, Erlang, etc syntax, so I’ll attempt to add some Julia constructs, and see what I get.

---

<div class="post-metadata">

**Author:** ![Ronis\_BR](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ronis_br/32/50999_2.png) [@Ronis\_BR](https://discourse.julialang.org/u/Ronis_BR)\
**Post date:** [June 18, 2020, 6:14pm UTC](https://discourse.julialang.org/t/code-folding-for-emacs/41421/8 "2020-06-18T18:14:20Z")

</div>

I would love to have support for `expand-region` in Julia. I use it a lot in other languages and it is awesome!

---

<div class="post-metadata">

**Author:** ![marcelovmaciel](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/marcelovmaciel/32/2573_2.png) [@marcelovmaciel](https://discourse.julialang.org/u/marcelovmaciel)\
**Post date:** [June 29, 2020, 6:54pm UTC](https://discourse.julialang.org/t/code-folding-for-emacs/41421/9 "2020-06-29T18:54:03Z")

</div>

I had saved this reply to test this package (outshine). I’ve tested it right now and I’m happy it works here, going to incorporate it into my emacs workflow. This reminds me that I should periodically take a look here for tips like this one 😆
