Emacs Config
This is my public Emacs configuration.
General
Initialization
(setq custom-file (locate-user-emacs-file "custom.el"))
;; create custom.el if it does not exists.
(unless (file-exists-p custom-file)
(write-region "" nil custom-file))
(load custom-file)
;; package
(setq package-enable-at-startup nil)
;; (defvar bootstrap-version)
;; (let ((bootstrap-file
;; (expand-file-name "straight/repos/straight.el/bootstrap.el" user-emacs-directory))
;; (bootstrap-version 6))
;; (unless (file-exists-p bootstrap-file)
;; (with-current-buffer
;; (url-retrieve-synchronously
;; "https://raw.githubusercontent.com/radian-software/straight.el/develop/install.el"
;; 'silent 'inhibit-cookies)
;; (goto-char (point-max))
;; (eval-print-last-sexp)))
;; (load bootstrap-file nil 'nomessage))
;; (straight-use-package 'use-package)
;; MELPA package
(require 'package)
(add-to-list 'package-archives '("melpa" . "https://melpa.org/packages/") t)
;; (setq package-archives '(("melpa" . "https://melpa.org/packages/")))
(package-initialize)
;; Install 'use-package if not installed.
(unless (package-installed-p 'use-package)
(package-refresh-contents)
(package-install 'use-package))
;; For use-package keywords:
;; https://jwiegley.github.io/use-package/keywords/
(eval-when-compile (require 'use-package))
;; Ensure for all packages
(require 'use-package-ensure)
(setq use-package-always-ensure t)
Startup
;; Increase the garbage collection threshold to 100MB to reduced startup time.
;; See https://www.reddit.com/r/emacs/comments/3kqt6e
(setq gc-cons-threshold (* 1024 1024 100))
;; Turn off mouse interface early in startup to avoid momentary display.
(menu-bar-mode -1)
(tooltip-mode -1)
;; On Emacs without X, tool-bar-mode and scroll-bar-mode are not defined.
(when (fboundp 'tool-bar-mode)
(tool-bar-mode -1))
(when (fboundp 'scroll-bar-mode)
(scroll-bar-mode -1))
;; Show column number in the mode line.
(column-number-mode)
;; Inhibit the startup screen and shorten the scratch buffer's message.
(setq inhibit-startup-screen t)
(setq initial-scratch-message ";; scratch buffer\n\n")
Hydra File Menu
A hydra menu to open common files.
Also see https://github.com/abo-abo/hydra/wiki/Hydras-by-Topic.
(use-package hydra
:commands defhydra
:init
(defhydra hydra-file-menu (:hint nil
:foreign-keys warn
:exit t
:pre (setq which-key-inhibit t)
:post (setq which-key-inhibit nil))
"
^Emacs^ Org ^Blog^
^-----^------------------------------------------------
_er_: reload i: inbox.org _bb_: drafts.org
_ed_: dotemacs.org t: task.org _bp_: posts.org
_ep_: private.org d: diary.org
_el_: local.org n: note.org
"
("er" (load-file user-init-file))
("ed" (let ((vc-follow-symlinks t)) (find-file (locate-user-emacs-file "dotemacs.org"))))
("ep" (let ((vc-follow-symlinks t)) (find-file (locate-user-emacs-file "private.org"))))
("el" (let ((vc-follow-symlinks t)) (find-file (locate-user-emacs-file (concat "local-" (system-name) ".org")))))
("i" (find-file (expand-file-name "inbox.org" org-directory)))
("t" (find-file (expand-file-name "task.org" org-directory)))
("d" (find-file (expand-file-name "diary.org" org-directory)))
("n" (find-file (expand-file-name "note.org" org-directory)))
("bb" (find-file (expand-file-name "drafts.org" org-directory)))
("bp" (find-file "~/sync/blog/org-content/posts.org"))
("q" nil "quit menu" :color blue))
(global-set-key (kbd "C-c e") 'hydra-file-menu/body))
Quitting
A popular recommendation is to bind <escape>
with keyboard-escape-quit
. However, it closes all the other windows at the same time and it is annoying to me. Instead, I map <escape>
to keyboard-quit
.
;; Don't forget to handle minibuffer with "minibuffer-keyboard-quit".
(global-set-key (kbd "<escape>") 'keyboard-quit)
;; Avoid entering the ~repeat-complex-cammand~ when quiting everything with ~C-x~.
(global-unset-key (kbd "C-x <escape> <escape>")) ; repeat-complex-command
An even better way is to build muscle memory to use the standard C-g
to “quit” and C-]
to “abort”.
Backups
I don’t use backup files often as I use git to manage most of my org files. However, I still feel safer when having a backup. I follow the backup configuration from Sacha Chua to enable Emacs’s backups aggressively.
;; Save backup files to a dedicated directory.
(setq backup-directory-alist '(("." . "~/.emacs.d/backups")))
(setq delete-old-versions -1)
;; Make numeric backup versions unconditionally.
(setq version-control t)
(setq vc-make-backup-files t)
(setq auto-save-file-name-transforms '((".*" "~/.emacs.d/auto-save-list/" t)))
;; Do not create lock files.
(setq create-lockfiles nil)
Disable Native Compilation Warnings
(setq native-comp-async-report-warnings-errors nil)
Auto Tangle
Add #+auto_tangle: t
to tangle a file on save.
(use-package org-auto-tangle
:defer t
:hook (org-mode . org-auto-tangle-mode))
Miscellaneous
;; Use year/month/day
(setq calendar-date-style 'iso)
(xterm-mouse-mode +1)
;; Remember and restore the last cursor location of opened files
(save-place-mode 1)
;; Don't pop up UI dialogs when prompting
(setq use-dialog-box nil)
;; Disable the alarm bell (https://www.emacswiki.org/emacs/AlarmBell).
(setq ring-bell-function 'ignore)
;; Use shorter "y" or "n" to confirm killing emacs.
(setq confirm-kill-emacs 'y-or-n-p)
Editing
Remove Key Bindings
I disable most Emacs’s default keybinding because I am not able to get my muscle memory to work with it. Even if I do, that muscle memory probably will cause trouble when editing in other applications.
;; Disabe some Emacs's default keybindings.
(global-unset-key (kbd "C-v")) ; scroll-up-command
(global-unset-key (kbd "M-v")) ; scroll-down-command
(global-unset-key (kbd "C-n")) ; next line
(global-unset-key (kbd "C-p")) ; previous line
(global-unset-key (kbd "C-b")) ; previous char
;; (global-unset-key (kbd "C-f")) ; next char
(global-unset-key (kbd "C-t")) ; switch char
(global-unset-key (kbd "C-l")) ; recenter
(global-unset-key (kbd "C-j")) ; (electric-newline-and-maybe-indent)
(global-unset-key (kbd "M-l")) ; downcase-word
(global-unset-key (kbd "M-u")) ; upcase-wrod
;; Disable the "numeric argument". When used, I use the =C-u= prefix.
(dolist (prefix '("M-" "C-M-"))
(global-unset-key (kbd (concat prefix "-")))
(dotimes (i 10)
(global-unset-key (kbd (concat prefix (number-to-string i))))))
Text Mode and Prog Mode
I defined my own my/text-mode
and my/prog-mode
which enable several minor modes and key bindings.
I avoid global settings like global-display-line-numbers-mode
and only enable stuff when I need to, like in prog-mode
. I use global settings only if that is globally applicable everywhere.
(defun my/edit-mode ()
(visual-line-mode +1) ; enable "word-wrap"
(toggle-truncate-lines -1)
;; (hl-line-mode +1)
(local-set-key (kbd "C-S-s") 'write-file)
(setq cursor-type 'bar)
(setq show-trailing-whitespace t))
(defun my/text-mode ()
(my/edit-mode)
(goto-address-mode +1)
(flyspell-mode))
(defun my/prog-mode ()
(display-line-numbers-mode +1)
(my/edit-mode)
(show-paren-mode +1)
(goto-address-prog-mode +1)
(flyspell-prog-mode))
(add-hook 'text-mode-hook 'my/text-mode)
(add-hook 'prog-mode-hook 'my/prog-mode)
(add-hook 'conf-mode-hook 'my/prog-mode)
;; Removes the overlay properties which flyspell uses on incorrect words for mouse operations.
;; https://emacs.stackexchange.com/a/55708
(defun make-flyspell-overlay-return-mouse-stuff (overlay)
(overlay-put overlay 'help-echo nil)
(overlay-put overlay 'keymap nil)
(overlay-put overlay 'mouse-face nil))
(advice-add 'make-flyspell-overlay :filter-return #'make-flyspell-overlay-return-mouse-stuff)
Delete Trailing Whitespaces Except Current Line
I hook this function with my/save-buffer
.
;; Remove trailing whitespace except current line.
;; https://stackoverflow.com/a/35781486/1747877
(defun my/delete-trailing-whitespace-except-current-line ()
"Delete trailing whitespace in the whole buffer, except on the current line.
The current line exception is because we do want to remove any whitespace
on the current line on saving the file (`before-save-hook') while we are
in-between typing something.
Do not do anything if `do-not-delete-trailing-whitespace' is non-nil."
(interactive)
(when (not (bound-and-true-p do-not-delete-trailing-whitespace))
(delete-trailing-whitespace (point-min) (line-beginning-position))
(delete-trailing-whitespace (line-end-position) (point-max))))
(add-hook 'before-save-hook #'my/delete-trailing-whitespace-except-current-line)
Ignore save-buffer
unless visiting a file
See my blog post.
(defun my/save-buffer (&optional arg)
"Like `save-buffer', but does nothing if buffer is not visiting a file."
(interactive "p")
(unless (or (buffer-file-name) ; regular buffer
(buffer-file-name (buffer-base-buffer))) ; indirect buffer
(user-error "Use 'M-x save-buffer' to save this buffer."))
(progn
(my/delete-trailing-whitespace-except-current-line)
(save-buffer arg)))
(global-set-key [remap save-buffer] #'my/save-buffer)
Auto Save and Auto Revert
Auto-save and auto-revert cause less conflict when editing files synchronized on multiple computers.
;; Auto save buffer if idled for 2 seconds.
(setq auto-save-timeout 2)
(auto-save-visited-mode 1)
;; Watch and reload the file changed on the disk.
(setq auto-revert-remote-files t)
(global-auto-revert-mode 1)
;; Use a timer instead of file notification.
;;
;; File notification does not work well on Emacs 29.1.2.
;; Error: "file-notify--call-handler: Symbol’s function definition is void: nil"
(setq auto-revert-use-notify nil
auto-revert-interval 5)
Modern Editor Behavior
These configurations modernize Emacs.
;; Delete the selected text first before editing.
(delete-selection-mode +1)
;; Mouse middle-click yanks where the point is, not where the mouse is.
(setq mouse-yank-at-point t)
Smooth Scrolling
The default Emacs scrolling behavior is really weird and different from other modern editors. However, the result is still not ideal. We might need to wait for the pixel-based scrolling coming with Emacs 29.
(if (fboundp 'pixel-scroll-precision-mode)
(pixel-scroll-precision-mode 1)
;; Smooth Scrolling: https://www.emacswiki.org/emacs/SmoothScrolling
(setq scroll-conservatively 10000
scroll-step 1))
Moving Lines
Moving lines up and down are very common editing operations to me. This stackoverflow entry has more fancy answers but these two are exactly what I need.
;; move line up
(defun my/move-line-up ()
(interactive)
(transpose-lines 1)
(previous-line 2))
(global-set-key [(meta shift up)] 'my/move-line-up)
;; move line down
(defun my/move-line-down ()
(interactive)
(next-line 1)
(transpose-lines 1)
(previous-line 1))
(global-set-key [(meta shift down)] 'my/move-line-down)
- try drag-stuff.el
Sentence End with Single Space
By default, Emacs treat a period followed by double spaces as the end of sentence. This is old-fashioned and uncommon now.
(setq sentence-end-double-space nil)
Indent with Space
(setq-default indent-tabs-mode nil)
Rename File and Buffer Together
See https://emacs.readthedocs.io/en/latest/file_management.html.
;; rename-visited-file is introduced in Emacs 29.
(unless (fboundp 'rename-visited-file)
(defun rename-visited-file ()
"Renames the current buffer and the file it is visiting."
(interactive)
(let ((name (buffer-name))
(filename (buffer-file-name)))
(if (not (and filename (file-exists-p filename)))
(error "Buffer '%s' is not visiting a file!" name)
(let ((new-name (read-file-name "New name: " filename)))
(if (get-buffer new-name)
(error "A buffer named '%s' already exists!" new-name)
(rename-file filename new-name 1)
(rename-buffer new-name)
(set-visited-file-name new-name)
(set-buffer-modified-p nil)
(message "File '%s' successfully renamed to '%s'"
name (file-name-nondirectory new-name))))))))
(global-set-key (kbd "C-x R") 'rename-visited-file)
Delete File and Buffer Together
See http://emacsredux.com/blog/2013/04/03/delete-file-and-buffer/. If you like this command, it worth taking a look at crux package as well for similar useful collection.
(defun my/delete-file-and-buffer ()
"Kills the current buffer and deletes the file it is visiting."
(interactive)
(let ((filename (buffer-file-name)))
(if filename
(if (y-or-n-p (concat "Do you really want to delete file " filename " ?"))
(progn
(delete-file filename)
(message "Deleted file %s." filename)
(kill-buffer)))
(message "Not a file visiting buffer!"))))
(global-set-key (kbd "C-x K") 'my/delete-file-and-buffer)
Edit File as Root
(defun my/sudo-find-file (file-name)
"Like find file, but opens the file as root."
(interactive "FSudo Find File: ")
(let ((tramp-file-name (concat "/sudo::" (expand-file-name file-name))))
(find-file tramp-file-name)))
Insert Snippets
Very often, I want to insert today’s date. There are many ways of doing this.
(defun my/insert-current-date () (interactive)
(insert (shell-command-to-string "echo -n $(date +%Y-%m-%d)")))
(defhydra my/hydra-snippets (:hint nil
:foreign-keys warn
:exit t)
"Snippets"
("d" (my/insert-current-date) "Current Date (yyyy-mm-dd)")
("q" nil "Quit Menu"))
(global-set-key (kbd "C-c s") 'my/hydra-snippets/body)
Tramp
I found Tramp could be spammy to the minibuffer when saving file.
For example, these logs are printed to the minibuffer when saving a file.
Tramp: Encoding local file ‘/tmp/tramp.JniPQo.org’ using ‘base64-encode-region’…done Tramp: Decoding remote file ‘/ssh:<user>@<host>:/path/to/test.org’ using ‘base64 -d -
Set tramp-verbose
to 2
or lower to avoid showing the “connection to remote hosts” level message in minibuffer.
;; for Emacs 28 or above
(setq tramp-verbose 2)
;; for Emacs 27 or below
(setq tramp-message-show-message nil)
Case-sensitive replace-string
M-x replace-string
preserves case in each match if case-replace
(preserve case) and case-fold-search
(ignore case) are non-nil. The
latter makes replace-string
case-insensitive.
I prefer case-sensitive replace-string
and apply the customization
from https://stackoverflow.com/a/5346216.
(defun with-case-fold-search (orig-fun &rest args)
(let ((case-fold-search nil))
(apply orig-fun args)))
(advice-add 'replace-string :around #'with-case-fold-search)
Appearance
Modus Theme
Note that there is a command M-x modus-themes-toggle
to toggle the dark and light modus theme. Thanks to this video. I found the Modus themes is less buggy than doom theme, with the which-key
package.
(use-package modus-themes
:init
(setq modus-themes-to-toggle '(modus-operandi modus-vivendi-tinted))
(setq modus-themes-org-blocks 'gray-background)
(setq modus-themes-headings
'((agenda-date . (1.2))))
(setq modus-themes-common-palette-overrides
'((fg-heading-1 blue-cooler)
(fg-heading-2 blue)
(fg-heading-3 blue-warmer)))
(load-theme 'modus-vivendi-tinted :no-confirm)
:bind ("<f5>" . modus-themes-toggle))
Doom Modeline
I don’t use Doom Emacs but I do use its modeline.
;; https://github.com/seagle0128/doom-modeline
(use-package doom-modeline
:init
(doom-modeline-mode +1))
;; Doom modeline needs all-the-icons.
(use-package all-the-icons)
Window Management
Winner Mode
Winner Mode is a global minor mode. When activated, it allows you to “undo” (and “redo”) changes in the window configuration with the key commands C-c left
and C-c right
.
(use-package winner
:init
(winner-mode +1))
Window Navigation
(use-package windmove
:bind
("M-s-<up>" . 'windmove-up)
("M-s-<down>" . 'windmove-down)
("M-s-<left>" . 'windmove-left)
("M-s-<right>" . 'windmove-right))
(use-package ace-window
:bind
("M-o" . 'ace-window)
:custom
(aw-keys '(?a ?s ?d ?f ?g ?h ?j ?k ?l))
(aw-scope 'frame))
Window Dedication
(defun my/toggle-current-window-dedication ()
(interactive)
(let* ((window (selected-window))
(dedicated (window-dedicated-p window)))
(set-window-dedicated-p window (not dedicated))
(message "Window %sdedicated to %s"
(if dedicated "no longer " "")
(buffer-name))))
(global-set-key (kbd "<f11>") 'my/toggle-current-window-dedication)
Focus to new Window after Split
I found it is more nature to focus to the new window after splitting.
(global-set-key (kbd "C-x 2")
(lambda ()
(interactive)
(split-window-vertically)
(other-window 1)))
(global-set-key (kbd "C-x 3")
(lambda ()
(interactive)
(split-window-horizontally)
(other-window 1)))
Tab Bar Mode
I use the tab bar mode to keep important buffers visible. For example, I create a new tab for a frequently used buffer so that I can switch to it real quick.
Note that it is common to use tab bar mode with desktop mode to restore the window configuration. However, I am not using that.
(use-package emacs
:config
(setq tab-bar-close-button-show nil)
(setq tab-bar-new-button-show nil)
;; hide the tab bar when only one tab. Looks better especially when editting a file with emacsclient.
(setq tab-bar-show 1)
;; Show the absolute number of the tab
(setq tab-bar-tab-hints t)
;; Switch to tab by C-?
(setq tab-bar-select-tab-modifiers '(control))
(tab-bar-mode +1)
:bind
("C-<tab>" . tab-next))
Discoverability
Completion with vertico
(use-package vertico
:init
(vertico-mode)
(setq vertico-count 25)
:bind
(:map vertico-map
([escape] . minibuffer-keyboard-quit)
("<prior>" . vertico-scroll-down)
("<next>" . vertico-scroll-up)))
;; Persist history over Emacs restarts. Vertico sorts by history position.
(use-package savehist
:init
(savehist-mode))
;; A few more useful configurations...
(use-package emacs
: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)
;; Emacs 28: Hide commands in M-x which do not work in the current mode.
;; Vertico commands are hidden in normal buffers.
(setq read-extended-command-predicate
#'command-completion-default-include-p)
;; Enable recursive minibuffers
(setq enable-recursive-minibuffers t))
;; Optionally use the `orderless' completion style.
(use-package orderless
:init
;; Configure a custom style dispatcher (see the Consult wiki)
;; (setq orderless-style-dispatchers '(+orderless-dispatch)
;; orderless-component-separator #'orderless-escapable-split-on-space)
(setq completion-styles '(orderless basic)
completion-category-defaults nil
completion-category-overrides '((file (styles partial-completion)))))
Consult
(use-package consult
:bind
;; ("C-f" . consult-line)
("C-s" . consult-line)
("C-b" . consult-bookmark)
("C-j" . consult-imenu)
:init
(with-eval-after-load "org"
(define-key org-mode-map (kbd "C-j") #'consult-org-heading)))
Embark & Marginalia
(use-package marginalia
:init
(setq marginalia-align 'center)
(marginalia-mode))
(use-package embark
:bind
(("C-." . embark-act) ;; pick some comfortable binding
("C-;" . embark-dwim) ;; good alternative: M-.
("C-h B" . embark-bindings)) ;; alternative for "describe-bindings"
:init
;; Optionally replace the key help with a completing-read interface
(setq prefix-help-command #'embark-prefix-help-command)
:config
;; Hide the mode line of the Embark live/completions buffers
(add-to-list 'display-buffer-alist
'("\\`\\*Embark Collect \\(Live\\|Completions\\)\\*"
nil
(window-parameters (mode-line-format . none)))))
;; Consult users will also want the embark-consult package.
(use-package embark-consult
:hook
(embark-collect-mode . consult-preview-at-point-mode))
Hydra Consult Menu
Create a hydra menu for the consult commands I use frequently.
;; Hydra menu to for consult.
(defhydra hydra-consult-menu (:hint nil
:foreign-keys warn
:exit t
:pre (setq which-key-inhibit t)
:post (setq which-key-inhibit nil))
"===== Consult Menu (F12) =====\n"
("f" (my/consult-org-agenda) "Agenda")
("r" (consult-recent-file) "Recentf")
("q" nil "quit menu" :color blue))
(global-set-key (kbd "<f8>") 'hydra-consult-menu/body)
which-key
which-key displays the key bindings following your currently entered incomplete command (a prefix).
(use-package which-key
:custom
; (which-key-idle-delay 10000 "Set idle delay to infinite so it never trigger automatically")
; (which-key-show-early-on-C-h t "Allow C-h to trigger which-key before it is done automatically.")
; (which-key-idle-secondary-delay 0.05)
(which-key-mode +1 "Non-nil if which-Key mode is enabled"))
Org-mode
Org Files
;; Adding a "/" so that =find-file= finds the files under =~/org/=.
(setq org-directory "~/org/")
(defun my/expand-org-file-name (filename)
(expand-file-name filename org-directory))
(setq my/org-inbox-file (my/expand-org-file-name "inbox.org")
my/org-task-file (my/expand-org-file-name "task.org")
my/org-task-archive-file (my/expand-org-file-name "task.org_archive")
my/org-note-file (my/expand-org-file-name "note.org")
my/org-checklist-file (my/expand-org-file-name "checklist.org")
my/org-review-file (my/expand-org-file-name "review.org")
my/org-vocab-file (my/expand-org-file-name "drill/vocab.org")
my/org-calendar-directory (my/expand-org-file-name ".calendar")
my/org-attachment-directory (my/expand-org-file-name ".attachment"))
Org Basic
(use-package org
:bind
(:map org-mode-map ("C-x n n" . org-toggle-narrow-to-subtree))
:config
;; Enable the =org-indent-mode= by default.
(setq org-startup-indented t)
;; Reduce org-indent-indentation-per-level from 2 to 1.
;;
;; This keeps =org-tags-column= the same for all headings.
(setq org-indent-indentation-per-level 1)
:hook
;; Reserve "C-c <arrow>" for windmove
(org-mode . (lambda ()
(local-unset-key (kbd "C-c <left>"))
(local-unset-key (kbd "C-c <right>"))
(local-unset-key (kbd "C-c <up>"))
(local-unset-key (kbd "C-c <down>")))))
Capturing
(global-set-key (kbd "C-c c") 'org-capture)
;; The default file for capturing.
(setq org-default-notes-file my/org-inbox-file)
;; Org Capture Templates
;;
;; See https://orgmode.org/manual/Template-elements.html#index-org_002ddefault_002dnotes_002dfile-1
(setq org-capture-templates nil)
(add-to-list
'org-capture-templates
'("i" "Inbox" entry (file org-default-notes-file)
"* %?\n%i\n%a"))
(add-to-list
'org-capture-templates
'("p" "Project" entry (file+headline my/org-task-file "Projects")
(file "~/org/.template/project.org")))
(add-to-list
'org-capture-templates
'("d" "Review: Daily Review" entry (file+olp+datetree my/org-review-file)
(file "~/org/.template/review-daily.org")
:tree-type week :clock-in t :clock-keep t :immediate-finish t :jump-to-captured t))
(add-to-list
'org-capture-templates
'("w" "Review: Weekly Review" entry (file+datetree my/org-review-file)
(file "~/org/.template/review-weekly.org")
:clock-in t :clock-keep t :immediate-finish t :jump-to-captured t))
(add-to-list
'org-capture-templates
'("v" "Vocab" entry (file+headline my/org-vocab-file "Translation")
"* %? :drill:\n\n** Translation\n\n** Definition\n"))
(use-package org-cliplink)
Attachment
I was using org-download
but switched to the built-in org-attach
in 2022. I use .dir-locals.el
to customize org-attach-id-dir
if necessary.
(require 'org-attach)
(setq org-attach-id-dir my/org-attachment-directory)
(setq org-attach-use-inheritance t)
;; Use timestamp as ID and attachment folder. See https://helpdeskheadesk.net/2022-03-13/
;; (setq org-id-method 'ts)
;; (setq org-attach-id-to-path-function-list
;; '(org-attach-id-ts-folder-format
;; org-attach-id-uuid-folder-format))
;; Shorten the Org timestamp ID
;; (setq org-id-ts-format "%Y%m%dT%H%M%S")
Task Management
See this blog post for my Org-Mode workflow for task management.
;; Sets default-directory to org-directory so that =M-x magit= from the agenda view does not ask me for a dir.
(global-set-key (kbd "C-c a")
(lambda ()
(interactive)
(let ((default-directory org-directory)) (org-agenda))))
;; My agenda files.
(setq org-agenda-files (list my/org-inbox-file my/org-task-file my/org-calendar-directory))
;; Trying to use the current window as agenda frame.
(setq org-agenda-window-setup 'current-window)
;; Use sticky agenda since I need different agenda views (personal and work) at the same time.
(setq org-agenda-sticky t)
;; Just today
(setq org-agenda-span 'day)
;; Hide all scheduled todo.
(setq org-agenda-todo-ignore-scheduled 'all)
;; Ignores "far" deadline TODO items from TODO list.
(setq org-agenda-todo-ignore-deadlines 'far)
;; Hide all scheduled todo, from tags search view, like tags-todo.
(setq org-agenda-tags-todo-honor-ignore-options t)
;; Hide all done todo in agenda
(setq org-agenda-skip-scheduled-if-done t)
;; Hide task until the scheduled date.
(setq org-agenda-skip-deadline-prewarning-if-scheduled 'pre-scheduled)
;; Use an indirect buffer after <Tab> (org-agenda-goto) or <Enter> (org-agenda-switch-to).
;;
;; Also see https://emacs.stackexchange.com/a/17822
;; (advice-add 'org-agenda-goto :after
;; (lambda (&rest args)
;; (org-tree-to-indirect-buffer)))
;; (advice-add 'org-agenda-switch-to :after
;; (lambda (&rest args)
;; (org-tree-to-indirect-buffer)))
;; Narrow to subtree after <Tab> (org-agenda-goto) or <Enter> (org-agenda-switch-to).
;; (advice-add 'org-agenda-goto :after
;; (lambda (&rest args)
;; (org-narrow-to-subtree)))
;; (advice-add 'org-agenda-switch-to :after
;; (lambda (&rest args)
;; (org-narrow-to-subtree)))
(defun my/consult-org-agenda ()
(interactive)
(consult-org-agenda)
(org-tree-to-indirect-buffer))
(defun my/consult-org-task-and-note ()
(interactive)
(consult-org-heading
nil
(list my/org-inbox-file my/org-task-file my/org-task-archive-file my/org-note-file my/org-checklist-file)))
(defun my/consult-org-note ()
(interactive)
(consult-org-heading nil (list my/org-task-file my/org-task-archive-file my/org-note-file)))
(global-set-key (kbd "M-J") #'my/consult-org-task-and-note)
(global-set-key (kbd "<f1>") #'my/consult-org-task-and-note)
(global-set-key (kbd "<f2>") #'my/consult-org-note)
(defun my/consult-ripgrep-org-directory ()
(interactive)
;; Add "--no-ignore-vcs" to the rg command so todo.org could be searched.
(let ((consult-ripgrep-args
"rg --null --line-buffered --color=never --max-columns=1000 --path-separator / --smart-case --no-heading --line-number --no-ignore-vcs ."))
(consult-ripgrep org-directory "")))
(if (eq system-type 'darwin)
(global-set-key (kbd "s-F") 'my/consult-ripgrep-org-directory)
(global-set-key (kbd "C-S-f") 'my/consult-ripgrep-org-directory))
;; Include the file name into the path in refile target.
(setq org-refile-use-outline-path 'file)
(setq org-outline-path-complete-in-steps nil)
(setq org-refile-targets
'((nil :maxlevel . 2)
(my/org-task-file :maxlevel . 3)))
(setq org-enforce-todo-dependencies t)
(setq org-log-into-drawer t)
;; 4 priorities: A, B, C, D (default)
(setq org-priority-default 68
org-priority-lowest 68)
;; Interstitial Journaling: add note to CLOCK entry after clocking out
;; https://emacs.stackexchange.com/questions/37526/add-note-to-clock-entry-after-clocking-out
(setq org-log-note-clock-out t)
Pulling Google Calendar
I followed https://orgmode.org/worg/org-tutorials/org-google-sync.html to sync Google Calendar to org but not the other way, including a cron job every hour. Also, here is a elisp command to fetch manually.
(defun my/sync-calendar()
(interactive)
(message "Synchronizing...")
(shell-command "~/org/.calendar/fetch.sh")
(message "Synchronized."))
Org Breadcrumb
(defun ndk/heading-title ()
"Get the heading title."
(save-excursion
(if (not (org-at-heading-p))
(org-previous-visible-heading 1))
(org-element-property :title (org-element-at-point))))
(defun ndk/org-breadcrumbs ()
"Get the chain of headings from the top level down
to the current heading."
(let ((breadcrumbs (org-format-outline-path
(org-get-outline-path)
(1- (frame-width))
nil " > "))
(title (ndk/heading-title)))
(if (string-empty-p breadcrumbs)
title
(format "%s" breadcrumbs))))
(defun ndk/set-header-line-format()
(setq header-line-format '(:eval (ndk/org-breadcrumbs))))
(add-hook 'org-mode-hook #'ndk/set-header-line-format)
Literate Programming with Org-Babel
https://orgmode.org/worg/org-contrib/babel/languages/index.html
(org-babel-do-load-languages
'org-babel-load-languages
'((emacs-lisp . t) (shell . t)))
Respect Content When Inserting Heading
(setq org-insert-heading-respect-content t)
Also see https://www.n16f.net/blog/org-mode-headline-tips/.
Clocking
(setq org-clock-mode-line-total 'current)
(setq org-show-notification-timeout 3600)
(org-clock-auto-clockout-insinuate)
;; Create an indirect buffer for the current clocking task, and focus into it.
(defun my/org-clock-goto ()
(interactive)
(org-clock-goto)
(org-tree-to-indirect-buffer)
(other-window 1)
(delete-other-windows))
(global-set-key (kbd "C-c C-x j") 'my/org-clock-goto)
Images
(setq org-startup-with-inline-images t)
;; allows overriding the image width
(setq org-image-actual-width nil)
Personal Knowledge Management
Note Taking with denote
(defun my/denote-random-note (&optional directory)
"Open a random denote."
(interactive)
(let* ((denote-directory (or directory denote-directory))
(files (denote-directory-files)))
(find-file (nth (random (length files)) files))))
(defun my/project-org-file-file ()
(interactive)
(let ((default-directory org-directory)) (project-find-file)))
(use-package denote
:bind
("C-c n n" . 'denote)
;; ("C-c n f" . 'denote-open-or-create)
("C-c n f" . 'my/project-org-file-file)
("C-c n k" . 'denote-keywords-add) ;; update file name automatically
("C-c n K" . 'denote-keywords-remove) ;; update file name automatically
("C-c n u" . 'denote-rename-file-using-front-matter)
("C-c n l" . 'denote-link-find-backlink)
("C-c n r" . 'my/denote-random-note)
:init
(setq denote-directory (expand-file-name org-directory))
:config
(setq denote-known-keywords '("emacs"))
(setq denote-prompts '(subdirectory title))
(setq denote-excluded-directories-regexp ".attachment")
;; Makes the denote links different from usual link.
(set-face-attribute 'denote-faces-link
nil :foreground "magenta" :inherit 'link)
;; Remove the date and the identifier. They are duplicated with the file name.
;; I want to remove filetags too but denote-keyword-* need that.
(setq denote-org-front-matter "#+title: %1$s\n#+filetags: %3$s\n")
(add-hook 'dired-mode-hook #'denote-dired-mode))
;; allow empty keyword
(defun denote-rename-file-using-front-matter (file &optional auto-confirm)
(interactive (list (buffer-file-name) current-prefix-arg))
(when (buffer-modified-p)
(if (or auto-confirm
(y-or-n-p "Would you like to save the buffer?"))
(save-buffer)
(user-error "Save buffer before proceeding")))
(unless (denote-file-is-writable-and-supported-p file)
(user-error "The file is not writable or does not have a supported file extension"))
(if-let* ((file-type (denote-filetype-heuristics file))
(title (denote-retrieve-title-value file file-type))
(extension (file-name-extension file t))
(id (denote-retrieve-or-create-file-identifier file))
(dir (file-name-directory file))
(new-name (denote-format-file-name
dir id (denote-retrieve-keywords-value file file-type) (denote-sluggify title) extension)))
(when (or auto-confirm
(denote-rename-file-prompt file new-name))
(denote-rename-file-and-buffer file new-name)
(denote-update-dired-buffers))
(user-error "No front matter for title and/or keywords")))
;; (use-package consult-denote
;; :straight (consult-denote :type git :host codeberg :repo "whhone/consult-denote")
;; :after denote
;; :bind
;; ("C-c n i" . 'consult-denote-link-insert-link)
;; ("C-c n f" . 'consult-denote-open-or-create)
;; ("C-c n F" . 'consult-denote-ripgrep))
Blogging with ox-hugo
My blog is generated by Hugo and ox-hugo.
(use-package ox-hugo
:after ox)
Exporting Notes
;; Do not export with TOC, e.g., with org-md-export-as-markdown, org-ascii-export-as-ascii
(setq org-export-with-toc nil)
;; (setq org-export-with-section-numbers nil)
Spaced Repetition with org-drill
I use org-drill to enhance my learning, like vocabulary, reading notes, concepts, etc.
;; https://orgmode.org/worg/org-contrib/org-drill.html
(use-package org-drill
:custom
(org-drill-save-buffers-after-drill-sessions-p nil "Save buffers after drill sessions without prompt.")
(org-drill-maximum-items-per-session 10 "Reduce from the default 30 to make it to become a habit.")
:bind
("C-c d" . org-drill))
I used Anki before embracing Emacs and Org Mode. Anki gave me access on mobile but I have to sync my data to its server. I migrated to org-drill for a more coherent Emacs workflow. To be fair, it gives me access over ssh :-p.
Reading
Dictionary
;; https://github.com/xuchunyang/youdao-dictionary.el
(use-package youdao-dictionary
:config
(setq url-automatic-caching t) ; enable cache
:bind
("C-c v" . youdao-dictionary-search-at-point+)
("C-c V" . youdao-dictionary-play-voice-at-point))
(defun my/browse-dictionary-at-point ()
(interactive)
(browse-url (concat "https://dictionary.cambridge.org/zht/詞典/英語-漢語-繁體/" (thing-at-point 'word))))
Google Translate
(use-package google-translate
:config
(setq google-translate-output-destination 'popup)
(setq google-translate-default-source-language "en")
(setq google-translate-default-target-language "zh-TW")
:bind
("C-c b" . google-translate-at-point))
Writing
Synonyms
;; https://www.powerthesaurus.org/
(use-package powerthesaurus
:bind
("M-`" . powerthesaurus-lookup-word-dwim))
Write Good
The first challenge from Dreyer’s English is to write without weasel words.
(use-package writegood-mode
:config
(setq writegood-weasel-words
'("very" "rather" "really" "quite" "in fact" "just" "so" "pretty" "of course" "surely" "that said" "actually")))
Programming
Magit
There are multiple key-binding to trigger magit-status
:
C-x g
- The default keybinding from magit.
C-x p m
- The keybinding from project.el.
(use-package magit)
Diff Highlight
(use-package diff-hl
:init
(global-diff-hl-mode)
:hook
(magit-pre-refresh . diff-hl-magit-pre-refresh)
(magit-post-refresh . diff-hl-magit-post-refresh))
Terminal
(use-package vterm
:config
(define-key vterm-mode-map (kbd "<f1>") nil)
(define-key vterm-mode-map (kbd "<f2>") nil)
(define-key vterm-mode-map (kbd "<f3>") nil)
(define-key vterm-mode-map (kbd "<f4>") nil)
(define-key vterm-mode-map (kbd "<f5>") nil)
(define-key vterm-mode-map (kbd "<f6>") nil)
(define-key vterm-mode-map (kbd "<f7>") nil)
(define-key vterm-mode-map (kbd "<f8>") nil)
(define-key vterm-mode-map (kbd "<f9>") nil)
(define-key vterm-mode-map (kbd "<f10>") nil)
(define-key vterm-mode-map (kbd "<f11>") nil)
(define-key vterm-mode-map (kbd "<f12>") nil)
(setq vterm-shell "zsh")
(setq vterm-tramp-shells '(("docker" "/bin/sh") ("ssh" "/bin/zsh")))
:hook
(vterm-mode . goto-address-mode))
(use-package vterm-toggle
:config
;; show vterm buffer in side window
(add-to-list 'display-buffer-alist
'("\\*vterm\\*"
(display-buffer-reuse-window display-buffer-in-side-window)
(side . bottom)
(dedicated . t)
(reusable-frames . visible) ;; depends on how I use Emacs / Emacs Client
(window-height . 0.5)))
:bind
("s-<return>" . vterm-toggle)
("C-`" . vterm-toggle)
("C-c t" . vterm-toggle))
Make Script Files Executable Automatically
Make script files (with shebang like #!/bin/bash
, #!/bin/sh
) executable automatically. See this blog post from Emacs Redux.
(add-hook 'after-save-hook
'executable-make-buffer-file-executable-if-script-p)
YAML Mode
(use-package yaml-mode)
Web Mode
https://web-mode.org/ is an autonomous major-mode for editing web templates.
(use-package web-mode
:init
(add-to-list 'auto-mode-alist '("\\.html\\'" . web-mode))
;; TODO: Use .dir.local because not all .html are go template
(setq web-mode-engines-alist
'(("go" . "\\.html\\'"))))
Markdown Mode
(use-package markdown-mode
:bind
(:map markdown-mode-map ("M-<left>" . markdown-promote))
(:map markdown-mode-map ("M-<right>" . markdown-demote))
(:map markdown-mode-map ("M-S-<left>" . markdown-promote-subtree))
(:map markdown-mode-map ("M-S-<right>" . markdown-demote-subtree)))
Company Mode
(use-package company)
Eglot
(use-package eglot
:config
(define-key eglot-mode-map
[remap xref-find-definitions] #'eglot-find-typeDefinition)
(define-key eglot-mode-map
[remap xref-find-references] #'eglot-find-declaration)
(define-key eglot-mode-map (kbd "M-l r") 'eglot-rename)
(define-key eglot-mode-map (kbd "M-l f") 'eglot-format)
(add-hook 'c++-mode-hook 'eglot-ensure))
Caddyfile Mode
(use-package caddyfile-mode
:ensure t
:mode (("Caddyfile\\'" . caddyfile-mode)))
Competitive Programming
Auto Insert the C++ Template
;; *NOTE* Trailing slash important
(setq auto-insert-directory "~/.emacs.d/auto-insert/")
(define-auto-insert "\.cpp" "template.cpp")
(auto-insert-mode)
Compile and Run
See https://codeforces.com/blog/entry/101292.
(defun my/compileandrun()
(interactive)
(if (eq major-mode 'c++-mode)
(let* ((src (file-name-nondirectory (buffer-file-name)))
(exe (file-name-sans-extension src)))
(compile (concat "g++ -std=c++17 -O2 -Wall -Wno-sign-compare " src " -o /tmp/" exe " && time /tmp/" exe " < /tmp/input.txt" )))
(recompile)))
(global-set-key (kbd "<f12>") 'my/compileandrun)
Project Management
Project
I particularly love the handy projectile
function projectile-find-file-in-known-project
and adding that function to project.el
below.
Note: Run project-forget-zombie-projects
to remove deleted project directories.
project-prefix-map
is bind to C-x p
by default. (it seems no way to change it)
(setq project-switch-use-entire-map t)
Full Text Search (F
) in Current Project Files
(defun my/consult-ripgrep-current-project ()
"Full text search with ripgrep in current project"
(interactive)
(consult-ripgrep (if (project-current) (project-root (project-current))
default-directory) ""))
(define-key project-prefix-map (kbd "F") #'my/consult-ripgrep-current-project)
Find File (a
) in All Known Project
(defun my/project-all-project-files ()
"Get a list of all files in all known projects."
(cl-mapcan
(lambda (project)
(when (file-exists-p project)
(mapcar (lambda (file)
(expand-file-name file project))
(project-files (project-current nil project)))))
(project-known-project-roots)))
(defun my/project-find-file-in-known-projects ()
"Find a file from all known projects."
(interactive)
(let* ((completion-ignore-case read-file-name-completion-ignore-case)
;; Note that "project--read-file-cpd-relative" is broken for
;; this function since Emacs 29. Instead of using
;; "project-read-file-name-function", always use
;; "project--read-file-absolute".
(file (project--read-file-absolute
"Find file in known projects" (my/project-all-project-files) nil nil
(thing-at-point 'filename))))
(if (string= file "")
(user-error "You didn't specify the file")
(find-file file))))
(define-key project-prefix-map (kbd "a") #'my/project-find-file-in-known-projects)
Chinese Support
Rime Input Method
See https://github.com/DogLooksGood/emacs-rime for the setup instruction.
Use C-\ (toggle-input-method)
to toggle the input method. Run M-x rime-open-configuration
to open the default.custom.yaml
under rime-user-data-dir
.
(use-package rime
:init
(when (eq system-type 'darwin)
;; Set the root for librime ("rime-api.h").
;; See https://github.com/DogLooksGood/emacs-rime/blob/master/INSTALLATION.org
(setq rime-librime-root "~/.emacs.d/librime/dist")
;; Set the root for emacs module ("emacs-module.h")
(setq rime-emacs-module-header-root "/opt/homebrew/opt/emacs-head@29/include/"))
(setq default-input-method "rime")
:custom
(rime-show-candidate 'posframe)
(rime-posframe-style 'vertical))
CJK Word Wrap
Emacs 28 adds better word wrap / line break support for CJK. See Emacs 28 新特性介绍.
(setq word-wrap-by-category t)
Better CJK Font
cnfonts is a tool for configuring the fonts used in Emacs.
(use-package cnfonts
:init
(if (eq system-type 'darwin)
(setq cnfonts-profiles '("osx"))
(setq cnfonts-profiles '("hack")))
(setq cnfonts-default-fontsize 10)
:config
(cnfonts-mode 1))
GnuPG Passphrase Via Minibuffer
GnuPG could be used when signing git commit with GPG in Magit over the SSH.
See https://elpa.gnu.org/packages/pinentry.html for the setup steps. It requires adding allow-emacs-pinentry
to the ~/.gnupg/gpg-agent.conf
.
(use-package pinentry
:init
(pinentry-start))
Mac OS (Command: “M”, Option: “A”)
This preserves the Emacs’s bindings closer to Linux by setting the mac-*-modifier.
;; These are some key bindings ported from MacOS to everywhere, since I found they are handy.
;; MacOS uses Command+f for "find".
(global-set-key (kbd "M-f") 'consult-line)
(global-set-key (kbd "M-F") 'my/consult-ripgrep-org-directory)
;; MacOS uses Command+s for "save".
(global-set-key (kbd "M-s") 'my/save-buffer)
;; MacOS uses Command+c to copy but I prefer keeping the Emacs style.
;; Disable it in case I mistrigger "capitalize-word".
(global-unset-key (kbd "M-c"))
(when (eq system-type 'darwin)
;; MacOS put the menu-bar in the top bar which already using the space.
(menu-bar-mode 1)
(setq mac-option-modifier 'alt)
(setq mac-command-modifier 'meta)
(global-set-key (kbd "<home>") 'move-beginning-of-line)
(global-set-key (kbd "<end>") 'move-end-of-line)
;; MacOS uses Command+Control+f to toggle fullscreen. Also make it work with Emacs.
(global-set-key (kbd "C-M-f") 'toggle-frame-fullscreen)
;; MacOS uses Option+up/down to scroll.
(global-set-key (kbd "A-<up>") 'scroll-down-command)
(global-set-key (kbd "A-<down>") 'scroll-up-command)
;; (global-set-key (kbd "M-p") 'execute-extended-command)
;; (global-set-key (kbd "M-x") 'kill-region) ; already bind for s-x
;; sets fn-delete to be right-delete
(global-set-key [kp-delete] 'delete-char)
(defun my/apply-theme (appearance)
"Load theme, taking current system APPEARANCE into consideration."
;; (mapc #'disable-theme custom-enabled-themes)
(pcase appearance
('light (modus-themes-select 'modus-operandi))
('dark (modus-themes-select 'modus-vivendi-tinted))))
(add-hook 'ns-system-appearance-change-functions #'my/apply-theme))
Personal Information
(setq user-mail-address "whhone@gmail.com"
user-full-name "Wai Hon Law")