1 #+title: =aminb='s Emacs Init file
2 #+property: header-args :results silent :comments link :tangle ~/dotfiles/emacs/init.el
6 This org file is tangled to [[./init.el][init.el]] and constitutes my Emacs
7 configuration. =straight.el=, =use-package=, =general.el=, =exwm=,
8 =org-mode=, and =magit= are some of the awesome packages powering my
11 * Contents :toc_1:noexport:
15 - [[#initial-setup][Initial setup]]
26 #+begin_src emacs-lisp :comments none
27 ;;; init.el --- Amin Bandali's Emacs config -*- lexical-binding: t ; eval: (view-mode 1)-*-
30 Enable =view-mode=, which both makes the file read-only (as a reminder
31 that =init.el= is an auto-generated file, not supposed to be edited),
32 and provides some convenient key bindings for browsing through the
37 #+begin_src emacs-lisp :comments none
38 ;; Copyright (C) 2018 Amin Bandali <amin@aminb.org>
40 ;; This program is free software: you can redistribute it and/or modify
41 ;; it under the terms of the GNU General Public License as published by
42 ;; the Free Software Foundation, either version 3 of the License, or
43 ;; (at your option) any later version.
45 ;; This program is distributed in the hope that it will be useful,
46 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
47 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
48 ;; GNU General Public License for more details.
50 ;; You should have received a copy of the GNU General Public License
51 ;; along with this program. If not, see <https://www.gnu.org/licenses/>.
56 #+begin_src emacs-lisp :comments none
59 ;; Emacs configuration of Amin Bandali, computer scientist and functional
62 ;; THIS FILE IS AUTO-GENERATED FROM `init.org'.
67 The conventions below were inspired by [[https://github.com/hlissner/doom-emacs][Doom]]'s conventions, found
68 [[https://github.com/hlissner/doom-emacs/blob/5dacbb7cb1c6ac246a9ccd15e6c4290def67757c/core/core.el#L3-L17][here]]. Naturally, I use my initials, =ab=, instead of =doom=.
70 #+begin_src emacs-lisp :comments none
71 ;; Naming conventions:
73 ;; ab-... public variables or non-interactive functions
74 ;; ab--... private anything (non-interactive), not safe for direct use
75 ;; ab/... an interactive function; safe for M-x or keybinding
76 ;; ab:... an evil operator, motion, or command
77 ;; ab|... a hook function
78 ;; ab*... an advising function
79 ;; ab@... a hydra command
85 :CUSTOM_ID: initial-setup
88 #+begin_src emacs-lisp :comments none
94 Measure and display startup time. Also, temporarily increase
95 ~gc-cons-threshhold~ during startup to reduce reduce garbage
96 collection frequency. Taken from [[https://github.com/dieggsy/dotfiles/tree/3d95bc08033920e077855caf545a975eba52d28d/emacs.d#startup-time][here]].
98 #+begin_src emacs-lisp
99 (defconst ab--emacs-start-time (current-time))
100 (defconst ab--gc-cons-threshold gc-cons-threshold)
101 (defconst ab--gc-cons-percentage gc-cons-percentage)
102 (defvar ab--file-name-handler-alist file-name-handler-alist)
103 (setq gc-cons-threshold 400000000
104 gc-cons-percentage 0.6
105 file-name-handler-alist nil
106 ;; sidesteps a bug when profiling with esup
107 esup-child-profile-require-level 0)
110 Reset the variables back to default after init.
112 #+begin_src emacs-lisp
116 (setq gc-cons-threshold ab--gc-cons-threshold
117 gc-cons-percentage ab--gc-cons-percentage
118 file-name-handler-alist ab--file-name-handler-alist)
119 (let ((elapsed (float-time (time-subtract (current-time)
120 ab--emacs-start-time))))
121 (message "Loading %s...done (%.3fs) [after-init]"
122 ,load-file-name elapsed))))
125 ** Package management
130 Next-generation, purely functional package manager for the Emacs
134 =straight.el= allows me to have a fully reproducible Emacs setup.
138 #+begin_src emacs-lisp
139 (let ((bootstrap-file (concat user-emacs-directory "straight/repos/straight.el/bootstrap.el"))
140 (bootstrap-version 3))
141 (unless (file-exists-p bootstrap-file)
143 (url-retrieve-synchronously
144 "https://raw.githubusercontent.com/raxod502/straight.el/develop/install.el"
145 'silent 'inhibit-cookies)
146 (goto-char (point-max))
147 (eval-print-last-sexp)))
148 (load bootstrap-file nil 'nomessage))
153 #+begin_src emacs-lisp
154 (defun ab/reload-init ()
157 (straight-transaction
158 (straight-mark-transaction-as-init)
159 (message "Reloading init.el...")
160 (load user-init-file nil 'nomessage)
161 (message "Reloading init.el... done.")))
163 (defun ab/eval-buffer ()
164 "Evaluate the current buffer as Elisp code."
166 (message "Evaluating %s..." (buffer-name))
167 (straight-transaction
168 (if (null buffer-file-name)
170 (when (string= buffer-file-name user-init-file)
171 (straight-mark-transaction-as-init))
172 (load-file buffer-file-name)))
173 (message "Evaluating %s... done." (buffer-name)))
179 A use-package declaration for simplifying your .emacs
182 =use-package= is an awesome utility for managing and configuring
183 packages in a neatly organized way and without compromising on
184 performance. So let's install it using =striaght.el= and have it use
185 =straight.el= for installing packages.
187 #+begin_src emacs-lisp
188 (straight-use-package 'use-package)
189 (setq straight-use-package-by-default t)
192 ** No littering in =~/.emacs.d=
195 Help keeping ~/.emacs.d clean
198 By default, even for Emacs' built-in packages, the configuration files
199 and persistent data are all over the place. Use =no-littering= to help
202 #+begin_src emacs-lisp
203 (use-package no-littering
207 (add-to-list 'savehist-additional-variables 'kill-ring)
209 (setq auto-save-file-name-transforms
210 `((".*" ,(no-littering-expand-var-file-name "auto-save/") t))))
213 ** Custom file (=custom.el=)
215 I'm not planning on using the custom file much, but even so, I
216 definitely don't want it mixing with =init.el=. So, here, let's give
219 #+begin_src emacs-lisp
220 (setq custom-file (no-littering-expand-etc-file-name "custom.el"))
221 (when (file-exists-p custom-file)
225 ** Better =$PATH= handling
227 Let's use [[https://github.com/purcell/exec-path-from-shell][exec-path-from-shell]] to make Emacs use the =$PATH= as set up
230 #+begin_src emacs-lisp
231 (use-package exec-path-from-shell
234 (setq exec-path-from-shell-check-startup-files nil)
236 (exec-path-from-shell-initialize)
237 ;; while we're at it, let's fix access to our running ssh-agent
238 (exec-path-from-shell-copy-env "SSH_AGENT_PID")
239 (exec-path-from-shell-copy-env "SSH_AUTH_SOCK"))
244 Start server if not already running. Alternatively, can be done by
245 issuing =emacs --daemon= in the terminal, which can be automated with
246 a systemd service or using =brew services start emacs= on macOS. I use
247 Emacs as my window manager (via =exwm=), so I always start Emacs on
248 login; so starting the server from inside Emacs is good enough for me.
250 See [[https://www.gnu.org/software/emacs/manual/html_node/emacs/Emacs-Server.html#Emacs-Server][Using Emacs as a Server]].
252 #+begin_src emacs-lisp
254 (unless (server-running-p)
265 *** Disable disabled commands
267 Emacs disables some commands by default that could persumably be
268 confusing for novice users. Let's disable that.
270 #+begin_src emacs-lisp
271 (setq disabled-command-function nil)
276 Save what I copy into clipboard from other applications into Emacs'
277 kill-ring, which would allow me to still be able to easily access it
278 in case I kill (cut or copy) something else inside Emacs before
279 yanking (pasting) what I'd originally intended to.
281 #+begin_src emacs-lisp
282 (setq save-interprogram-paste-before-kill t)
285 *** Keep more =*Messages*=
287 #+begin_src emacs-lisp
288 (setq message-log-max 10000)
293 #+begin_src emacs-lisp
294 (setq enable-recursive-minibuffers t
295 resize-mini-windows t)
298 *** Lazy-person-friendly yes/no prompts
300 Lazy people would prefer to type fewer keystrokes, especially for yes
301 or no questions. I'm lazy.
303 #+begin_src emacs-lisp
304 (defalias 'yes-or-no-p #'y-or-n-p)
309 Let's customize the =*scratch*= buffer a bit. First off, I don't need
312 #+begin_src emacs-lisp
313 (setq initial-scratch-message "")
316 Also, let's use Text mode as the major mode, in case I want to
317 customize it (=*scratch*='s default major mode, Fundamental mode,
318 can't really be customized).
320 #+begin_src emacs-lisp
321 (setq initial-major-mode 'text-mode)
324 *** More useful frame titles
326 Show either the file name or the buffer name (in case the buffer isn't
327 visiting a file). Borrowed from Emacs Prelude.
329 #+begin_src emacs-lisp
330 (setq frame-title-format
331 '("" invocation-name " - "
332 (:eval (if (buffer-file-name)
333 (abbreviate-file-name (buffer-file-name))
339 Emacs' default backup settings aren't that great. Let's use more
340 sensible options. See documentation for the ~make-backup-file~
343 #+begin_src emacs-lisp
344 (setq backup-by-copying t
350 The packages in this section are absolutely essential to my everyday
351 workflow, and they play key roles in how I do my computing. They
352 immensely enhance the Emacs experience for me; both using Emacs, and
355 *** [[https://github.com/noctuid/general.el][general.el]]
358 More convenient key definitions in emacs
361 More like /the most/ convenient key definitions in Emacs.
363 #+begin_src emacs-lisp
369 *** [[https://github.com/ch11ng/exwm][exwm]] (window manager)
371 #+begin_src emacs-lisp
374 (require 'exwm-config)
375 (exwm-config-default))
378 *** [[https://orgmode.org/][Org mode]]
381 Org mode is for keeping notes, maintaining TODO lists, planning
382 projects, and authoring documents with a fast and effective plain-text
386 In short, my favourite way of life.
388 #+begin_src emacs-lisp
389 (setq org-src-tab-acts-natively t
390 org-src-preserve-indentation nil
391 org-edit-src-content-indentation 0)
394 *** [[https://magit.vc/][Magit]]
397 It's Magit! A Git porcelain inside Emacs.
400 Not just how I do git, but /the/ way to do git.
402 #+begin_src emacs-lisp
405 ("s-g" 'magit-status))
408 *** [[https://github.com/abo-abo/swiper][Ivy]] (and friends)
411 Ivy - a generic completion frontend for Emacs, Swiper - isearch with
412 an overview, and more. Oh, man!
415 There's no way I could top that, so I won't attempt to.
419 #+begin_src emacs-lisp
423 [escape] 'keyboard-escape-quit
425 "C-k" 'ivy-previous-line
426 [S-up] 'ivy-previous-history-element
427 [S-down] 'ivy-next-history-element
428 "DEL" 'ivy-backward-delete-char)
435 #+begin_src emacs-lisp
437 :general ("C-s" 'swiper))
442 #+begin_src emacs-lisp
446 "C-x C-f" 'counsel-find-file
447 "s-r" 'counsel-recentf)
448 (imap minibuffer-local-map
449 "C-r" 'counsel-minibuffer-history)
452 (defalias 'locate #'counsel-locate))
460 #+begin_src emacs-lisp :comments none
461 ;;; init.el ends here