Skip to content

Instantly share code, notes, and snippets.

@mwfogleman
Last active May 19, 2018 23:28
Show Gist options
  • Save mwfogleman/95cc60c87a9323876c6c to your computer and use it in GitHub Desktop.
Save mwfogleman/95cc60c87a9323876c6c to your computer and use it in GitHub Desktop.
Emacs: narrow-or-widen-dwim
(defun narrow-or-widen-dwim ()
"If the buffer is narrowed, it widens. Otherwise, it narrows to region, or Org subtree."
(interactive)
(cond ((buffer-narrowed-p) (widen))
((region-active-p) (narrow-to-region (region-beginning) (region-end)))
((equal major-mode 'org-mode) (org-narrow-to-subtree))
(t (error "Please select a region to narrow to"))))
;; I bind this key to C-c n, using the bind-key function that comes with use-package.
(bind-key "C-c n" 'narrow-or-widen-dwim)
;; I also bind it to C-x t n, using Artur Malabarba's toggle map idea:
;; http:://www.endlessparentheses.com/the-toggle-map-and-wizardry.html
(bind-key "n" 'narrow-or-widen-dwim toggle-map)
@mwfogleman
Copy link
Author

Yes, and yes!

Copy link

ghost commented May 19, 2018

Nice. What I did though was add a speed command to org:
("n" . org-narrow-to-subtree)
but I still type M-x widen to undo it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment