u/sylas_main

▲ 8 r/emacs

this is my best emacs function written in my config up till now

  • I created an emacs function that would
    • create a custom tmp split buffer
      • that would list only all functions assigned to a shortcut on the format:

>

function ==> shortcut

  • and the buffer is sorted by funtions names
  • plus has line numbers;

​

;; --------------------
;; list bindings
;; --------------------
(defun list-bindings ()
  "List all commands with a keybinding, sorted by name."
  (interactive)
  (let* ((bindings '())
         (seen (make-hash-table :test #'equal)))

    (cl-labels
        ((walk (km)
           (map-keymap
            (lambda (_evt bind)
              (cond
               ((keymapp bind)
                (walk bind))
               ((and (commandp bind) (symbolp bind))
                (unless (gethash bind seen)
                  (puthash bind t seen)
                  (let ((keys (where-is-internal bind nil t)))
                    (when keys
                      (push (cons (symbol-name bind)
                                  (key-description keys))
                            bindings)))))))
            km)))

      ;; Walk maps
      (dolist (km
               (append
                (list (current-global-map)
                      (current-local-map))
                (when (featurep 'evil)
                  (list evil-normal-state-map
                        evil-insert-state-map
                        evil-visual-state-map
                        evil-motion-state-map
                        evil-emacs-state-map))))
        (when km (walk km)))

      ;; Sort
      (setq bindings
            (sort bindings
                  (lambda (a b) (string< (car a) (car b)))))

      ;; Output buffer
      (with-current-buffer (get-buffer-create "*keybindings*")
        (let ((inhibit-read-only t))
          (erase-buffer)
          (insert (format "%-50s %s\n" "Function" "Keybinding"))
          (insert (make-string 70 ?=) "\n\n")
          (pcase-dolist (`(,cmd . ,keys) bindings)
            (insert (format "%-50s ==>  %s\n" cmd keys)))
          (goto-char (point-min))
          (setq buffer-read-only t))
        (display-buffer (current-buffer))
        (message "Found %d keybindings" (length bindings))))))

;; Bind it
(global-set-key (kbd "C-c k") #'list-bindings)

this is so much useful for the new emacs users especially

>makes it easier to search functions/ memorize their names / and of course remember the keybindings of them

hope you enjoy it

reddit.com
u/sylas_main — 28 days ago