Skip to content

Instantly share code, notes, and snippets.

@thriveth
Created November 5, 2022 17:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save thriveth/1cfb03fac55c3076a34f9627bc3c6a63 to your computer and use it in GitHub Desktop.
Save thriveth/1cfb03fac55c3076a34f9627bc3c6a63 to your computer and use it in GitHub Desktop.
Evil-mode setup and keybindings managed with General.el

Evil-mode & friends

EVIL itself

(use-package evil
  :init
  (setq evil-want-keybinding nil)	;
  (setq evil-want-integration t)
  :config
  (define-key evil-normal-state-map (kbd "C-h") 'evil-window-left)
  (define-key evil-normal-state-map (kbd "C-j") 'evil-window-down)
  (define-key evil-normal-state-map (kbd "C-k") 'evil-window-up)
  (define-key evil-normal-state-map (kbd "C-l") 'evil-window-right)
  (evil-select-search-module 'evil-search-module 'evil-search)
  (evil-mode 1)
  )

Evil-collection

A set of additional integrations for Evil with a bunch of packages and modes that not everyone might want (but which I do).

(use-package evil-collection
  :after evil
  :ensure nil
  :config
  (evil-collection-init)
  )

Evil-Quickscope

Basically a port of Vim-sniper.

(use-package evil-quickscope
  :ensure nil
  :config
  (global-evil-quickscope-mode 1)
  :bind ("C-q" . evil-quickscope-mode)
  )

Evil commentary

Port of Tim Pope’s plugin

(use-package evil-commentary
  :ensure nil
  :config
  (evil-commentary-mode 1)
  )

Evil port of Vim-Surround

(use-package evil-surround
  :ensure nil
  :after
  (evil)
  :config
  (global-evil-surround-mode t)
  )

Vim-like undo handling with undo-fu

(use-package undo-fu
  :ensure nil
  :after evil
  :config
  (setq evil-undo-system "undo-fu")
  (define-key evil-normal-state-map "u" 'undo-fu-only-undo)
  (define-key evil-normal-state-map "\C-r" 'undo-fu-only-redo))

Keybindings with General.el

Function: Toggle maximize current window

“Monocle view” found <a href=”https://www.reddit.com/r/emacs/comments/gtfxg4/zoommonocle_a_buffer/ “>on Reddit, stolen from Spacemacs.

(defun my/toggle-maximize-buffer ()
  "Maximize current buffer"
  (interactive)
  (if (= 1 (length (window-list)))
      (jump-to-register '_)
    (progn
      (window-configuration-to-register '_)
      (delete-other-windows))))

General in general

These are first key sequences triggered by <Space>, kinda like in Doom/Spacemacs, but with my own mnemonics.

(use-package general 
  :after evil-collection  ;; Manual local keybindings after packaged ones!
  :ensure t
  :config
  (general-evil-setup t)
  (general-define-key
   :states '(normal visual motion emacs)
   :keymaps 'override
   :prefix "SPC"
   :non-normal-prefix "C-SPC"
   ;; simple command
   "m"   '(mu4e :which-key "Email (mu4e)")
   ;; "SPC" '(counsel-M-x :which-key "Command palette")
   "SPC" '(execute-extended-command :which-key "Command palette")
   "TAB" '(er-switch-to-previous-buffer :which-key "Prev buffer")
   "/" '(counsel-grep-or-swiper :which-key "Search buffer")
   "l" '(display-line-numbers-mode :which-key "Toggle line numbers")
   "," '(evil-ex-nohighlight :which-key "Clear highlight")
   "r" '(repeat :which-key "Repeat last action")
   ;; The function above to toggle maximized window, found at
   ;; https://www.reddit.com/r/emacs/comments/gtfxg4/zoommonocle_a_buffer/
   "z" '(my/toggle-maximize-buffer :which-key "Toggle monocle mode")

   ;; Applications
   "a" '(:ignore t :which-key "Applications")
   "ar" 'ranger
   "ad" 'dired

   ;; Navigate buffers
   "b"  '(:ignore t :which-key "Buffers & Bookmarks")
   ;; "bp" '(switch-to-other-buffer :which-key "Prev buffer")
   "bb" '(consult-buffer :which-key "Buffer picker")
   ;; ~iflipb~ buffer switching
   "bp" '(iflipb-previous-buffer :which-key "Prev buffer")
   "bn" '(iflipb-next-buffer :which-key "Prev buffer")
   ;; "bk" '(kill-this-buffer :which-key "Kill (close) buffer")
   "bk" '(iflipb-kill-buffer :which-key "Kill (close) buffer")
   "bj" '(bookmark-jump :which-key "Jump to bookmark")
   "bs" '(bookmark-set :which-key "Set bookmark")

   ;; Config and Commentary
   "c" '(:ignore t :which-key "Commenting, Emacs config")
   "ce" '((lambda() (interactive)(find-file (expand-file-name "init.el" user-emacs-directory))) :which-key "Open config file")
   "cl" '((lambda() (interactive)(load-file (expand-file-name "init.el" user-emacs-directory))) :which-key "Execute config file")
   "cc" '(evil-commentary-line :which-key "Comment line")
   "cv" '(evil-commentary :which-key "Comment selected")

   ;; Deft
   "d" '(:ignore t :which-key "Deft")
   "dd" '(deft :which-key "Overview")
   "dr" '(deft-refresh :which-key "Refresh")
   "ds" '(deft-toggle-sort-method :which-key "Toggle sort method")

   ;; Files
   "f" '(:ignore t :which-key "Files")
   "ff" '(counsel-find-file :which-key "Find file")
   "fz" '(counsel-fzf :which-key "Fuzzy find in this dir")
   "fr" '(consult-buffer :which-key "Buffers and MRU")
   "fg" '((lambda ()
            (interactive)
            (let ((current-prefix-arg 4))
              (call-interactively #'counsel-fzf)))
          :which-key "Fuzzy find in other dir")

   ;; Avy motion
   "j" '(:ignore t :which-key "Quick motion")
   "jk" '(avy-goto-char-2 :which-key "Goto two-character sequence")
   "jj" '(avy-goto-word-0 :which-key "Goto word beginning")

   ;; Org-mode functionality accessible from anywhere.
   "o" '(:ignore t :which-key "Org-mode")
   "ol" '(:ignore t :which-key "Org-links")
   "oll" '(org-store-link :which-key "Store link")
   "oli" '(org-insert-last-stored-link :which-key "Insert latest")
   "oc" '(org-capture :which-key "Org capture")
   "os" '(org-save-all-org-buffers :which-key "Save all Org buffers")

   ;; Search
   "s" '(:ignore t :which-key "Search")
   "ss" '(consult-line :which-key "Search buffer lines")
   "sc" '(evil-ex-nohighlight :which-key "Clear highight")

   ;; Toggle various UI elements
   "t" '(:ignore t :which-key "Toggle terminal or UI elements")
   "tm" '(toggle-menu-bar-mode-from-frame :which-key "Menu bar")
   "tn" '(display-line-numbers-mode :which-key "Line numbers")
   "tt" '(vterm-toggle :which-key "Terminal")
   "ts" '(toggle-scroll-bar :which-key "Scrollbar")

   ;; Window management
   "w" '(:ignore t :which-key "Windows")
   "wl" '(evil-window-right  :which-key "Go right")
   "wh" '(evil-window-left   :which-key "Go left")
   "wk" '(evil-window-up     :which-key "Go up")
   "wj" '(evil-window-down   :which-key "Go down")
   "wd" '(evil-window-delete :which-key "Close")
   "wn" '(evil-window-split  :which-key "New over/under")
   "wv" '(evil-window-vsplit :which-key "New side-by-side")
   "ww" '(:ignore t :which-key "Resize")
   "wwj" '(shrink-window :which-key "Shrink window vertically")
   "wwk" '(enlarge-window :which-key "Enlarge window vertically")
   "wwh" '(shrink-window-horizontally :which-key "Shrink window horizontally")
   "wwl" '(enlarge-window-horizontally :which-key "Enlarge window horizontally")

   ;; Yasnippet
   "y" '(:ignore t :which-key "Yasnippet")
   "yy" '(consult-yasnippet :which-key "Insert snippet with completion")
   )
  )

Org-mode specific General.el key bindings

Using comma as “Localleader”. Similar ones can of course be defined for other modes.

(general-evil-define-key 'motion org-mode-map
  :prefix ","
    ;; Links
    "l" '(:ignore t :which-key "Org-links")
    "ll" '(org-insert-link :which-key "Insert link")
    "lo" '(org-open-at-point :which-key "Follow link")
    "lt" '(org-toggle-link-display :which-key "Toggle link display")
    ;; Agenda
    "a" '(org-agenda :which-key "Agenda")
    ;; BibTeX
    "b" '(:ignore t :which-key "BibTeX and Org-ref")
    "bb" '(ivy-bibtex :which-key "Search BibTeX")
    "bn" '(ivy-bibtex-with-notes :which-key "Search BibTeX with notes only")
    ;; Structure elements
    "i" '(:ignore t :which-key "Insert Org elements")
    "ib" '(org-insert-structure-template :which-key "Block template")
    ;; Headings
    "h" '(:ignore t :which-key "Headings")
    "hh" '(org-toggle-heading :which-key "Toggle heading here")
    "hj" '(org-insert-heading-after-current :which-key "Heading below")
    "hb" '(org-insert-heading :which-key "Heading this line")
    ; Org-roam
    "m" '(:ignore t :which-key "Org-Roam")
    "mt" '(org-roam-tag-add :which-key "Add tag")
    "ma" '(org-roam-alias-add :which-key "Add alt title")
    "mi" '(org-roam-node-insert :which-key "Find or create note")
    "mn" '(org-roam-capture :which-key "Capture new note")
    "mm" '(org-roam-node-find :which-key "Find note")
    "ms" '(org-roam-buffer-toggle :which-key "Toggle Roam sidebar")
    "mj" '(org-roam-ui-mode :which-key "Start graph server")
    "mr" '(orb-insert-link :which-key "Insert link to literature note")
    ; Org-roam dailies
    "md" '(:ignore t :which-key "Org-roam Dailies")
    "mdd" '(org-roam-dailies-goto-today :which-key "Today")
    "mdo" '(org-roam-dailies-goto-date :which-key "Other date")
    "mdt" '(org-roam-dailies-goto-tomorrow :which-key "Tomorrow")
    "mdy" '(org-roam-dailies-goto-yesterday :which-key "Yesterday")
    ;; Org save all buffers
    "s" '(org-save-all-org-buffers :which-key "Save all org buffers")
    ;; Toggles
    "t" '(:ignore t :which-key "Toggle org things")
    "ti" '(org-display-inline-images :which-key "Toggle inline images")
    "tp" '(org-pretty-entities :which-key "Toggle pretty entities")
    ;; Export
    "e" '(org-export-dispatch :which-key "Export menu")
    )

Org-roam in insert mode

(general-evil-define-key 'insert org-mode-map
  :prefix "C-o"
    ""  '(nil :which-key "Org-roam key bindings")
    "t" '(org-roam-tag-add :which-key "Add tag")
    "a" '(org-roam-alias-add :which-key "Add alt title")
    "i" '(org-roam-node-insert :which-key "Find or create note")
    "n" '(org-roam-capture :which-key "Capture new note")
    "m" '(org-roam-node-find :which-key "Find note")
    "s" '(org-roam-buffer-toggle :which-key "Toggle Roam sidebar")
    "j" '(org-roam-ui-mode :which-key "Start graph server")
    "r" '(orb-insert-link :which-key "Insert link to literature note")
    )

pdf-view mode settings

(general-evil-define-key '(normal visual motion) pdf-view-mode-map
  :prefix ","
  ;; Occur
  ;; "o" '(pdf-occur :which-key "Search (pdf-occur)")
  "f" '(pdf-occur :which-key "Find (pdf-occur)")
  ;; Annotations
  "a" '(:ignore t :which-key "Annotations")
  "aq" '(pdf-annot-add-squiggly-markup-annotation :which-key "Add squiggly annotation")
  "as" '(pdf-annot-add-strikeout-markup-annotation :which-key "Add strikeout annotation")
  "ah" '(pdf-annot-add-highlight-markup-annotation :which-key "Add highlight annotation")
  "at" '(pdf-annot-add-text-annotation :which-key "Add text annotation")
  "aa" '(pdf-annot-add-markup-annotation :which-key "Add markup annotation")
  "au" '(pdf-annot-add-underline-markup-annotation :which-key "Add underline annotation")
  "ad" '(pdf-annot-delete :which-key "Delete annotation")
  "al" '(pdf-annot-list-annotations :which-key "List annotations")
  ;; Dark mode
  "d" '(pdf-view-midnight-minor-mode :which-key "Toggle dark mode")
  )
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment