emacs-config/config.org

19 KiB

KyleKrein's GNU Emacs Config

IMPORTANT PROGRAMS TO LOAD FIRST

Evil Mode

(use-package evil
    :ensure t
    :init      ;; tweak evil's configuration before loading it
    (setq evil-want-integration t) ;; This is optional since it's already set to t by default.
    (setq evil-want-keybinding nil)
    (setq evil-vsplit-window-right t)
    (setq evil-split-window-below t)
    (evil-mode))
  (use-package evil-collection
    :ensure t
    :after evil
    :config
    (setq evil-collection-mode-list '(dashboard dired ibuffer))
    (evil-collection-init))
  (use-package evil-tutor
  :ensure t)

Recent Files

(recentf-mode t)

General Keybindings

        (use-package general
          :ensure t
          :config
          (general-evil-setup)
        )
        (require 'general) ;; If i do this in :config, it says that function is wrong
          ;; set up 'SPC' as the global leader key
          (general-create-definer kylekrein/leader-keys
            :states '(normal insert visual emacs)
            :keymaps 'override
            :prefix "SPC" ;; set leader
            :global-prefix "M-SPC") ;; access leader in insert mode
            (kylekrein/leader-keys
          "." '(find-file :wk "Find file")
          "f r" '(recentf-open :wk "Open recent file")
          "TAB TAB" '(comment-line :wk "Comment lines"))
    (kylekrein/leader-keys
        "b" '(:ignore t :wk "buffer")
        "b b" '(switch-to-buffer :wk "Switch buffer")
        "b i" '(ibuffer :wk "Ibuffer")
        "b k" '(kill-this-buffer :wk "Kill this buffer")
        "b n" '(next-buffer :wk "Next buffer")
        "b p" '(previous-buffer :wk "Previous buffer")
        "b r" '(revert-buffer :wk "Reload buffer"))

      (kylekrein/leader-keys
        "e" '(:ignore t :wk "Evaluate")    
        "e b" '(eval-buffer :wk "Evaluate elisp in buffer")
        "e d" '(eval-defun :wk "Evaluate defun containing or after point")
        "e e" '(eval-expression :wk "Evaluate and elisp expression")
        "e l" '(eval-last-sexp :wk "Evaluate elisp expression before point")
        "e r" '(eval-region :wk "Evaluate elisp in region")) 

       (kylekrein/leader-keys
        "h" '(:ignore t :wk "Help")
        "h f" '(describe-function :wk "Describe function")
        "h v" '(describe-variable :wk "Describe variable"))
        ;;"h r r" '((lambda () (interactive) (load-file "~/.config/emacs/init.el")) :wk "Reload emacs config"))
        ;;"h r r" '(reload-init-file :wk "Reload emacs config"))

       (kylekrein/leader-keys
        "t" '(:ignore t :wk "Toggle")
        "t l" '(display-line-numbers-mode :wk "Toggle line numbers")
        "t t" '(visual-line-mode :wk "Toggle truncated lines"))

  (kylekrein/leader-keys
      "w" '(:ignore t :wk "Windows")
      ;; Window splits
      "w c" '(evil-window-delete :wk "Close window")
      "w n" '(evil-window-new :wk "New window")
      "w s" '(evil-window-split :wk "Horizontal split window")
      "w v" '(evil-window-vsplit :wk "Vertical split window")
      ;; Window motions
      "w h" '(evil-window-left :wk "Window left")
      "w j" '(evil-window-down :wk "Window down")
      "w k" '(evil-window-up :wk "Window up")
      "w l" '(evil-window-right :wk "Window right")
      "w w" '(evil-window-next :wk "Goto next window")
      ;; Move Windows
      "w H" '(buf-move-left :wk "Buffer move left")
      "w J" '(buf-move-down :wk "Buffer move down")
      "w K" '(buf-move-up :wk "Buffer move up")
      "w L" '(buf-move-right :wk "Buffer move right"))

Fonts

Defining the various fonts that Emacs will use.

(set-face-attribute 'default nil
  :font "JetBrains Mono"
  :height 110
  :weight 'medium)
(set-face-attribute 'variable-pitch nil
  :font "Ubuntu"
  :height 120
  :weight 'medium)
(set-face-attribute 'fixed-pitch nil
  :font "JetBrains Mono"
  :height 110
  :weight 'medium)
;; Makes commented text and keywords italics.
;; This is working in emacsclient but not emacs.
;; Your font must have an italic face available.
(set-face-attribute 'font-lock-comment-face nil
  :slant 'italic)
(set-face-attribute 'font-lock-keyword-face nil
  :slant 'italic)

;; This sets the default font on all graphical frames created after restarting Emacs.
;; Does the same thing as 'set-face-attribute default' above, but emacsclient fonts
;; are not right unless I also add this method of setting the default font.
(add-to-list 'default-frame-alist '(font . "JetBrains Mono-11"))

;; Uncomment the following line if line spacing needs adjusting.
(setq-default line-spacing 0.12)

Zooming In/Out

You can use the bindings CTRL plus =/- for zooming in/out. You can also use CTRL plus the mouse wheel for zooming in/out.

(global-set-key (kbd "C-=") 'text-scale-increase)
(global-set-key (kbd "C--") 'text-scale-decrease)
(global-set-key (kbd "<C-wheel-up>") 'text-scale-increase)
(global-set-key (kbd "<C-wheel-down>") 'text-scale-decrease)

GRAPHICAL USER INTERFACE TWEAKS

Let's make GNU Emacs look a little better.

Disable Menubar, Toolbars and Scrollbars

(menu-bar-mode -1)
(tool-bar-mode -1)
(scroll-bar-mode -1)

Display Line Numbers and Truncated Lines

(global-display-line-numbers-mode 1)
(global-visual-line-mode t)

ORG MODE

Enabling Table of Contents

  (use-package toc-org
    :ensure t
      :commands toc-org-enable
      :init (add-hook 'org-mode-hook 'toc-org-enable))

Enabling Org Bullets

Org-bullets gives us attractive bullets rather than asterisks.

  (add-hook 'org-mode-hook 'org-indent-mode)
  (use-package org-bullets :ensure t)
  (add-hook 'org-mode-hook (lambda () (org-bullets-mode 1)))

Disable Electric Indent

Org mode source blocks have some really weird and annoying default indentation behavior. I think this has to do with electric-indent-mode, which is turned on by default in Emacs. So let's turn it OFF!

(electric-indent-mode -1)

Source Code Block Tag Expansion

Org-tempo is not a separate package but a module within org that can be enabled. Org-tempo allows for '<s' followed by TAB to expand to a begin_src tag. Other expansions available include:

Typing the below + TAB Expands to …
<a '#+BEGIN_EXPORT ascii' … '#+END_EXPORT
<c '#+BEGIN_CENTER' … '#+END_CENTER'
<C '#+BEGIN_COMMENT' … '#+END_COMMENT'
<e '#+BEGIN_EXAMPLE' … '#+END_EXAMPLE'
<E '#+BEGIN_EXPORT' … '#+END_EXPORT'
<h '#+BEGIN_EXPORT html' … '#+END_EXPORT'
<l '#+BEGIN_EXPORT latex' … '#+END_EXPORT'
<q '#+BEGIN_QUOTE' … '#+END_QUOTE'
<s '#+BEGIN_SRC' … '#+END_SRC'
<v '#+BEGIN_VERSE' … '#+END_VERSE'
(require 'org-tempo)

RAINBOW MODE

Display the actual color as a background for any hex color value (ex. #ffffff). The code block below enables rainbow-mode in all programming modes (prog-mode) as well as org-mode, which is why rainbow works in this document.

  (use-package rainbow-mode
    :ensure t
    :hook 
    ((org-mode prog-mode) . rainbow-mode))

SHELLS AND TERMINALS

Eshell

Eshell is an Emacs 'shell' that is written in Elisp.

  (use-package eshell-syntax-highlighting
    :ensure t
    :after esh-mode
    :config
    (eshell-syntax-highlighting-global-mode +1))

Vterm

Vterm is a terminal emulator within Emacs. The 'shell-file-name' setting sets the shell to be used in M-x shell, M-x term, M-x ansi-term and M-x vterm. By default, the shell is set to 'fish' but could change it to 'bash' or 'zsh' if you prefer.

  (use-package vterm
    :ensure t
  ;;:config
)

Vterm-Toggle

vterm-toggle toggles between the vterm buffer and whatever buffer you are editing.

  (use-package vterm-toggle
    :ensure t
    :after vterm
    :config
    (setq vterm-toggle-fullscreen-p nil)
    (setq vterm-toggle-scope 'project)
    (add-to-list 'display-buffer-alist
                 '((lambda (buffer-or-name _)
                       (let ((buffer (get-buffer buffer-or-name)))
                         (with-current-buffer buffer
                           (or (equal major-mode 'vterm-mode)
                               (string-prefix-p vterm-buffer-name (buffer-name buffer))))))
                    (display-buffer-reuse-window display-buffer-at-bottom)
                    ;;(display-buffer-reuse-window display-buffer-in-direction)
                    ;;display-buffer-in-direction/direction/dedicated is added in emacs27
                    ;;(direction . bottom)
                    ;;(dedicated . t) ;dedicated is supported in emacs27
                    (reusable-frames . visible)
                    (window-height . 0.3))))

SUDO EDIT

sudo-edit gives us the ability to open files with sudo privileges or switch over to editing with sudo privileges if we initially opened the file without such privileges.

  (use-package sudo-edit
    :ensure t
    :config
      (kylekrein/leader-keys
        "fu" '(sudo-edit-find-file :wk "Sudo find file")
        "fU" '(sudo-edit :wk "Sudo edit file")))

Nerd Icons

  (use-package nerd-icons
    :ensure t
    ;; :custom
    ;; The Nerd Font you want to use in GUI
    ;; "Symbols Nerd Font Mono" is the default and is recommended
    ;; but you can use any other Nerd Font if you want
    ;; (nerd-icons-font-family "Symbols Nerd Font Mono")
    )

Nerd Icons Completion

https://github.com/rainstormstudio/nerd-icons-completion

  (use-package nerd-icons-completion
    :ensure t
    :after marginalia
    :config
    (nerd-icons-completion-mode)
    (add-hook 'marginalia-mode-hook #'nerd-icons-completion-marginalia-setup))

Buffer Move

Creating some functions to allow us to easily move windows (splits) around. The following block of code was taken from buffer-move.el found on the EmacsWiki: https://www.emacswiki.org/emacs/buffer-move.el

(require 'windmove)

;;;###autoload
(defun buf-move-up ()
"Swap the current buffer and the buffer above the split.
If there is no split, ie now window above the current one, an
error is signaled."
;;  "Switches between the current buffer, and the buffer above the
;;  split, if possible."
(interactive)
(let* ((other-win (windmove-find-other-window 'up))
(buf-this-buf (window-buffer (selected-window))))
  (if (null other-win)
      (error "No window above this one")
    ;; swap top with this one
    (set-window-buffer (selected-window) (window-buffer other-win))
    ;; move this one to top
    (set-window-buffer other-win buf-this-buf)
    (select-window other-win))))

;;;###autoload
(defun buf-move-down ()
"Swap the current buffer and the buffer under the split.
If there is no split, ie now window under the current one, an
error is signaled."
(interactive)
(let* ((other-win (windmove-find-other-window 'down))
(buf-this-buf (window-buffer (selected-window))))
  (if (or (null other-win) 
          (string-match "^ \\*Minibuf" (buffer-name (window-buffer other-win))))
      (error "No window under this one")
    ;; swap top with this one
    (set-window-buffer (selected-window) (window-buffer other-win))
    ;; move this one to top
    (set-window-buffer other-win buf-this-buf)
    (select-window other-win))))

;;;###autoload
(defun buf-move-left ()
"Swap the current buffer and the buffer on the left of the split.
If there is no split, ie now window on the left of the current
one, an error is signaled."
(interactive)
(let* ((other-win (windmove-find-other-window 'left))
(buf-this-buf (window-buffer (selected-window))))
  (if (null other-win)
      (error "No left split")
    ;; swap top with this one
    (set-window-buffer (selected-window) (window-buffer other-win))
    ;; move this one to top
    (set-window-buffer other-win buf-this-buf)
    (select-window other-win))))

;;;###autoload
(defun buf-move-right ()
"Swap the current buffer and the buffer on the right of the split.
If there is no split, ie now window on the right of the current
one, an error is signaled."
(interactive)
(let* ((other-win (windmove-find-other-window 'right))
(buf-this-buf (window-buffer (selected-window))))
  (if (null other-win)
      (error "No right split")
    ;; swap top with this one
    (set-window-buffer (selected-window) (window-buffer other-win))
    ;; move this one to top
    (set-window-buffer other-win buf-this-buf)
    (select-window other-win))))

Vertico

Vertico provides a performant and minimalistic vertical completion UI based on the default completion system.

    ;; Enable vertico
    (use-package vertico
      :ensure t
      :custom
      ;; (vertico-scroll-margin 0) ;; Different scroll margin
      ;; (vertico-count 20) ;; Show more candidates
      ;; (vertico-resize t) ;; Grow and shrink the Vertico minibuffer
      ;; (vertico-cycle t) ;; Enable cycling for `vertico-next/previous'
      :init
      (vertico-mode))

  (vertico-mode t) ;; enable vertico for all buffers
    ;; Persist history over Emacs restarts. Vertico sorts by history position.
    (use-package savehist
      :ensure -1 ;;package is unavailable in nix
      :init
      (savehist-mode))

    ;; A few more useful configurations...
    (use-package emacs
      :custom
      ;; Support opening new minibuffers from inside existing minibuffers.
      (enable-recursive-minibuffers t)
      ;; Hide commands in M-x which do not work in the current mode.  Vertico
      ;; commands are hidden in normal buffers. This setting is useful beyond
      ;; Vertico.
      (read-extended-command-predicate #'command-completion-default-include-p)
      :init
      ;; Add prompt indicator to `completing-read-multiple'.
      ;; We display [CRM<separator>], e.g., [CRM,] if the separator is a comma.
      (defun crm-indicator (args)
        (cons (format "[CRM%s] %s"
                      (replace-regexp-in-string
                       "\\`\\[.*?]\\*\\|\\[.*?]\\*\\'" ""
                       crm-separator)
                      (car args))
              (cdr args)))
      (advice-add #'completing-read-multiple :filter-args #'crm-indicator)

      ;; Do not allow the cursor in the minibuffer prompt
      (setq minibuffer-prompt-properties
            '(read-only t cursor-intangible t face minibuffer-prompt))
      (add-hook 'minibuffer-setup-hook #'cursor-intangible-mode))
    
  ;; Optionally use the `orderless' completion style.
  (use-package orderless
    :ensure t
    :custom
    ;; Configure a custom style dispatcher (see the Consult wiki)
    ;; (orderless-style-dispatchers '(+orderless-consult-dispatch orderless-affix-dispatch))
    ;; (orderless-component-separator #'orderless-escapable-split-on-space)
    (completion-styles '(orderless basic))
    (completion-category-defaults nil)
    (completion-category-overrides '((file (styles partial-completion)))))

Descriptions

Marginalia

https://github.com/minad/marginalia/ Descriptions for completions

  ;; Enable rich annotations using the Marginalia package
  (use-package marginalia
    :ensure t
    ;; Bind `marginalia-cycle' locally in the minibuffer.  To make the binding
    ;; available in the *Completions* buffer, add it to the
    ;; `completion-list-mode-map'.
    :bind (:map minibuffer-local-map
           ("M-A" . marginalia-cycle))

    ;; The :init section is always executed.
    :init

    ;; Marginalia must be activated in the :init section of use-package such that
    ;; the mode gets enabled right away. Note that this forces loading the
    ;; package.
    (marginalia-mode))

Theme

Emacs Theme Editor

Theme loading

(use-package doom-themes
  :ensure t
  :config
  ;; Global settings (defaults)
  (setq doom-themes-enable-bold t    ; if nil, bold is universally disabled
        doom-themes-enable-italic t) ; if nil, italics is universally disabled
  (load-theme 'doom-one t)

  ;; Enable flashing mode-line on errors
  (doom-themes-visual-bell-config)
  ;; Enable custom neotree theme (nerd-icons must be installed!)
  (doom-themes-neotree-config)
  ;; or for treemacs users
  (setq doom-themes-treemacs-theme "doom-atom") ; use "doom-colors" for less minimal icon theme
  (doom-themes-treemacs-config)
  ;; Corrects (and improves) org-mode's native fontification.
  (doom-themes-org-config))

WHICH-KEY

  (use-package which-key
    :ensure t
    :init
      (which-key-mode 1)
    :config
    (setq which-key-side-window-location 'bottom
  	  which-key-sort-order #'which-key-key-order-alpha
  	  which-key-sort-uppercase-first nil
  	  which-key-add-column-padding 1
  	  which-key-max-display-columns nil
  	  which-key-min-display-lines 6
  	  which-key-side-window-slot -10
  	  which-key-side-window-max-height 0.25
  	  which-key-idle-delay 0.8
  	  which-key-max-description-length 25
  	  which-key-allow-imprecise-window-fit t
  	  which-key-separator " → " ))