Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Emacs Org-Mode: how to fold block without going to block header?

Tags:

emacs

org-mode

I know I can go to a block header and fold/unfold by hitting the TAB key. However suppose I'm inside a block that has hundreds of lines, and I just want to fold the current block, without having to go to the block header -- is there a keyboard short-cut that can do that? Or is there an elisp function that does that, so that I can bind some shortcut to that function?

like image 252
Prasad Chalasani Avatar asked Dec 22 '11 17:12

Prasad Chalasani


2 Answers

Create a keybinding that performs the following function:

(defun zin/org-cycle-current-headline ()
  (interactive)
  (outline-previous-heading)
  (org-cycle))

This will jump back to the previous headline and then cycle. Since the headline is already open, it will close it. It also places the point at the start of the headline.

If you wrap the two commands in (save-excursion ) it will retain the point, however that could lead to entering information inside the ellipsis without realizing it. Alternately you can change the command to call a non-interactive form:

(defun zin/org-cycle-current-headline ()
  (interactive)
  (org-cycle-internal-local))

This is equivalent to the above with (save-excursion ).

like image 158
Jonathan Leech-Pepin Avatar answered Oct 21 '22 16:10

Jonathan Leech-Pepin


C-c C-p will get you to the heading, TAB will fold. You can create a keyboard macro for this, or the equivalent ELISP:

(defun up-n-fold ()
  (interactive)
   (progn
     (outline-previous-visible-heading 1)
     (org-cycle)))

Edit: corrected C-c p for C-c C-p as noted by many below. Thanks!

like image 4
Juancho Avatar answered Oct 21 '22 17:10

Juancho