From 5460412d96c2ccf05f04a5a229dd7230a29ce48e Mon Sep 17 00:00:00 2001 From: Aryadev Chavali Date: Fri, 14 Aug 2020 23:55:14 +0100 Subject: +custom code to toggle any given buffer When called, function will: - Close buffer hosting window if buffer displayed - Open new window displaying buffer This can be useful for utils like eshell which don't provide this functionality. --- Emacs/.config/emacs/config.org | 15 +++++++++++++++ 1 file changed, 15 insertions(+) (limited to 'Emacs') diff --git a/Emacs/.config/emacs/config.org b/Emacs/.config/emacs/config.org index c73670c..e58344f 100644 --- a/Emacs/.config/emacs/config.org +++ b/Emacs/.config/emacs/config.org @@ -129,6 +129,21 @@ The logic is pretty simple: (goto-char (+ old 1)) (evil-normal-state)))) #+END_SRC +** Toggle buffer +For some buffer with name =buf-name= with a creation function +=buf-create=, toggle it via this function. +#+BEGIN_SRC emacs-lisp +(defun +dx/toggle-buffer (buf-name buf-create) + (interactive) + (let* ((buffer (or (get-buffer buf-name) (funcall buf-create))) + (displayed (get-buffer-window buffer))) ; Get window when displayed, nil otherwise + (cond (displayed ; already displayed thus delete + (select-window displayed) + (delete-window)) + (t ; not displayed thus show and select + (display-buffer buffer) + (select-window (get-buffer-window buffer)))))) +#+END_SRC * General Setup general, a good package for defining keys. In this case, I generate a new definer for the "LEADER" keys. Leader is bound to SPC -- cgit v1.2.3-13-gbd6f