showing and hiding blocks of code

When I'm writing some code, I find it often useful to hide most function bodies, and only see the one I am working on. This way, I can keep the overview, and concentrate on the part I am working on. Emacs has support for showing/hiding blocks of code, using the HideShow-package. HideShow works with C/C++, Lisp, Scheme, Java, Perl, PHP, TCL, VHDL and Fortran. It also seems to work with Javascript (at least JS2).

You can enable Hide-Show for C/C++ code by adding the following to your .emacs:

(add-hook 'c-mode-common-hook
(lambda()
(local-set-key (kbd "C-c <right>") 'hs-show-block)
(local-set-key (kbd "C-c <left>") 'hs-hide-block)
(local-set-key (kbd "C-c <up>") 'hs-hide-all)
(local-set-key (kbd "C-c <down>") 'hs-show-all)
(hs-minor-mode t)))
You can hide or show either the current block, or the whole file. To show the former, consider a C-function:

int my_function (int a, int b)
{
return a + b;
}
Now, when calling hs-hide-block, we get:

int my_function (int a, int b)...
HideShow has one problem: the truly bizarre default key bindings, such as 'C-c @ ESC C-s' for hs-show-all. This is the reason I am adding these alternative key bindings in the add-hook above - 'C-c' and the arrow keys; alternatively, you could use the 'Super'-key instead of C-c.

If you'd like to hide everything by default when you open a file, you can do so by adding

(hs-hide-all)
to the above hook function.


Side-note: Even though there are many keys on your keyboard, there is a finite number of easy combinations, especially when the arrow-keys are involved. They are popular, and not just by emacs -- for example, window managers often use Ctrl-Alt-<arrow-key> for switching between virtual desktops. So, it's good to think a little bit where to 'spend' your easiest key bindings.

No comments:

Post a Comment

Followers

Popular Posts