My Emacs Config
-
Emacs
-
Elisp
As you might have seen on my Tools page, I am an avid Emacs user, and am even using it to write this post! So I thought it would be fitting to go over my Emacs config, and a little bit about how it came to be the way it is today.
If I recall correctly, I actually first used Emacs back when I also used Windows, but I didn't end up getting that far into Emacs back then- I thought the keymaps were odd, and was more used to Vim, so I just used that instead. However at some point after switching to Linux, I got more into Emacs, and tinkered around with Evil Mode, Doom Emacs, and more. Nowadays, I just roll my own config.
Before You Read
Before we go into the config, I want to say that there are some packages used in this config that are not included in Emacs by default. They may have their own licenses and information needed to set them up- before installing any package, I strongly recommend you take at least a quick look at the source code of it. You should also note that this config is not plug and play- some of the required packages are not automatically installed in the setup. Some parts of the config have been left out for one reason or another.
The Early Init File
The early init file (early-init.el) is a file that is evaluated before the Emacs window is shown- and it is commonly used for makes changes to the UI itself. Here is my early init file:
(tool-bar-mode 0)
(scroll-bar-mode 0)
Yup, it's just two lines- it disables the tool bar and scroll bar modes. Don't get comfy though, it's about to get a whole lot more complicated.
The Init File
The init file (init.el) is were the majority of the config lies, starting with this line:
;; -*- lexical-binding: t -*-
To be honest I'm not really sure what this does. All I know is that it's something to do with how the Emacs evaluates the Elisp, and that it stops a Flymake warning.
Custom File
Now the code- first, I set the location for the custom file and load it.
(setopt custom-file (expand-file-name "custom.el" user-emacs-directory))
(load-file custom-file)
This may give an error if custom.el doesn't exist, so it's best to create an empty custom.el file and then let custom write to it afterwards. You don't have to set a separate file for changes made by custom of course, but I like to as it can help keep my init file a little cleaner.
Theme
Next, we load the theme. During the day I use the modus-operandi theme, and when it gets dark out (which happens quite often, and quite quickly where I live) I use the ef-elea-dark theme. You can find it in the ef-themes package.
(load-theme 'ef-elea-dark)
Modes
Now we finally have a section more then two lines long- mode activation.
(global-display-line-numbers-mode)
(global-hl-line-mode)
(global-eldoc-mode)
(global-diff-hl-mode)
(diff-hl-flydiff-mode)
(tab-bar-mode)
(recentf-mode)
(savehist-mode)
(vertico-mode)
(marginalia-mode)
(which-key-mode)
(electric-pair-mode)
(electric-indent-mode)
(context-menu-mode)
(column-number-mode)
(auto-save-visited-mode)
(winner-mode)
(windmove-default-keybindings)
This should be fairly self-explanatory- these are modes that I want activated by default.
Options
Next, we have this enormous setopt call.
(setopt blink-cursor-mode nil
create-lockfiles nil
ibuffer-show-empty-filter-groups nil
menu-bar-mode nil
indent-tabs-mode nil
;; t
auto-save-default t
backup-inhibited t
delete-by-moving-to-trash t
dired-isearch-filenames t
dired-kill-when-opening-new-dired-buffer t
global-auto-revert-non-file-buffers t
inhibit-startup-screen t
isearch-allow-motion t
isearch-lazy-count t
truncate-lines t
use-short-answers t
;; Numbers
auto-save-timeout 1
c-basic-offset 4
pgtk-wait-for-event-timeout 0.001
show-paren-delay 0.0
tab-width 4
;; Quotes
display-line-numbers-type 'relative
indent-line-function 'insert-tab
minimap-window-location 'right
ring-bell-function 'ignore
;; Strings
org-directory "~/Documents/Org"
org-agenda-files (directory-files-recursively org-directory "org")
;; Mode line
mode-line-format '("%e"
mode-line-front-space
mode-line-mule-info
mode-line-modified
mode-line-client
mode-line-frame-identification
mode-line-buffer-identification
mode-line-position
mode-line-format-right-align
(vc-mode vc-mode)
mode-line-modes
mode-line-misc-info
mode-line-end-spaces)
;; Ibuffer
ibuffer-saved-filter-groups
'(("Default"
("Dired" (mode . dired-mode))
("Programming" (or (mode . emacs-lisp-mode)
(mode . zig-mode)
(mode . rust-mode)
(mode . clojure-mode)
(mode . python-mode)
(mode . java-mode)
(mode . javascript-mode)
(mode . c-mode)
(mode . sh-mode)
(mode . c++-mode)
(mode . css-mode)
(mode . mhtml-mode)
(mode . xml-mode)))
("Config" (or (mode . conf-mode)
(mode . conf-toml-mode)))
("Org & Markdown" (or (mode . org-mode)
(mode . markdown-mode)))
("Terminal" (or (mode . eshell-mode)
(mode . term-mode)))
("Web Browser" (mode . eww-mode))
("Magit" (or (name . "magit")
(mode . magit-diff-mode)
(mode . magit-process-mode)))
("Emacs" (or (name . "scratch")
(name . "Messages")
(name . "GNU Emacs")
(name . "Help")
(name . "Customize Group")
(name . "Warnings"))))))
This sets general options I want- disabling cursor blinking, setting the tab width, etc. It also customizes the mode line via the mode-line-format variable, and something I learned recently is that you can add groups the the ibuffer menu via the ibuffer-saved-filter-groups variable, so I do that here as well. One more thing to note is that I inhibit the default startup screen, as I use my own custom one (more on that later).
Hooks
I have a few hooks set to make my Emacs life a little easier.
(add-hook 'eshell-mode-hook (corfu-mode -1))
(add-hook 'prog-mode-hook (lambda ()
(hs-minor-mode 1)
(outline-minor-mode 1)))
(add-hook 'ibuffer-mode-hook (lambda () (ibuffer-switch-to-saved-filter-groups "Default")))
(add-hook 'after-save-hook #'(lambda () '(eglot-managed-p)) ; Still uncertain about how sharp quoting works, hopefully this is okay?
'(eglot-format-buffer))
(defun cyn/simple-focus ()
"Activates/changes some toggles/modes for simpler, more focused writing."
(setq-local
truncate-lines nil
word-wrap 1
fill-column 120)
(visual-fill-column-mode 1)
(fringe-mode 0)
(call-interactively 'hl-line-mode)
(display-line-numbers-mode -1))
(add-hook 'org-mode-hook 'cyn/simple-focus)
(add-hook 'markdown-mode-hook 'cyn/simple-focus)
Lists
The add-to-list function is useful for a lot of things, and here I use it to add Homebrew programs to the Emacs executable path, setup some file associations, setup some LSPs, etc.
(add-to-list 'exec-path "/home/cyn/Documents/Apps/zls-x86_64-linux/")
(add-to-list 'exec-path "/home/cyn/.cargo/bin")
(add-to-list 'exec-path "/home/linuxbrew/.linuxbrew/bin/")
(add-to-list 'ispell-skip-region-alist '("```" . "```"))
(add-to-list 'ispell-skip-region-alist '("`" . "`"))
(add-to-list 'ispell-skip-region-alist '("<br" . "/>"))
(add-to-list 'default-frame-alist '(internal-border-width . 16))
(with-eval-after-load 'eglot
(add-to-list 'eglot-server-programs '(python-mode . ("ty" "server"))))
Use Package Declarations
The use-package macro is likely familiar to you if you're an Emacs user, it is super useful for declarative package management.
(use-package corfu
:init
(global-corfu-mode)
(corfu-popupinfo-mode)
(corfu-indexed-mode)
:config
(setopt corfu-cycle t
corfu-auto t
corfu-auto-delay 0.0
corfu-preselect 'prompt
corfu-auto-prefix 2
corfu-quit-no-match 'separator)
:bind (:map corfu-map
("<tab>" . 'corfu-next)
("<backtab>" . 'corfu-previous)))
(use-package orderless
:custom
(completion-styles '(orderless basic))
(completion-category overrides '((file (styles partial-completion)))))
(use-package indent-bars
:hook(prog-mode . indent-bars-mode))
(use-package tetherdir
:vc (tetherdir :url https://codeberg.org/Cyncrovee/Tetherdir.el))
(use-package rainbow-delimiters
:hook (prog-mode . rainbow-delimiters-mode))
(use-package eldoc-mouse
:hook eldoc-mode)
(use-package multiple-cursors
:config
(setopt mc/always-run-for-all t))
I even sneaked my Tetherdir package in there!
Functions
Here I have some custom functions for working with lines- I find these very useful.
(defun cyn/mark-line ()
"Put the current line within the region."
(interactive)
(move-beginning-of-line nil)
(forward-line 1)
(set-mark-command nil)
(forward-line -1))
(defun cyn/copy-line ()
"Mark and copy the current line."
(interactive)
(save-excursion
(cyn/mark-line)
(call-interactively 'kill-ring-save)))
(defun cyn/copy-line-and-yank (&optional ARG)
"Select and copy the current line, then yank it ARG times.
(defun cyn/init-file-jump ()
"Convenience function to go to the init file."
(interactive)
(find-file user-init-file))
(defun cyn/delete-other-window ()
"Call `other-window' with an argument of 1, then call `delete-window'.
This is similar to delete-other-windows', however it only deletes *one* window, said window is decided by other-window'."
(interactive)
(other-window 1)
(delete-window))
(defun cyn/increment-or-decrement-number-at-point (&optional is-negative)
"Increment/decrement the single-character number at point by one.
If the optional argument IS-NEGATIVE is non-nil, decrement the number.
Otherwise, increment it."
(let ((current-char (char-after)))
(when (looking-at "[0-9]")
(let* ((new-int-function (if is-negative
'-
'+))
(new-int-string
(int-to-string (funcall new-int-function
(string-to-number (char-to-string current-char))
1))))
(when (= (string-width new-int-string) 1)
(delete-char 1)
(insert new-int-string)
(backward-char))))))
(defun cyn/increment-number-at-point ()
"Increment the single-character number at point by one.
Calls `cyn/increment-or-decrement-number-at-point' without the optional
argument, incrementing the number at point if
that number is a single character."
(interactive)
(cyn/increment-or-decrement-number-at-point))
(defun cyn/decrement-number-at-point ()
"Decrement the single-character number at point by one.
Calls `cyn/increment-or-decrement-number-at-point' with the optional
argument, decrementing the number at point if
that number is a single character."
(interactive)
(cyn/increment-or-decrement-number-at-point t))
(defun cyn/delete-all-buffers-except-current ()
"Delete all buffers except the current one."
(interactive)
(dolist (buf (buffer-list))
(when (not (equal buf (current-buffer)))
(kill-buffer buf))))
Keymaps
What would an Emacs config be without some custom keymaps? The following are global keymaps:
(keymap-global-set "C-+" 'er/mark-inside-pairs)
(keymap-global-set "C-=" 'er/expand-region)
(keymap-global-set "M-=" 'er/contract-region)
(keymap-global-set "C-x b" 'consult-buffer)
(keymap-global-set "C-c b" 'ibuffer)
(keymap-global-set "C-c j" 'join-line)
(keymap-global-set "C-c p" 'list-processes)
(keymap-global-set "C-c c b" 'consult-buffer)
(keymap-global-set "C-c c f" 'consult-find)
(keymap-global-set "C-c c f" 'consult-flymake)
(keymap-global-set "C-c c g" 'consult-grep)
(keymap-global-set "C-c c i" 'consult-imenu)
(keymap-global-set "C-c c k" 'consult-bookmark)
(keymap-global-set "C-c c l" 'consult-line)
(keymap-global-set "C-c c m" 'consult-man)
(keymap-global-set "C-c c o" 'consult-outline)
(keymap-global-set "C-c c p" 'consult-project-buffer)
(keymap-global-set "C-c c r" 'consult-org-agenda)
(keymap-global-set "C-c c t" 'consult-goto-line)
(keymap-global-set "C-c c u" 'consult-imenu-multi)
(keymap-global-set "C-<" 'mc/mark-previous-like-this)
(keymap-global-set "C->" 'mc/mark-next-like-this)
(keymap-global-set "C-c l c" 'cyn/copy-line)
(keymap-global-set "C-c l e" 'mc/edit-lines)
(keymap-global-set "C-c l m" 'cyn/mark-line)
(keymap-global-set "C-c l p" 'cyn/copy-line-and-yank)
(keymap-global-set "C-c l r" 'mc/mark-all-in-region)
(keymap-global-set "C-c l s" 'sort-lines)
(keymap-global-set "C-c f f" 'flymake-mode)
(keymap-global-set "C-c e a" 'eglot-code-actions)
(keymap-global-set "C-c e e" 'eglot)
(keymap-global-set "C-c e f" 'eglot-format)
(keymap-global-set "C-c e l" 'eldoc)
(keymap-global-set "C-c e r" 'eglot-rename)
(keymap-global-set "C-c e s" 'eglot-shutdown-all)
(keymap-global-set "C-c g P" 'magit-push)
(keymap-global-set "C-c g f" 'magit-fetch)
(keymap-global-set "C-c g g" 'magit)
(keymap-global-set "C-c g l" 'magit-log)
(keymap-global-set "C-c g p" 'magit-pull)
(keymap-global-set "C-c a a" 'embark-act)
(keymap-global-set "C-c a c" 'comment-or-uncomment-region)
(keymap-global-set "M-<left>" 'previous-buffer)
(keymap-global-set "M-<right>" 'next-buffer)
(keymap-global-set "M-_" 'undo-only)
(keymap-global-set "M-o" 'other-window)
(keymap-global-set "C-M-+" 'cyn/increment-number-at-point)
(keymap-global-set "C-M--" 'cyn/decrement-number-at-point)
(keymap-global-set "<f5>" 'menu-bar-mode)
(keymap-global-set "<f6>" 'tool-bar-mode)
(keymap-global-set "<f7>" 'scroll-bar-mode)
(keymap-global-set "<f8>" 'minimap-mode)
(keymap-global-set "<f9>" 'tab-bar-mode)
Fair warning- some of these keymaps require external packages to use.
Startup Screen
I have a custom startup screen for my Emacs, It's really useful for quick access to my bookmarks and projects.
(defun cyn/startup-screen ()
"Create/open, clear, and setup the custom startup screen."
(interactive)
(require 'bookmark)
(require 'project)
;; Create, switch to, and do initial setup for the buffer
(let ((start-buffer (get-buffer-create "Startup")))
(switch-to-buffer start-buffer)
(read-only-mode -1)
(text-scale-set 2)
(erase-buffer)
(insert (propertize "Welcome Back to Emacs, Cyn!"
'face '(:height 2.5)))
;; We're using insert instead of newline, to avoid using the properties of the previous insertion
(insert "\n\n")
;; Bookmarks list
(insert (propertize "Jump to Bookmark:"
'face '(:height 1.5)))
(newline)
(let ((bmark-num 0))
(dolist (bm-name (bookmark-all-names))
(when (not (equal 10 bmark-num))
(insert-text-button
(concat "- " bm-name)
'action (lambda (_button)
(bookmark-jump bm-name)))
(newline)
(setq bmark-num (+ bmark-num 1)))))
(newline)
(insert (propertize "Jump to Project:"
'face '(:height 1.5)))
(newline)
;; Projects list
(let ((proj-num 0))
(dolist (proj-dir (project-known-project-roots))
(when (not (equal 5 proj-num))
(insert-text-button
(concat "- " proj-dir)
'action (lambda (_button)
(project-switch-project proj-dir)))
(newline)
(setq proj-num (+ proj-num 1)))))
;; Ending setup
(goto-char (point-min))
(read-only-mode 1)))
The above code just sets up the function- after that it needs to be called.
(cyn/startup-screen)
Closing
After that, the config ends. I used to split my config up into more files, and/or write it via an Org file, but I don't really do that anymore- I find it easier to manage with fewer files, and some of the Elisp tooling works better in a plain Elisp file.