Skip to content

larstvei/dot-emacs

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Emacs configuration file

About

This is an Emacs configuration file written in Org mode. It is an attempt to keep my ~/.emacs.d tidy, but still be able to keep it all in one file. I aim to briefly explain all my configurations as I go along!

I would not recommend using this configuration as-is, because it probably contains a lot you don’t really need. I do, however, hope people find some golden nuggets that they can smuggle into their own configs.

If you really do want to try this config out, this is how I’d go about it:

Manual installation

Clone the repo.

git clone https://github.com/larstvei/dot-emacs

Backup your old ~/.emacs.d (if necessary).

mv ~/.emacs.d ~/.emacs.d-bak

Backup your old ~/.emacs-file (if necessary).

mv ~/.emacs ~/.emacs-bak

And finally

mv dot-emacs ~/.emacs.d

On first run it should install a bunch of packages (this might take a while), and you might have to restart your Emacs the first time. If you experience bugs, please let me know!

With Nix

I keep an updated nix flake for my Emacs config. If that tells you anything, you can try it out running:

nix run github:larstvei/emacs-flake

Meta

All changes to the configuration should be done in init.org, not in init.el. Any changes in the init.el will be overwritten by saving init.org. The init.el in this repo should not be tracked by git, and is replaced the first time Emacs is started (assuming it has been renamed to ~/.emacs.d).

Emacs can’t load .org-files directly, but org-mode provides functions to extract the code blocks and write them to a file. There are multiple ways of handling this; like suggested by this StackOverflow post, one could just use org-babel-load-file, but I had problems with byte-compilation. Previously I tracked both the org.- and el.-files, but the git commits got a little messy. So here is a new approach.

When this configuration is loaded for the first time, the init.el is the file that is loaded. It looks like this:

;; This file replaces itself with the actual configuration at first run.

;; We can't tangle without org!
(require 'org)
;; Open the configuration
(find-file (concat user-emacs-directory "init.org"))
;; tangle it
(org-babel-tangle)
;; load it
(load-file (concat user-emacs-directory "init.el"))
;; finally byte-compile it
(byte-compile-file (concat user-emacs-directory "init.el"))

It tangles the org-file, so that this file is overwritten with the actual configuration.

There is no reason to track the init.el that is generated; by running the following command git will not bother tracking it:

git update-index --assume-unchanged init.el

If one wishes to make changes to the repo-version of init.el start tracking again with:

git update-index --no-assume-unchanged init.el

I want lexical scoping for the init-file, which can be specified in the header. The first line of the configuration is as follows:

;;; -*- lexical-binding: t -*-

The init.el should (after the first run) mirror the source blocks in the init.org. We can use C-c C-v t to run org-babel-tangle, which extracts the code blocks from the current file into a source-specific file (in this case a .el-file).

To avoid doing this each time a change is made we can add a function to the after-save-hook ensuring to always tangle and byte-compile the org-document after changes.

(defun tangle-init ()
  "If the current buffer is init.org the code-blocks are
tangled, and the tangled file is compiled."
  (when (equal (buffer-file-name)
               (expand-file-name (concat user-emacs-directory "init.org")))
    ;; Avoid running hooks when tangling.
    (let ((prog-mode-hook nil))
      (org-babel-tangle)
      (byte-compile-file (concat user-emacs-directory "init.el")))))

(add-hook 'after-save-hook 'tangle-init)

I’d like to keep a few settings private, so we load a private.el if it exists after the init-file has loaded.

(add-hook
 'after-init-hook
 (lambda ()
   (let ((private-file (concat user-emacs-directory "private.el")))
     (when (file-exists-p private-file)
       (load-file private-file))
     (when custom-file
       (load-file custom-file))
     (server-start))))

Faster startup

A common optimization is to temporarily disable garbage collection during initialization. Here, we set the gc-cons-threshold to a ridiculously large number, and restore the default value after initialization.

(setq gc-cons-threshold most-positive-fixnum)
(add-hook 'after-init-hook
          (lambda ()
            (setq gc-cons-threshold (* 1024 1024 20))))

Packages

John Wiegley’s extremely popular use-package was included in Emacs 29. It provides a powerful macro for isolating package configuration. After ignoring this for a decade, I’ll budge and give it a whirl.

(require 'use-package)
(setq use-package-always-ensure t)

Packages can be fetched from different mirrors, melpa is the largest archive and is well maintained.

(setq package-archives
      '(("GNU ELPA"     . "https://elpa.gnu.org/packages/")
        ("MELPA Stable" . "https://stable.melpa.org/packages/")
        ("MELPA"        . "https://melpa.org/packages/"))
      package-archive-priorities
      '(("GNU ELPA"     . 10)
        ("MELPA"        . 5)
        ("MELPA Stable" . 0)))

Sane defaults

These are what I consider to be saner defaults.

Set utf-8 as preferred coding system.

