(redirected from Homepage)

Strona domowa

Witam na mojej prywatnej stronie internetowej!

[If this is all Polish to you, click here: English]

Uwaga: z oczywistych powodów nie mogę zagwarantować swojej nieomylności, choć staram się o zgodność tego, co piszę, z Prawdą. Jest również oczywiste, że nie gwarantuję takiej zgodności w przypadku komentarzy. Umieszczenie linku do strony spoza niniejszego serwisu nie musi oznaczać, że podzielam poglądy autora tej strony, a jedynie, że uważam ją za wartościową z takich czy innych powodów.

Marcin ‘mbork’ Borkowski

2026-07-20 Hideshow mode

Recently I discovered a very cool feature of Emacs. Apparently, it’s been present in Emacs for over 30 years, although the Git log shows that it’s been heavily worked on very recently. Meet Hideshow mode. It’s purpose is simple: it allows to, well, hide or show blocks of code.

There is another feature like this (probably even older), called selective display (in fact, I’ve written about it several years ago). It turns out that Hideshow mode is way better. Its main disadvantage is that its keymap is bound to C-c @, which is probably one of the least ergonomic bindings possible. Apart from that, it’s great. You can press C-c @ C-d and C-c @ C-s to hide and show the current block, or just use C-c @ C-c to toggle the current block’s hide/show status. (You can also click S-mouse-2, that is, shift-click the middle button, if you are a mouse person.) Also, C-c @ C-t hides all top-level blocks in the buffer and C-c @ C-a shows everything. (Go to the manual to see a bit more features.)

But wait, there’s more! If you are brave enough to compile Emacs from the master branch, you’ll get something much better! Very recently, Emacs devs added a few fantastic options to Hideshow mode, and once I saw them, I immediately decided to turn them on.

First of all, you can set hs-display-lines-hidden to t, and then you’ll see the number of hidden lines along with the ellipsis. Then, you can set hs-show-indicators to t to see little chevrons in the fringe whenever there is a block starting in that line that can be shown or hidden. These chevrons can be clicked to toggle the visibility of the block.

One of the coolest features of Hideshow mode is the hs-cycle-filter option. It is nil by default, but you can set it to t so that pressing TAB on a line where a block begins toggles its visibility (via hs-toggle-hiding, that is, like C-c @ C-c). Of course, TAB is usually bound to indent-for-tab-command (at least whenever I edit JavaScript, which is my main use case for Hideshow mode). That’s why I set hs-cycle-filter to #'bolp – this means that the toggling behavior of TAB is only active when the point is at the beginning of line, and I can still use it to fix indentation with point somewhere else.

Now, I’ve saved one thing for the end of this post. There is one more command in Hideshow mode (again, only on the master branch as of writing this): hs-cycle. It works very similar to hs-toggle-hiding, but acts as a three-way toggle between hiding the whole block, showing the block but hiding its nested blocks, and showing everything beneath it. (It also accepts a numerical argument greater than 1 and then hides blocks that many levels underneath.) By default it is bound to C-c @ TAB, but I like it so much that I decided to make it easier to use. Here is what the docstring for the hs-cycle-filter option said pretty recently:
Currently it affects only the command hs-toggle-hiding by default, but it can be easily replaced with the command hs-cycle.
Here, the “easy” part means diving into the source code and changing what needs to be changed:

(keymap-set hs-minor-mode-map
            "TAB"
            `(menu-item
              "" hs-cycle
              :filter
              ,(lambda (cmd)
                 (when (and hs-cycle-filter
                            ;; On the headline with hideable blocks
                            (save-excursion
                              (forward-line 0)
                              (hs-get-first-block-on-line))
                            (or (not (functionp hs-cycle-filter))
                                (funcall hs-cycle-filter)))
                   cmd))))

This is copied almost verbatim from the (defvar-keymap hs-minor-mode-map ...) form in hideshow.el – I just swapped hs-cycle for hs-toggle-hiding. (I have to admit that I don’t fully understand this snippet. I never studied Emacs menus, which are quite a sophisticated feature, and while I obviously have a general idea of how this code most probably works, I’d prefer not to analyze it too deeply – menus in Emacs are something I never use, and that is not something I want to change in the near future.) And now, TAB at the beginning of line when I edit JavaScript works much like in Org mode!

Of course, it would be nicer if Emacs allowed to make this happen without cargo-culting parts of its source code, so I made a feature request to allow to set this up more easily. Lo and behold, Emacs now has it! Elijah Gabe Pérez added a function called hs-add-cycle-binding, which now allows to do this:

(hs-add-cycle-binding nil "TAB" #'hs-cycle)

instead of my (much longer) snippet. Hurray!

The only thing I slightly miss is that Hideshow mode cannot hide pages, which are another feature of Emacs I often use to divide my source files into “sections”. But that is something which can be remedied in another way, and will be a topic of a future post.

Anyway, that’s it for today. I’m definitely going to keep an eye on any further Hideshow mode developments, and I strongly recommend you to check it out – especially when the next Emacs version is released!

CategoryEnglish, CategoryBlog, CategoryEmacs

Comments on this page

More...