(set-language-environment "UTF-8")
(prefer-coding-system 'utf-8)

We can set variables to whatever value we’d like using setq.

(setq auto-revert-interval 1            ; Refresh buffers fast
      default-input-method "TeX"        ; Use TeX when toggling input method
      echo-keystrokes 0.1               ; Show keystrokes asap
      enable-recursive-minibuffers t    ; Allow recursive minibuffers
      frame-inhibit-implied-resize 1    ; Don't resize frame implicitly
      inhibit-startup-screen t          ; No splash screen please
      initial-scratch-message nil       ; Clean scratch buffer
      recentf-max-saved-items 10000     ; Show more recent files
      ring-bell-function 'ignore        ; Quiet
      scroll-margin 1                   ; Space between cursor and top/bottom
      sentence-end-double-space nil     ; No double space
      custom-file                       ; Customizations in a separate file
      (concat user-emacs-directory "custom.el"))
;; Some mac-bindings interfere with Emacs bindings.
(when (boundp 'mac-pass-command-to-system)
  (setq mac-pass-command-to-system nil))

Some variables are buffer-local, so changing them using setq will only change them in a single buffer. Using setq-default we change the buffer-local variable’s default value.

(setq-default tab-width 4                       ; Smaller tabs
              fill-column 79                    ; Maximum line width
              truncate-lines t                  ; Don't fold lines
              indent-tabs-mode nil              ; Use spaces instead of tabs
              split-width-threshold 160         ; Split verticly by default
              split-height-threshold nil        ; Split verticly by default
              frame-resize-pixelwise t          ; Fine-grained frame resize
              auto-fill-function 'do-auto-fill) ; Auto-fill-mode everywhere

The load-path specifies where Emacs should look for .el-files (or Emacs lisp files). I have a directory called site-lisp where I keep all extensions that have been installed manually (these are mostly my own projects).

(let ((default-directory (concat user-emacs-directory "site-lisp/")))
  (when (file-exists-p default-directory)
    (setq load-path
          (append
           (let ((load-path (copy-sequence load-path)))
             (normal-top-level-add-subdirs-to-load-path))
           load-path))))

Answering yes and no to each question from Emacs can be tedious, a single y or n will suffice.

(fset 'yes-or-no-p 'y-or-n-p)

To avoid file system clutter we put all auto saved files in a single directory.

(defvar emacs-autosave-directory
  (concat user-emacs-directory "autosaves/")
  "This variable dictates where to put auto saves. It is set to a
  directory called autosaves located wherever your .emacs.d/ is
  located.")

;; Sets all files to be backed up and auto saved in a single directory.
(setq backup-directory-alist
      `((".*" . ,emacs-autosave-directory))
      auto-save-file-name-transforms
      `((".*" ,emacs-autosave-directory t)))

By default the narrow-to-region command is disabled and issues a warning, because it might confuse new users. I find it useful sometimes, and don’t want to be warned.

(put 'narrow-to-region 'disabled nil)

Automatically revert doc-view-buffers when the file changes on disk.

(add-hook 'doc-view-mode-hook 'auto-revert-mode)

Key bindings

Inspired by this StackOverflow post I keep a custom-bindings-map that holds all my custom bindings. This map can be activated by toggling a simple minor-mode that does nothing more than activating the map. This inhibits other major-modes to override these bindings.

(defvar custom-bindings-map (make-keymap)
  "A keymap for custom bindings.")

Visual

First off, let’s declutter. Remove clickies to give a nice and clean look. Also, the cursor can relax. We add this to the early-init, as it might be marginally faster, and look less wonky.

(dolist (mode
         '(tool-bar-mode                ; No toolbars, more room for text
           scroll-bar-mode              ; No scroll bars either
           blink-cursor-mode))          ; The blinking cursor gets old
  (funcall mode 0))

Add a small border on the frame. This also goes in the early-init.

(add-to-list 'default-frame-alist '(undecorated-round . t))
(add-to-list 'default-frame-alist '(internal-border-width . 24))

I am using a lot from rougier’s N Λ N O Emacs, starting with the theme.

Theme

For the light theme, I keep the light background toned down a touch.

;; N Λ N O theme
(use-package nano-theme
  :init
  (setq nano-light-background "#fafafa"
        nano-light-highlight "#f5f7f8"))

The theme is set according to the system appearance (on macOS) if that is available, defaulting to a light theme.

(defun load-nano-theme (variant)
  (let ((theme (intern (concat "nano-" (symbol-name variant)))))
    (load-theme theme t)))

(load-nano-theme (if (boundp 'ns-system-appearance) ns-system-appearance 'light))

Let’s have Emacs change theme when the system appearance changes as well.

(when (boundp 'ns-system-appearance-change-functions)
  (add-hook 'ns-system-appearance-change-functions 'load-nano-theme))

I want to be able to quickly switch between a light and a dark theme.

(defun cycle-themes ()
  "Returns a function that lets you cycle your themes."
  (let ((themes '(nano-light nano-dark)))
    (lambda ()
      (interactive)
      ;; Rotates the thme cycle and changes the current theme.
      (let ((rotated (nconc (cdr themes) (list (car themes)))))
        (load-theme (car (setq themes rotated)) t))
      (message (concat "Switched to " (symbol-name (car themes)))))))

Mode line

This is my setup for N Λ N O Modeline after version 1.0.0:

;; N Λ N O modeline
(use-package nano-modeline
  :init
  ;; Disable the default modeline
  (setq-default mode-line-format nil)
  :config
  (defun my-default-nano-modeline (&optional default)
    "My nano modeline configuration."
    (funcall nano-modeline-position
             `((nano-modeline-buffer-status)
               (nano-modeline-buffer-name) " "
               (nano-modeline-git-info))
             `((nano-modeline-cursor-position)
               (nano-modeline-window-dedicated))
             default))
  (my-default-nano-modeline 1))

Font

I primarily use Adobe Fonts.

My default monospace font is Source Code Pro:

(when (member "Source Code Pro" (font-family-list))
  (set-face-attribute 'default nil :font "Source Code Pro-15"))

My preferred proportional font is Source Serif. In order to get variable-pitch fonts where it makes sense, I use mixed-pitch.

;; Use a variable pitch, keeping fixed pitch where it's sensible
(use-package mixed-pitch
  :defer t
  :hook (text-mode . mixed-pitch-mode)
  :config
  (when (member "Source Serif Pro" (font-family-list))
    (set-face-attribute 'variable-pitch nil :family "Source Serif Pro")))

Centering with Olivetti

Olivetti is a package that simply centers the text of a buffer. It is very simple and beautiful. The default width is just a bit short.

;; Minor mode for a nice writing environment
(use-package olivetti
  :defer t
  :bind (:map custom-bindings-map ("C-c o" . olivetti-mode))
  :config
  (setq-default olivetti-body-width (+ fill-column 3)))

Adaptive wrapping

I usually have auto-fill-mode enabled. When visual-fill-mode is enabled, try to mimic how it looks when having used fill-paragraph with adaptive-wrap.

(use-package adaptive-wrap
  :defer t
  :hook (visual-line-mode . adaptive-wrap-prefix-mode))

Focusing with focus

Focus is my own package. It looks pretty nice, especially in combination with Olivetti!

;; Dim color of text in surrounding sections
(use-package focus
  :defer t
  :bind (:map custom-bindings-map ("C-c f" . focus-mode)))

Dashboard

Dashboard provides a nice welcome.

;; A startup screen extracted from Spacemacs
(use-package dashboard
  :config
  (setq dashboard-projects-backend 'project-el
        dashboard-banner-logo-title nil
        dashboard-center-content t
        dashboard-set-footer nil
        dashboard-page-separator "\n\n\n"
        dashboard-items '((projects . 15)
                          (recents  . 15)
                          (bookmarks . 5)))
  (dashboard-setup-startup-hook))

Center content mode

center-content-mode is a small, homegrown, minor mode for centering the buffer content both horizontally and vertically.

(use-package center-content-mode
  :ensure nil)

Mac OS X

I run this configuration mostly on macOS, so we need a couple of settings to make things work smoothly. I use the Command-key as the Meta-key, Freeing up the Option-key, which I need for typing Norwegian characters on a US keyboard. In addition, it is more comfortable.

I try to minimize the use of frames. The native compilation gives a lot of warnings, but they seem safe to ignore.

(when (memq window-system '(mac ns))
  (setq mac-option-modifier nil
        mac-command-modifier 'meta
        ns-pop-up-frames nil
        native-comp-async-report-warnings-errors nil))

The package exec-path-from-shell synchronizes environment variables from the shell to Emacs. This makes it a lot easier to deal with external programs on macOS.

(use-package exec-path-from-shell
  :if (memq window-system '(mac ns))
  :config
  (exec-path-from-shell-initialize))

I had some problems with Dired, and this seems to have solved it. I think the solutions was from here, and my problems were related, but not the same.

(use-package ls-lisp
  :ensure nil
  :if (memq window-system '(mac ns))
  :config
  (setq ls-lisp-use-insert-directory-program nil))

It is useful to be able to occasionally open the file associated with a buffer in macOS Finder.

(use-package reveal-in-osx-finder
  :if (memq window-system '(mac ns)))

Modes

Here are a list of modes that I prefer enable by default.

(dolist (mode
         '(abbrev-mode                  ; E.g. sopl -> System.out.println
           column-number-mode           ; Show column number in mode line
           delete-selection-mode        ; Replace selected text
           dirtrack-mode                ; directory tracking in *shell*
           global-so-long-mode          ; Mitigate performance for long lines
           recentf-mode                 ; Recently opened files
           show-paren-mode))            ; Highlight matching parentheses
  (funcall mode 1))

Version control

Magit is the best.

;; A Git porcelain inside Emacs.
(use-package magit
  :hook ((magit-pre-refresh . diff-hl-magit-pre-refresh)
         (magit-post-refresh . diff-hl-magit-post-refresh))
  :bind (:map custom-bindings-map ("C-c m" . magit-status)))

Have some visual indication where there are uncommitted changes.

;; Highlight uncommitted changes using VC
(use-package diff-hl
  :config
  (global-diff-hl-mode 1))

Project

(use-package project
  :config
  (add-to-list 'project-switch-commands '(magit-project-status "Magit" ?m)))

Window management

Some keybindings (involving the option, resulting in funny symbols) for window management.

(use-package windmove
  :ensure nil
  :bind (:map custom-bindings-map
              ("M-˙" . windmove-left)
              ("M-∆" . windmove-down)
              ("M-˚" . windmove-up)
              ("M-¬" . windmove-right)

              ("M-ó" . windmove-swap-states-left)
              ("M-ô" . windmove-swap-states-down)
              ("M-" . windmove-swap-states-up)
              ("M-ò" . windmove-swap-states-right)))

EditorConfig

Using EditorConfig is a must when collaborating with others. It is also a way of having multiple tools that want to format your buffer to agree (e.g. both the language’s Emacs mode and some external formatter/prettifier).

;; EditorConfig Emacs Plugin
(use-package editorconfig
  :config
  (editorconfig-mode 1))

Completion UI

I have transitioned from Helm to Ivy, and now, on to Vertico. It improves the interface calling commands (i.e. M-x), finding files, switching buffers, searching files and so on. Using the vertico-buffer-mode gives a more Helm-like experience, where completions are given a full fledged buffer.

;; VERTical Interactive COmpletion
(use-package vertico
  :init
  (vertico-mode 1)
  :config
  (setq vertico-count 25))

Use the built in savehist-mode to prioritize recently used commands.

;; Save minibuffer history
(use-package savehist
  :ensure nil
  :init
  (savehist-mode 1))

With Marginalia, we get better descriptions for commands inline.

;; Enrich existing commands with completion annotations
(use-package marginalia
  :init 
  (marginalia-mode 1))

Completion

I used Auto-Complete for years, then I used company-mode for even more years, and now I am giving corfu a shot. I want a pretty aggressive completion system, hence the no delay settings and a short prefix length.

;; Modular text completion framework
(use-package corfu
  :init
  (global-corfu-mode 1)
  (corfu-popupinfo-mode 1)
  :config
  (setq corfu-cycle t
        corfu-auto t
        corfu-auto-delay 0.1
        corfu-auto-prefix 2
        corfu-popupinfo-delay 0.5))

I use corfu in concert with orderless.

;; Emacs completion style that matches multiple regexps in any order
(use-package orderless
  :config
  (setq completion-styles '(orderless basic partial-completion)
        completion-category-overrides '((file (styles basic partial-completion)))
        orderless-component-separator "[ |]"))

Navigation and searching

The package Consult improves navigation and searching.

;; Consulting completing-read
(use-package consult
  :bind (:map custom-bindings-map
              ("C-x b" . consult-buffer)
              ("C-c r" . consult-ripgrep))
  :config
  (setq consult-preview-key (list :debounce 0.1 'any)))

PDF Tools

PDF Tools makes a huge improvement on the built-in doc-view-mode! Removing the header-line-format gives a very clean PDF-viewer; let’s add that to a key.

;; Emacs support library for PDF files
(use-package pdf-tools
  :defer t
  :mode "\\.pdf\\'"
  :bind (:map pdf-view-mode-map
              ("c" . (lambda ()
                       (interactive)
                       (if header-line-format
                           (setq header-line-format nil)
                         (nano-modeline-pdf-mode))))
              ("j" . pdf-view-next-line-or-next-page)
              ("k" . pdf-view-previous-line-or-previous-page))
  :hook (pdf-view-mode
         . (lambda ()
             (nano-modeline-pdf-mode)))
  :init (pdf-loader-install)
  :config (add-to-list 'revert-without-query ".pdf"))

Spelling

Jinx

Trying out jinx in favor of the builtin flyspell.

(use-package jinx
  :hook (emacs-startup . global-jinx-mode)
  :bind ("C-." . jinx-correct)
  :config
  (setq jinx-languages "en_US nb-no"))

Define word

This super neat package looks up the word at point. I use it a lot!

;; display the definition of word at point
(use-package define-word
  :defer t
  :bind (:map custom-bindings-map ("C-c D" . define-word-at-point)))

Move Text

For moving lines up and down, there is the appropriately named [Move Text](https://github.com/emacsfodder/move-text) package.

(use-package move-text
  :bind (:map custom-bindings-map
              ("C-M-<down>" . move-text-down)
              ("C-M-<up>" . move-text-up)))

Lorem ipsum

Do you ever want to insert some Lorem ipsum?

(use-package lorem-ipsum)

Now, run M-x lorem-ipsum-insert-paragraphs and get:

Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Donec hendrerit tempor tellus. Donec pretium posuere tellus. Proin quam nisl, tincidunt et, mattis eget, convallis nec, purus. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Nulla posuere. Donec vitae dolor. Nullam tristique diam non turpis. Cras placerat accumsan nulla. Nullam rutrum. Nam vestibulum accumsan nisl.

Org

I use Org mode extensively. Some of these configurations may be unfortunate, but it is a bit impractical to change, as I have years worth of org-files and want to avoid having to reformat a lot of files.

One example is org-adapt-indentation, which changed default value in version 9.5 of Org mode. Another is that I for some unknown reason decided to content within source content not be indented by two spaces (which is the default).

Note that I disable some safety features, so please don’t copy and paste mindlessly (see the documentation for org-confirm-babel-evaluate and org-export-allow-bind-keywords).

;; Outline-based notes management and organizer
(use-package org
  :defer t
  :config
  (setq org-adapt-indentation t
        org-hide-leading-stars t
        org-hide-emphasis-markers t
        org-pretty-entities t
        org-src-fontify-natively t
        org-startup-folded t
        org-edit-src-content-indentation 0))

LaTeX export

For LaTeX export, I default to using XeLaTeX for compilation, and the engrave-faces package for syntax highlighting source blocks after the Emacs color theme.

;; Convert font-lock faces to other formats
(use-package engrave-faces
  :defer t)

I have PDFs open directly in Emacs (PDF Tools). In addition, I have support for a couple of custom LaTeX classes.

;; LaTeX Back-End for Org Export Engine
(use-package ox-latex
  :ensure nil
  :after org
  :config
  (setq org-export-allow-bind-keywords t
        org-latex-src-block-backend 'engraved
        org-latex-pdf-process
        '("latexmk -pdflatex='xelatex -shell-escape -interaction nonstopmode' -pdf -f %f"))

  (add-to-list 'org-file-apps '("\\.pdf\\'" . emacs))

  (add-to-list 'org-latex-classes
               '("ifimaster"
                 "\\documentclass{ifimaster}
[DEFAULT-PACKAGES]
[PACKAGES]
[EXTRA]
\\usepackage{babel,csquotes,ifimasterforside,url,varioref}"
                 ("\\chapter{%s}" . "\\chapter*{%s}")
                 ("\\section{%s}" . "\\section*{%s}")
                 ("\\subsection{%s}" . "\\subsection*{%s}")
                 ("\\subsubsection{%s}" . "\\subsubsection*{%s}")
                 ("\\paragraph{%s}" . "\\paragraph*{%s}")
                 ("\\subparagraph{%s}" . "\\subparagraph*{%s}")))

  (add-to-list 'org-latex-classes
               '("easychair" "\\documentclass{easychair}"
                 ("\\section{%s}" . "\\section*{%s}")
                 ("\\subsection{%s}" . "\\subsection*{%s}")
                 ("\\subsubsection{%s}" . "\\subsubsection*{%s}")
                 ("\\paragraph{%s}" . "\\paragraph*{%s}")
                 ("\\subparagraph{%s}" . "\\subparagraph*{%s}"))))

Babel

Add a few languages for Org babel. In addition, don’t evaluate code on export by default.

;; Working with Code Blocks in Org
(use-package ob
  :ensure nil
  :after org
  :config
  (setq org-export-use-babel nil
        org-confirm-babel-evaluate nil)
  (org-babel-do-load-languages
   'org-babel-load-languages
   '((emacs-lisp . t)
     (python . t)
     (clojure . t))))

Default to use whatever interpreter is set by python-shell-interpreter.

;; Babel Functions for Python
(use-package ob-python
  :ensure nil
  :after (ob python)
  :config
  (setq org-babel-python-command python-shell-interpreter))

Tempo

Since version 9.2 of Org mode, typing <s to get a source block (and similar variants) has been tucked away in the Org Tempo library, hoping that users rather use C-c C-,. Hopefully I’ll stop typing <s at some point, and adapt the much saner C-c C-,.

;; Template expansion for Org structures
(use-package org-tempo
  :ensure nil
  :after org)

Org Modern

Touch up the appearance of org mode files with some fancy UTF-8 characters. I disable org-modern-block-fringe due to org-modern conflicting with org-adapt-indentation.

;; Modern looks for Org
(use-package org-modern
  :after org
  :hook (org-mode . org-modern-mode)
  :config
  (setq org-modern-block-fringe nil))

Org appear

Setting org-hide-emphasis-markers to t often makes it harder to edit markup (i have found myself sometimes reverting to fundamental-mode because of this). The package org-appear automatically shows the hidden markup when the cursor is on it.

(use-package org-appear
  :hook (org-mode . org-appear-mode)
  :config
  (setq org-appear-autosubmarkers t
        org-appear-autoentities t
        org-appear-autolinks t
        org-appear-inside-latex t))

GitHub flavored markdown

I guess I have to include my (semi-abandoned) mode ox-gfm for exporting org mode to GitHub Flavored Markdown.

;; Export Github Flavored Markdown from Org
(use-package ox-gfm
  :after (org))

Org present

org-present-mode provides a minimalistic slide view of an org-mode buffer. Together with org-modern, center-content-mode, focus-mode and a few other customizations, we get pretty decent looking slides!

(use-package org-present
  :after center-content-mode
  :hook ((org-present-mode
          . (lambda ()
              (jinx-mode -1)
              (org-modern-mode -1)
              (set (make-local-variable 'org-modern-hide-stars) t)
              (setq cursor-type nil)
              (org-modern-mode 1)
              (org-present-big)
              (org-display-inline-images)
              (focus-mode 1)
              (center-content-mode 1)))
         (org-present-mode-quit
          . (lambda ()
              (jinx-mode 1)
              (org-modern-mode -1)
              (setq org-modern-hide-stars (default-value 'org-modern-hide-stars))
              (setq cursor-type (default-value 'cursor-type))
              (org-modern-mode 1)
              (focus-mode -1)
              (center-content-mode -1))))
  :config
  (defun org-present-next-item ()
    (interactive)
    (unless (re-search-forward "^+" nil t)
      (org-present-next)))

  (defun org-present-prev-item ()
    (interactive)
    (unless (re-search-backward "^+" nil t)
      (org-present-prev)))

  :bind (:map org-present-mode-keymap
              ("<next>" . org-present-next-item)
              ("C-<right>" . org-present-next-item)
              ("<prior>" . org-present-prev-item)
              ("C-<left>" . org-present-prev-item)))

Markdown

Markdown is pretty nice, especially when collaborating with others (as most people don’t use org), and nicer still when combined with Pandoc! I set fill-column to 72 as it’s

;; Emacs Major mode for Markdown-formatted files
(use-package markdown-mode
  :defer t
  :hook (markdown-mode . (lambda () (setq fill-column 72))))

Direnv

I use nix in most of my projects, to specify the programs needed in order work on that project. In combination with direnv, these programs are only available within those projects; that is: when I cd into a Javascript project, then I can call npm, but in my system globally, there is no trace of it. The package envrc helps Emacs and direnv play nice.

;; direnv integration
(use-package envrc
  :if (executable-find "direnv")
  :init
  (setq envrc-debug t)
  (add-hook 'after-init-hook (lambda () (envrc-global-mode 1)))
  (advice-add 'cider-jack-in :around #'envrc-propagate-environment))

LLM

The ChatGPT client gptel needs an API key from the OpenAI API. This key can be stored in your .authinfo file by adding a line like this:

machine api.openai.com password OPEN-AI-KEY

Then the gptel-api-key can be set using auth source.

Default to using llama3, a local LLM.

(use-package gptel
  :defer t
  :hook ((gptel-mode . (lambda () (visual-line-mode 1)))
         (gptel-mode . (lambda () (auto-fill-mode 0))))
  :config
  (setq gptel-backend (gptel-make-ollama "Ollama"
                        :host "localhost:11434"
                        :stream t
                        :models '("llama3.1:8b-instruct-q8_0"))
        gptel-model "llama3"
        gptel-api-key (auth-source-pick-first-password
                       :host "api.openai.com")))

Multiple cursors

I use this all the time. Perhaps more than I should?

;; Multiple cursors for Emacs
(use-package multiple-cursors
  :defer t
  :hook ((multiple-cursors-mode-enabled . (lambda () (corfu-mode -1)))
         (multiple-cursors-mode-disabled . (lambda () (corfu-mode 1))))
  :bind (:map custom-bindings-map
              ("C-c e" . mc/edit-lines)
              ("C-c a" . mc/mark-all-like-this)
              ("C-c n" . mc/mark-next-like-this)))

Expand region

This is neat, and I use it way less than I should.

;; Increase selected region by semantic units
(use-package expand-region
  :bind (:map custom-bindings-map ("C-=" . er/expand-region)))

Try

Try is my own package for trying out packages without installing them. It is the most useful of my packages (IMO).

;; Try out Emacs packages
(use-package try
  :defer t)

Interactive functions

<sec:defuns>

just-one-space removes all whitespace around a point - giving it a negative argument it removes newlines as well. We wrap a interactive function around it to be able to bind it to a key. In Emacs 24.4 cycle-spacing was introduced, and it works like just-one-space, but when run in succession it cycles between one, zero and the original number of spaces.

(defun cycle-spacing-delete-newlines ()
  "Removes whitespace before and after the point."
  (interactive)
  (if (version< emacs-version "24.4")
      (just-one-space -1)
    (cycle-spacing -1)))

Often I want to find other occurrences of a word I’m at, or more specifically the symbol (or tag) I’m at. The isearch-forward-symbol-at-point in Emacs 24.4 works well for this, but I don’t want to be bothered with the isearch interface. Rather jump quickly between occurrences of a symbol, or if non is found, don’t do anything.

(defun jump-to-symbol-internal (&optional backwardp)
  "Jumps to the next symbol near the point if such a symbol
exists. If BACKWARDP is non-nil it jumps backward."
  (let* ((point (point))
         (bounds (find-tag-default-bounds))
         (beg (car bounds)) (end (cdr bounds))
         (str (isearch-symbol-regexp (find-tag-default)))
         (search (if backwardp 'search-backward-regexp
                   'search-forward-regexp)))
    (goto-char (if backwardp beg end))
    (funcall search str nil t)
    (cond ((<= beg (point) end) (goto-char point))
          (backwardp (forward-char (- point beg)))
          (t  (backward-char (- end point))))))

(defun jump-to-previous-like-this ()
  "Jumps to the previous occurrence of the symbol at point."
  (interactive)
  (jump-to-symbol-internal t))

(defun jump-to-next-like-this ()
  "Jumps to the next occurrence of the symbol at point."
  (interactive)
  (jump-to-symbol-internal))

I sometimes regret killing the *scratch*-buffer, and have realized I never want to actually kill it. I just want to get it out of the way, and clean it up. The function below does just this for the *scratch*-buffer, and works like kill-this-buffer for any other buffer. It removes all buffer content and buries the buffer (this means making it the least likely candidate for other-buffer).

(defun kill-this-buffer-unless-scratch ()
  "Works like `kill-this-buffer' unless the current buffer is the
*scratch* buffer. In witch case the buffer content is deleted and
the buffer is buried."
  (interactive)
  (if (not (string= (buffer-name) "*scratch*"))
      (kill-this-buffer)
    (delete-region (point-min) (point-max))
    (switch-to-buffer (other-buffer))
    (bury-buffer "*scratch*")))

To duplicate either selected text or a line we define this interactive function.

(defun duplicate-thing (comment)
  "Duplicates the current line, or the region if active. If an argument is
given, the duplicated region will be commented out."
  (interactive "P")
  (save-excursion
    (let ((start (if (region-active-p) (region-beginning) (line-beginning-position)))
          (end   (if (region-active-p) (region-end) (line-end-position)))
          (fill-column most-positive-fixnum))
      (goto-char end)
      (unless (region-active-p)
        (newline))
      (insert (buffer-substring start end))
      (when comment (comment-region start end)))))

To tidy up a buffer we define this function borrowed from simenheg.

(defun tidy ()
  "Ident, untabify and unwhitespacify current buffer, or region if active."
  (interactive)
  (let ((beg (if (region-active-p) (region-beginning) (point-min)))
        (end (if (region-active-p) (region-end) (point-max))))
    (indent-region beg end)
    (whitespace-cleanup)
    (untabify beg (if (< end (point-max)) end (point-max)))))

Org mode does currently not support synctex (which enables you to jump from a point in your TeX-file to the corresponding point in the pdf), and it seems like a tricky problem.

Calling this function from an org-buffer jumps to the corresponding section in the exported pdf (given that the pdf-file exists), using pdf-tools.

(defun org-sync-pdf ()
  (interactive)
  (let ((headline (nth 4 (org-heading-components)))
        (pdf (concat (file-name-base (buffer-name)) ".pdf")))
    (when (file-exists-p pdf)
      (find-file-other-window pdf)
      (pdf-links-action-perform
       (cl-find headline (pdf-info-outline pdf)
                :key (lambda (alist) (cdr (assoc 'title alist)))
                :test 'string-equal)))))

The opposite of fill paragraph (from EmacsWiki),

(defun unfill-paragraph ()
  (interactive)
  (let ((fill-column most-positive-fixnum))
    (fill-paragraph nil (region-active-p))))

I don’t enjoy writing out today’s date, so let’s tuck that into a function.

(defun insert-todays-date ()
  (interactive)
  (insert (format-time-string "%Y-%m-%d")))

Advice

An advice can be given to a function to make it behave differently. This advice makes eval-last-sexp (bound to C-x C-e) replace the sexp with the value.

(defadvice eval-last-sexp (around replace-sexp (arg) activate)
  "Replace sexp when called with a prefix argument."
  (if arg
      (let ((pos (point)))
        ad-do-it
        (goto-char pos)
        (backward-kill-sexp)
        (forward-sexp))
    ad-do-it))

When interactively changing the theme (using M-x load-theme), the current custom theme is not disabled. This often gives weird-looking results; we can advice load-theme to always disable themes currently enabled themes.

(defadvice load-theme
    (before disable-before-load (theme &optional no-confirm no-enable) activate)
  (mapc 'disable-theme custom-enabled-themes))

global-scale-mode

These functions provide something close to text-scale-mode, but for every buffer, including the minibuffer and mode line.

(let* ((default (face-attribute 'default :height))
       (size default))

  (defun global-scale-default ()
    (interactive)
    (global-scale-internal (setq size default)))

  (defun global-scale-up ()
    (interactive)
    (global-scale-internal (setq size (+ size 20))))

  (defun global-scale-down ()
    (interactive)
    (global-scale-internal (setq size (- size 20))))

  (defun global-scale-internal (arg)
    (set-face-attribute 'default (selected-frame) :height arg)
    (set-transient-map
     (let ((map (make-sparse-keymap)))
       (define-key map (kbd "C-=") 'global-scale-up)
       (define-key map (kbd "C-+") 'global-scale-up)
       (define-key map (kbd "C--") 'global-scale-down)
       (define-key map (kbd "C-0") 'global-scale-default) map))))

Mode specific

Eglot

I am using eglot, which is built in from emacs 29.1. Some performance issues led me to set eglot-events-buffer-size to 0.

(use-package eglot
  :defer t
  :hook (eglot-managed-mode . (lambda ()
                                (eglot-inlay-hints-mode -1)
                                (add-hook 'before-save-hook 'eglot-format nil t)))
  :config
  (setq eglot-events-buffer-size 0)
  (add-to-list 'eglot-server-programs
               '(web-mode . ("svelteserver" "--stdio"))))

Compilation

I often run latexmk -pdf -pvc in a compilation buffer, which recompiles the latex-file whenever it is changed. This often results in annoyingly large compilation buffers; the following snippet limits the buffer size in accordance with comint-buffer-maximum-size, which defaults to 1024 lines.

(use-package comint
  :ensure nil
  :bind (:map comint-mode-map ("C-l" . comint-clear-buffer))
  :hook (comint-mode . (lambda () (auto-fill-mode -1)))
  :config (add-hook 'compilation-filter-hook 'comint-truncate-buffer))

vterm

vterm is a fully capable terminal emulator, and I use it exclusively.

Inspired by torenord, I maintain quick access to vterm buffers with bindings M-1 to M-9. In addition, the C-z toggles between the last visited vterm, and the last visited non-vterm buffer.

Fresh vterm buffers spawns with the directory given by vc-root-dir if it exists and default-directory otherwise.

;; A terminal via libvterm
(use-package vterm
  :defer t
  :preface
  (defvar vterms nil)

  (defun toggle-vterm (&optional n)
    (interactive)
    (setq vterms (seq-filter 'buffer-live-p vterms))
    (let ((default-directory (or (vc-root-dir) default-directory)))
     (cond ((numberp n) (push (vterm n) vterms))
           ((null vterms) (push (vterm 1) vterms))
           ((seq-contains-p vterms (current-buffer))
            (switch-to-buffer (car (seq-difference (buffer-list) vterms))))
           (t (switch-to-buffer (car (seq-intersection (buffer-list) vterms)))))))

  :bind (:map custom-bindings-map
              ("C-z" . toggle-vterm)
              ("M-1" . (lambda () (interactive) (toggle-vterm 1)))
              ("M-2" . (lambda () (interactive) (toggle-vterm 2)))
              ("M-3" . (lambda () (interactive) (toggle-vterm 3)))
              ("M-4" . (lambda () (interactive) (toggle-vterm 4)))
              ("M-5" . (lambda () (interactive) (toggle-vterm 5)))
              ("M-6" . (lambda () (interactive) (toggle-vterm 6)))
              ("M-7" . (lambda () (interactive) (toggle-vterm 7)))
              ("M-8" . (lambda () (interactive) (toggle-vterm 8)))
              ("M-9" . (lambda () (interactive) (toggle-vterm 9))))

  :config
  ;; Don't query about killing vterm buffers, just kill it
  (defadvice vterm (after kill-with-no-query nil activate)
    (set-process-query-on-exit-flag (get-buffer-process ad-return-value) nil)))

Lisp

I use Paredit when editing lisp code, we enable this for all lisp-modes. Paredit version 25 seems to interfere with REPL-modes, and unbinding RET is the proposed fix.

;; minor mode for editing parentheses
(use-package paredit
  :defer t
  :bind (:map paredit-mode-map ("RET" . nil))
  :hook ((cider-repl-mode
          clojure-mode
          ielm-mode
          racket-mode
          racket-repl-mode
          slime-repl-mode
          lisp-mode
          emacs-lisp-mode
          lisp-interaction-mode
          scheme-mode) 
         . paredit-mode))

Emacs Lisp

In emacs-lisp-mode we can enable eldoc-mode to display information about a function or a variable in the echo area.

(add-hook 'emacs-lisp-mode-hook 'turn-on-eldoc-mode)
(add-hook 'lisp-interaction-mode-hook 'turn-on-eldoc-mode)

Clojure

A very simple setup for Clojure. Cider works pretty much out of the box!

(use-package clojure-mode
  :config
  (setq clojure-toplevel-inside-comment-form t)
  (define-clojure-indent
   (match 1)))
;; Clojure Interactive Development Environment
(use-package cider
  :defer t
  :bind (:map cider-repl-mode-map ("C-l" . cider-repl-clear-buffer))
  :config
  (setq cider-save-file-on-load t
        cider-repl-pop-to-buffer-on-connect nil))
;; Commands for refactoring Clojure code
(use-package clj-refactor
  :hook (cider-mode . clj-refactor-mode)
  :defer t)

Racket

A minimal setup for Racket.

;; Major mode for Racket language
(use-package racket-mode
  :defer t)

Common lisp

Note that I haven’t used Common Lisp for a very long time, and this setup might be broken. I keep it around for reference.

I use Slime along with lisp-mode to edit Common Lisp code. Slime provides code evaluation and other great features, a must have for a Common Lisp developer. You can install the Common Lisp slime counterpart using Quicklisp, creating a helper that can be loaded.

We can specify what Common Lisp program Slime should use (I use SBCL). More sensible loop indentation is borrowed from simenheg.

;; Superior Lisp Interaction Mode for Emacs
(use-package slime
  :disabled
  :defer t
  :bind (:map slime-repl-mode-map ("C-l" . slime-repl-clear-buffer))
  :hook (common-lisp-mode . activate-slime-helper)
  :config
  (when (file-exists-p "~/.quicklisp/slime-helper.el")
    (load (expand-file-name "~/.quicklisp/slime-helper.el")))

  (setq inferior-lisp-program "sbcl")

  (setq lisp-loop-forms-indentation   6
        lisp-simple-loop-indentation  2
        lisp-loop-keyword-indentation 6))

Python

(setq python-shell-interpreter "python3.12")
(add-hook 'python-mode-hook
          (lambda () (setq forward-sexp-function nil)))

C

The c-mode-common-hook is a general hook that work on all C-like languages (C, C++, Java, etc…). I like being able to quickly compile using C-c C-c (instead of M-x compile), a habit from latex-mode.

(defun c-setup ()
  (local-set-key (kbd "C-c C-c") 'compile))

(add-hook 'c-mode-hook 'c-setup)

Java

Some statements in Java appear often, and become tedious to write out. We can use abbrevs to speed this up.

(define-abbrev-table 'java-mode-abbrev-table
  '(("psv" "public static void main(String[] args) {" nil 0)
    ("sopl" "System.out.println" nil 0)
    ("sop" "System.out.printf" nil 0)))

To be able to use the abbrev table defined above, abbrev-mode must be activated.

(add-hook 'java-mode-hook 'eglot-ensure)

Kotlin

(use-package kotlin-mode
  :hook (kotlin-mode . eglot-ensure))

Assembler

When writing assembler code I use # for comments. By defining comment-start we can add comments using M-; like in other programming modes. Also in assembler should one be able to compile using C-c C-c.

(defun asm-setup ()
  (setq comment-start "#")
  (local-set-key (kbd "C-c C-c") 'compile))

(add-hook 'asm-mode-hook 'asm-setup)

LaTeX

;; Integrated environment for *TeX*
(use-package auctex)

Erlang

Erlang mode works out of the box.

;; Erlang major mode
(use-package erlang
  :defer t)

Nix

;; Major mode for editing .nix files
(use-package nix-mode
  :defer t
  :hook (nix-mode . eglot-ensure))

Haskell

haskell-doc-mode is similar to eldoc, it displays documentation in the echo area. Haskell has several indentation modes - I prefer using haskell-indent.

;; A Haskell editing mode
(use-package haskell-mode
  :defer t
  :hook ((haskell-mode . interactive-haskell-mode)
         (haskell-mode . turn-on-haskell-doc-mode)
         (haskell-mode . turn-on-haskell-indent)))

Maude

Use --- for comments in Maude.

;; Emacs mode for the programming language Maude
(use-package maude-mode
  :defer t
  :hook (maude-mode . (lambda () (setq-local comment-start "---")))
  :config
  (add-to-list 'maude-command-options "-no-wrap"))

Minizinc

Provide a default compile-command.

(defun minizinc-setup-compile-command ()
  (let ((command (concat "minizinc " (buffer-file-name) " "))
        (f (concat (file-name-base (buffer-file-name)) ".dzn")))
    (local-set-key (kbd "C-c C-c") 'recompile)
    (setq-local compile-command (concat command (if (file-exists-p f) f "")))))

Use minizinc-mode, and hook up the minizinc-setup-compile-command above.

;; Major mode for MiniZinc code
(use-package minizinc-mode
  :disabled
  :defer t
  :mode "\\.mzn\\'"
  :hook (minizinc-mode . minizinc-setup-compile-command))

Coq

Proof General is really great for working with proof assistants. I have only tried it with Coq.

;; A generic Emacs interface for proof assistants
(use-package proof-general
  :disabled
  :defer t)

For completions, I use company-coq.

;; A collection of extensions PG's Coq mode
(use-package company-coq
  :disabled
  :defer t
  :hook (coq-mode . company-coq-mode))

Rust

;; Rust development environment
(use-package rustic
  :defer t
  :config
  (setq rustic-lsp-client 'eglot))

Go

;; Major mode for the Go programming language
(use-package go-mode
  :defer t
  :mode "\\.go\\'"
  :hook (go-mode . eglot-ensure))

Lua

;; a major-mode for editing Lua scripts
(use-package lua-mode
  :defer t)

Webdev

My webdev setup isn’t much, but with eglot and Tree-sitter, I don’t find myself missing much. It depends on Tree-sitter, which was added in emacs 29.1.

;; Major mode for editing JavaScript
(use-package js
  :ensure nil
  :defer t
  :mode "\\.jsx?\\'"
  :hook (js-ts-mode . eglot-ensure))

Similarly for TypeScript.

;; tree sitter support for TypeScript
(use-package typescript-ts-mode
  :ensure nil
  :defer t
  :mode "\\.tsx?\\'"
  :hook (tsx-ts-mode . eglot-ensure))

I am using Svelte for some projects, where I find web-mode along with the Svelte Language Server to work well.

(use-package web-mode
  :defer t
  :mode "\\.svelte\\'"
  :hook (web-mode . eglot-ensure)
  :config
  (add-to-list 'web-mode-engines-alist '("svelte" . "\\.svelte\\'")))

BQN

(use-package bqn-mode
  :bind (:map bqn-mode-map ("C-c C-c" . bqn-comint-send-dwim))
  :hook (bqn-mode . (lambda () (set-input-method "BQN-Z"))))

Z3

I mostly use Z3 as a Python library, but occasionally I’ll run some SMT-LIB code directly.

;; z3/SMTLIBv2 interactive development
(use-package z3-mode
  :disabled
  :defer t)

Swift

(use-package swift-mode
  :hook (swift . auto-revert-mode))

Which key

Which key is nice for discoverability.

;; Display available keybindings in popup
(use-package which-key
  :config
  (which-key-mode 1))

Bindings for built-ins

(use-package emacs
  :bind (:map custom-bindings-map
              ("M-u" . upcase-dwim)
              ("M-c" . capitalize-dwim)
              ("M-l" . downcase-dwim)
              ("M-]" . other-frame)
              ("C-j" . newline-and-indent)
              ("C-c s" . ispell-word)
              ("C-c v" . visible-mode)))

Bindings for functions defined above.

(use-package emacs
  :bind (("M-p" . jump-to-previous-like-this)
         ("M-n" . jump-to-next-like-this)
         :map custom-bindings-map
         ("M-,"     . jump-to-previous-like-this)
         ("M-."     . jump-to-next-like-this)
         ("C-x k"   . kill-this-buffer-unless-scratch)
         ("C-c C-0" . global-scale-default)
         ("C-c C-=" . global-scale-up)
         ("C-c C-+" . global-scale-up)
         ("C-c C--" . global-scale-down)
         ("C-c j"   . cycle-spacing-delete-newlines)
         ("C-c d"   . duplicate-thing)
         ("<C-tab>" . tidy)
         ("C-c t"   . insert-todays-date)
         ("C-c q"   . unfill-paragraph))
  :config
  (define-key custom-bindings-map (kbd "C-c .") (cycle-themes)))

Lastly we need to activate the map by creating and activating the minor-mode.

(define-minor-mode custom-bindings-mode
  "A mode that activates custom-bindings."
  :init-value t
  :keymap custom-bindings-map)

License

My Emacs configurations written in Org mode.

Copyright (c) 2013 - 2023 Lars Tveito

This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License along with this program. If not, see http://www.gnu.org/licenses/.

About

My Emacs configurations written in Org mode

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published