Need help with dot-emacs?
Click the “chat” button below for chat support from the developer who created it, or find similar developers for support.

About the developer

236 Stars 46 Forks GNU General Public License v3.0 223 Commits 1 Opened issues


My Emacs configurations written in Org mode

Services available


Need anything else?

Contributors list

# 355,940
209 commits

+TITLE: Emacs configuration file

+AUTHOR: Lars Tveito

+BABEL: :cache yes

+LATEX_HEADER: \usepackage{parskip}

+LATEX_HEADER: \usepackage{inconsolata}

+LATEX_HEADER: \usepackage[utf8]{inputenc}

+PROPERTY: header-args :tangle yes

  • 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:

Clone the repo. #+BEGINSRC sh :tangle no git clone #+ENDSRC

Backup your old =~/.emacs.d= (if necessary). #+BEGINSRC sh :tangle no mv ~/.emacs.d ~/.emacs.d-bak #+ENDSRC

Backup your old =~/.emacs=-file (if necessary). #+BEGINSRC sh :tangle no mv ~/.emacs ~/.emacs-bak #+ENDSRC

And finally #+BEGINSRC sh :tangle no mv dot-emacs ~/.emacs.d #+ENDSRC

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!

  • Configurations ** Meta

All changes to the configuration should be done in, not in =init.el=. Any changes in the =init.el= will be overwritten by saving 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 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:

#+BEGIN_SRC emacs-lisp :tangle no ;; 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 "")) ;; 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")) #+END_SRC

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:

#+BEGINSRC sh :tangle no git update-index --assume-unchanged init.el #+ENDSRC

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

#+BEGINSRC sh :tangle no git update-index --no-assume-unchanged init.el #+ENDSRC

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

#+BEGINSRC emacs-lisp ;;; -- lexical-binding: t -- #+ENDSRC

The =init.el= should (after the first run) mirror the source blocks in the 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.

#+BEGIN_SRC emacs-lisp (defun tangle-init () "If the current buffer is '' the code-blocks are tangled, and the tangled file is compiled." (when (equal (buffer-file-name) (expand-file-name (concat user-emacs-directory ""))) ;; 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) #+END_SRC

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

#+BEGINSRC emacs-lisp (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)))) #+ENDSRC

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.

#+BEGINSRC emacs-lisp (let ((old-gc-treshold gc-cons-threshold)) (setq gc-cons-threshold most-positive-fixnum) (add-hook 'after-init-hook (lambda () (setq gc-cons-threshold old-gc-treshold)))) #+ENDSRC

** Packages

Managing extensions for Emacs is simplified using =package= which is built in to Emacs 24 and newer. To load downloaded packages we need to initialize =package=. =cl= is a library that contains many functions from Common Lisp, and comes in handy quite often, so we want to make sure it's loaded, along with =package=, which is obviously needed.

#+BEGINSRC emacs-lisp (require 'package) (package-initialize) #+ENDSRC

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

#+BEGINSRC emacs-lisp (setq package-archives '(("gnu" . "") ("MELPA" . "")) package-archive-priorities '(("MELPA" . 5) ("gnu" . 0))) #+ENDSRC

The configuration assumes that the packages listed below are installed. To ensure we install missing packages if they are missing.

#+BEGINSRC emacs-lisp (let* ((package--builtins nil) (packages '(auto-compile ; automatically compile Emacs Lisp libraries cider ; Clojure Interactive Development Environment clj-refactor ; Commands for refactoring Clojure code company ; Modular text completion framework company-coq ; A collection of extensions PG's Coq mode counsel ; Various completion functions using Ivy counsel-projectile ; Ivy integration for Projectile define-word ; display the definition of word at point diff-hl ; Highlight uncommitted changes using VC doom-themes ; An opinionated pack of modern color-themes erlang ; Erlang major mode expand-region ; Increase selected region by semantic units focus ; Dim color of text in surrounding sections golden-ratio ; Automatic resizing windows to golden ratio haskell-mode ; A Haskell editing mode jedi ; Python auto-completion for Emacs js2-mode ; Improved JavaScript editing mode lsp-mode ; LSP mode lsp-java ; Java support for lsp-mode magit ; control Git from Emacs markdown-mode ; Emacs Major mode for Markdown-formatted files maude-mode ; Emacs mode for the programming language Maude minizinc-mode ; Major mode for MiniZinc code multiple-cursors ; Multiple cursors for Emacs olivetti ; Minor mode for a nice writing environment org ; Outline-based notes management and organizer org-bullets ; Show bullets in org-mode as UTF-8 characters org-roam ; Roam Research replica with Org-mode org-roam-server ; Org Roam Database Visualizer paredit ; minor mode for editing parentheses pdf-tools ; Emacs support library for PDF files projectile ; Manage and navigate projects in Emacs easily proof-general ; A generic Emacs interface for proof assistants racket-mode ; Major mode for Racket language slime ; Superior Lisp Interaction Mode for Emacs smex ; M-x interface with Ido-style fuzzy matching try ; Try out Emacs packages vterm ; A terminal via libvterm which-key ; Display available keybindings in popup z3-mode))) ; z3/SMTLIBv2 interactive development (when (memq window-system '(mac ns)) (push 'exec-path-from-shell packages) (push 'reveal-in-osx-finder packages)) (let ((packages (seq-remove 'package-installed-p packages))) (print packages) (when packages ;; Install uninstalled packages (package-refresh-contents) (mapc 'package-install packages)))) #+ENDSRC

** Mac OS X

I run this configuration mostly on Mac OS X, so we need a couple of settings to make things work smoothly. In the package section =exec-path-from-shell= is included (only if you're running OS X), this is to include environment-variables from the shell. It makes using Emacs along with external processes a lot simpler. I also prefer using the =Command=-key as the =Meta=-key.

#+BEGINSRC emacs-lisp (when (memq window-system '(mac ns)) (setq ns-pop-up-frames nil mac-option-modifier nil mac-command-modifier 'meta x-select-enable-clipboard t) (exec-path-from-shell-initialize) (when (fboundp 'mac-auto-operator-composition-mode) (mac-auto-operator-composition-mode 1))) #+ENDSRC

** Sane defaults

These are what /I/ consider to be saner defaults.

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

#+BEGINSRC emacs-lisp (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 inhibit-startup-screen t ; No splash screen please initial-scratch-message nil ; Clean scratch buffer recentf-max-saved-items 100 ; 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)) #+ENDSRC

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.

#+BEGINSRC emacs-lisp (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 #+ENDSRC

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).

#+BEGINSRC emacs-lisp (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)))) #+ENDSRC

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

#+BEGINSRC emacs-lisp (fset 'yes-or-no-p 'y-or-n-p) #+ENDSRC

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

#+BEGIN_SRC emacs-lisp (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))
((".*" ,emacs-autosave-directory t))) #+END_SRC

Set =utf-8= as preferred coding system.

#+BEGINSRC emacs-lisp (set-language-environment "UTF-8") #+ENDSRC

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.

#+BEGINSRC emacs-lisp (put 'narrow-to-region 'disabled nil) #+ENDSRC

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

#+BEGINSRC emacs-lisp (add-hook 'doc-view-mode-hook 'auto-revert-mode) #+ENDSRC

** Modes

There are some modes that are enabled by default that I don't find particularly useful. We create a list of these modes, and disable all of these.

#+BEGINSRC emacs-lisp (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)) #+ENDSRC

Let's apply the same technique for enabling modes that are disabled by default.

#+BEGIN_SRC emacs-lisp (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-company-mode ; Auto-completion everywhere global-diff-hl-mode ; Highlight uncommitted changes global-so-long-mode ; Mitigate performance for long lines counsel-projectile-mode ; Manage and navigate projects recentf-mode ; Recently opened files show-paren-mode ; Highlight matching parentheses which-key-mode)) ; Available keybindings in popup (funcall mode 1))

(when (version< emacs-version "24.4") (eval-after-load 'auto-compile '((auto-compile-on-save-mode 1)))) ; compile .el files on save #+END_SRC

** Visual

Change the color-theme to =leuven=.

#+BEGINSRC emacs-lisp (load-theme 'doom-one-light t) #+ENDSRC

=leuven= is my preferred light theme, but =monokai= makes a very nice dark theme. I want to be able to cycle between these.

#+BEGINSRC emacs-lisp (defun cycle-themes () "Returns a function that lets you cycle your themes." (let ((themes '#1=(doom-one-light doom-one . #1#))) (lambda () (interactive) ;; Rotates the thme cycle and changes the current theme. (load-theme (car (setq themes (cdr themes))) t) (message (concat "Switched to " (symbol-name (car themes))))))) #+ENDSRC

Use the [[][Inconsolata]] font if it's installed on the system.

#+BEGINSRC emacs-lisp (cond ((member "Hasklig" (font-family-list)) (set-face-attribute 'default nil :font "Hasklig-14")) ((member "Inconsolata" (font-family-list)) (set-face-attribute 'default nil :font "Inconsolata-14"))) #+ENDSRC

Let's pick out the my favorite elements from [[][elegant emacs]]! It looks really nice.

#+BEGIN_SRC emacs-lisp (add-to-list 'default-frame-alist '(internal-border-width . 24))

;; simplified mode line (define-key mode-line-major-mode-keymap header-line)

(defun mode-line-render (left right) (let* ((available-width (- (window-total-width) (length left)))) (format (format "%%s %%%ds" available-width) left right)))

(setq-default header-line-format '((:eval (propertize (mode-line-render (format-mode-line (list (propertize "☰" 'face

(:inherit mode-line-buffer-id)
                            'help-echo "Mode(s) menu"
                            'mouse-face 'mode-line-highlight
                            'local-map   mode-line-major-mode-keymap)
                " %b "
                (if (and buffer-file-name (buffer-modified-p))
                    (propertize "(modified)" 'face
(:inherit font-lock-comment-face))))) (format-mode-line (propertize "%4l:%2c " 'face
(:inherit font-lock-comment-face))))
(:underline ,(face-foreground 'font-lock-comment-face))))))

(setq-default mode-line-format nil) #+END_SRC

New in Emacs 24.4 is the =prettify-symbols-mode=! It's neat.

#+BEGINSRC emacs-lisp (setq-default prettify-symbols-alist '(("lambda" . ?λ) ("delta" . ?Δ) ("gamma" . ?Γ) ("phi" . ?φ) ("psi" . ?ψ))) #+ENDSRC

[[][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.

#+BEGINSRC emacs-lisp (with-eval-after-load 'olivetti (setq-default olivetti-body-width 82) (remove-hook 'olivetti-mode-on-hook 'visual-line-mode)) #+ENDSRC

** Ivy

Let's try [[][Ivy]] in favor of helm.

#+beginsrc emacs-lisp (setq ivy-wrap t ivy-height 25 ivy-use-virtual-buffers t ivy-count-format "(%d/%d) " ivy-on-del-error-function 'ignore) (ivy-mode 1) #+endsrc

** PDF Tools

[[][PDF Tools]] makes a huge improvement on the built-in [[][doc-view-mode]]; the only drawback is the =pdf-tools-install= (which has to be executed before the package can be used) takes a couple of /seconds/ to execute. Instead of running it at init-time, we'll run it whenever a PDF is opened. Note that it's only slow on the first run!

#+BEGINSRC emacs-lisp (add-to-list 'auto-mode-alist '("\.pdf\'" . pdf-tools-install)) #+ENDSRC

#+BEGINSRC emacs-lisp (add-hook 'pdf-view-mode-hook (lambda () (setq header-line-format nil))) #+ENDSRC

** Completion

[[][Auto-Complete]] has been a part of my config for years, but I want to try out [[][company-mode]]. If I code in an environment with good completion, I've made an habit of trying to /guess/ function-names, and looking at the completions for the right one. So I want a pretty aggressive completion system, hence the no delay settings and short prefix length.

#+BEGINSRC emacs-lisp (setq company-idle-delay 0 company-echo-delay 0 company-dabbrev-downcase nil company-minimum-prefix-length 2 company-selection-wrap-around t company-transformers '(company-sort-by-occurrence company-sort-by-backend-importance)) #+ENDSRC

** Spelling

Flyspell offers on-the-fly spell checking. We can enable flyspell for all text-modes with this snippet.

#+BEGINSRC emacs-lisp (add-hook 'text-mode-hook 'turn-on-flyspell) #+ENDSRC

To use flyspell for programming there is =flyspell-prog-mode=, that only enables spell checking for comments and strings. We can enable it for all programming modes using the =prog-mode-hook=.

#+BEGINSRC emacs-lisp (add-hook 'prog-mode-hook 'flyspell-prog-mode) #+ENDSRC

When working with several languages, we should be able to cycle through the languages we most frequently use. Every buffer should have a separate cycle of languages, so that cycling in one buffer does not change the state in a different buffer (this problem occurs if you only have one global cycle). We can implement this by using a [[][closure]].

#+BEGINSRC emacs-lisp (defun cycle-languages () "Changes the ispell dictionary to the first element in ISPELL-LANGUAGES, and returns an interactive function that cycles the languages in ISPELL-LANGUAGES when invoked." (let ((ispell-languages '#1=("american" "norsk" . #1#))) (ispell-change-dictionary (car ispell-languages)) (lambda () (interactive) ;; Rotates the languages cycle and changes the ispell dictionary. (ispell-change-dictionary (car (setq ispell-languages (cdr ispell-languages))))))) #+ENDSRC

=flyspell= signals an error if there is no spell-checking tool is installed. We can advice =turn-on-flyspell= and =flyspell-prog-mode= to only try to enable =flyspell= if a spell-checking tool is available. Also we want to enable cycling the languages by typing =C-c l=, so we bind the function returned from =cycle-languages=.

#+BEGINSRC emacs-lisp (defadvice turn-on-flyspell (before check nil activate) "Turns on flyspell only if a spell-checking tool is installed." (when (executable-find ispell-program-name) (local-set-key (kbd "C-c l") (cycle-languages)))) #+ENDSRC

#+BEGINSRC emacs-lisp (defadvice flyspell-prog-mode (before check nil activate) "Turns on flyspell only if a spell-checking tool is installed." (when (executable-find ispell-program-name) (local-set-key (kbd "C-c l") (cycle-languages)))) #+ENDSRC

** Org

When editing org-files with source-blocks, we want the source blocks to be themed as they would in their native mode.

#+BEGINSRC emacs-lisp (setq org-src-fontify-natively t org-src-tab-acts-natively t org-confirm-babel-evaluate nil org-edit-src-content-indentation 0) #+ENDSRC

This is quite an ugly fix for allowing code markup for expressions like ="this string"=, because the quotation marks causes problems.

#+BEGINSRC emacs-lisp (with-eval-after-load 'org (require 'org-tempo) (setcar (nthcdr 2 org-emphasis-regexp-components) " \t\n,") (custom-set-variables `(org-emphasis-alist ',org-emphasis-alist))) #+ENDSRC

Enable org-bullets when opening org-files.

#+BEGINSRC emacs-lisp (add-hook 'org-mode-hook (lambda () (org-bullets-mode 1))) #+ENDSRC

*** Org Roam

Trying out [[][org-roam]] for organizing notes.

#+begin_src emacs-lisp (setq org-roam-directory "~/Dropbox/org-roam") (add-hook 'after-init-hook 'org-roam-mode)

(setq org-roam-dailies-capture-templates '(("d" "dagbok" entry #'org-roam-capture--get-point "\n* %?" :file-name "daily/dagbok-%" :head "#+title: Dagbok %\n")

    ("r" "reading" entry
     "\n* %?"
     :file-name "daily/reading-%"
     :head "#+title: Reading %\n")))

(with-eval-after-load 'org-roam (define-key org-roam-mode-map (kbd "C-c r l") 'org-roam) (define-key org-roam-mode-map (kbd "C-c r d") 'org-roam-dailies-today) (define-key org-roam-mode-map (kbd "C-c r f") 'org-roam-find-file) (define-key org-roam-mode-map (kbd "C-c r g") 'org-roam-graph)

(define-key org-mode-map (kbd "C-c r i") 'org-roam-insert) (define-key org-mode-map (kbd "C-c r I") 'org-roam-insert-immediate)) #+end_src

#+begin_src emacs-lisp (setq org-roam-server-host "" org-roam-server-port 8080 org-roam-server-authenticate nil org-roam-server-export-inline-images t org-roam-server-serve-files nil org-roam-server-served-file-extensions '("pdf" "mp4" "ogv") org-roam-server-network-poll t org-roam-server-network-arrows nil org-roam-server-network-label-truncate t org-roam-server-network-label-truncate-length 60 org-roam-server-network-label-wrap-length 20) #+end_src

** 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.

#+BEGINSRC emacs-lisp (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))) #+ENDSRC

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.

#+BEGIN_SRC emacs-lisp (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)) #+END_SRC

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=).

#+BEGINSRC emacs-lisp (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"))) #+ENDSRC

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

#+BEGINSRC emacs-lisp (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) (point-at-bol))) (end (if (region-active-p) (region-end) (point-at-eol))) (fill-column most-positive-fixnum)) (goto-char end) (unless (region-active-p) (newline)) (insert (buffer-substring start end)) (when comment (comment-region start end))))) #+ENDSRC

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

#+BEGINSRC emacs-lisp (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))))) #+ENDSRC

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.

#+BEGINSRC emacs-lisp (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))))) #+ENDSRC

** 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.

#+BEGINSRC emacs-lisp (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)) #+ENDSRC

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.

#+BEGINSRC emacs-lisp (defadvice load-theme (before disable-before-load (theme &optional no-confirm no-enable) activate) (mapc 'disable-theme custom-enabled-themes)) #+ENDSRC

** global-scale-mode

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

#+BEGIN_SRC emacs-lisp (let* ((default (face-attribute 'default :height)) (size default))

 (defun global-scale-default ()
   (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-temporary-overlay-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 ** LSP

LSP mode works really well, especially with languages like Java, which traditionally has had quite poor Emacs support.

#+BEGINSRC emacs-lisp (with-eval-after-load 'lsp-mode (define-key lsp-mode-map (kbd "C-c f") lsp-command-map) (add-hook 'lsp-mode-hook #'lsp-enable-which-key-integration)) #+ENDSRC

** 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.

#+BEGINSRC emacs-lisp (add-hook 'compilation-filter-hook 'comint-truncate-buffer) #+ENDSRC

** Shell

Inspired by [[][torenord]], I maintain quick access to shell buffers with bindings ~M-1~ to ~M-9~. In addition, the ~M-§~ (on an international English keyboard) toggles between the last visited shell, and the last visited non-shell buffer. The following functions facilitate this, and are bound in the [[Key bindings]] section.

#+BEGIN_SRC emacs-lisp (let ((last-vterm "")) (defun toggle-vterm () (interactive) (cond ((string-match-p "^\vterm<[1-9][0-9]*>$" (buffer-name)) (goto-non-vterm-buffer)) ((get-buffer last-vterm) (switch-to-buffer last-vterm)) (t (vterm (setq last-vterm "vterm<1>")))))

 (defun switch-vterm (n)
   (let ((buffer-name (format "vterm" n)))
     (setq last-vterm buffer-name)
     (cond ((get-buffer buffer-name)
            (switch-to-buffer buffer-name))
           (t (vterm buffer-name)
              (rename-buffer buffer-name)))))

(defun goto-non-vterm-buffer () (let* ((r "^\vterm$") (vterm-buffer-p (lambda (b) (string-match-p r (buffer-name b)))) (non-vterms (cl-remove-if vterm-buffer-p (buffer-list)))) (when non-vterms (switch-to-buffer (car non-vterms))))))


Don't query whether or not the ~shell~-buffer should be killed, just kill it.

#+BEGINSRC emacs-lisp (defadvice vterm (after kill-with-no-query nil activate) (set-process-query-on-exit-flag (get-buffer-process ad-return-value) nil)) #+ENDSRC

Use zsh:

#+BEGINSRC emacs-lisp (setq vterm-shell "/usr/local/bin/zsh") #+ENDSRC

I'd like the =C-l= to work more like the standard terminal (which works like running =clear=), and resolve this by simply removing the buffer-content. Mind that this is not how =clear= works, it simply adds a bunch of newlines, and puts the prompt at the top of the window, so it does not remove anything. In Emacs removing stuff is less of a worry, since we can always undo!

#+BEGINSRC emacs-lisp (defun clear-comint () "Runs

comint-truncate-buffer' with the
comint-buffer-maximum-size' set to zero." (interactive) (let ((comint-buffer-maximum-size 0)) (comint-truncate-buffer))) #+ENDSRC

** Lisp

I use =Paredit= when editing lisp code, we enable this for all lisp-modes.

#+BEGINSRC emacs-lisp (dolist (mode '(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)) ;; add paredit-mode to all mode-hooks (add-hook (intern (concat (symbol-name mode) "-hook")) 'paredit-mode)) #+ENDSRC

*** Emacs Lisp

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

#+BEGIN_SRC emacs-lisp (add-hook 'emacs-lisp-mode-hook 'turn-on-eldoc-mode) (add-hook 'lisp-interaction-mode-hook 'turn-on-eldoc-mode) #+END_SRC

*** Common lisp

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. [[][Quicklisp]] is a library manager for Common Lisp,
and you can install Slime following the instructions from the site along
with this snippet.

#+BEGIN_SRC emacs-lisp (defun activate-slime-helper () (when (file-exists-p "/.quicklisp/slime-helper.el") (load (expand-file-name "/.quicklisp/slime-helper.el")) (define-key slime-repl-mode-map (kbd "C-l") 'slime-repl-clear-buffer)) (remove-hook 'common-lisp-mode-hook #'activate-slime-helper))

(add-hook 'common-lisp-mode-hook #'activate-slime-helper) #+END_SRC

We can specify what Common Lisp program Slime should use (I use SBCL).

#+BEGIN_SRC emacs-lisp (setq inferior-lisp-program "sbcl") #+END_SRC

More sensible =loop= indentation, borrowed from [[][simenheg]].

#+BEGIN_SRC emacs-lisp (setq lisp-loop-forms-indentation 6 lisp-simple-loop-indentation 2 lisp-loop-keyword-indentation 6) #+END_SRC

*** Clojure

Indent neatly when using pattern matching in Clojure.

#+begin_src emacs-lisp (with-eval-after-load 'clojure-mode (define-clojure-indent (match 1))) #+end_src

** Python

#+BEGINSRC emacs-lisp (setq python-shell-interpreter "python3") (add-hook 'python-mode-hook (lambda () (setq forward-sexp-function nil))) #+ENDSRC

** Java and 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=.

#+BEGIN_SRC emacs-lisp (defun c-setup () (local-set-key (kbd "C-c C-c") 'compile))

(add-hook 'c-mode-hook 'c-setup) #+END_SRC

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

#+BEGINSRC emacs-lisp (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))) #+ENDSRC

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

#+BEGINSRC emacs-lisp (add-hook 'java-mode-hook 'lsp) (add-hook 'java-mode-hook 'yas-minor-mode) #+ENDSRC

** 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=.

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

(add-hook 'asm-mode-hook 'asm-setup) #+END_SRC

** LaTeX and org-mode LaTeX export

=.tex=-files should be associated with =latex-mode= instead of =tex-mode=.

#+BEGINSRC emacs-lisp (add-to-list 'auto-mode-alist '("\.tex\'" . latex-mode)) #+ENDSRC

Use [[][latexmk]] for compilation by default.

#+BEGINSRC emacs-lisp (add-hook 'LaTeX-mode-hook (lambda () (add-hook 'hack-local-variables-hook (lambda () (setq-local compile-command (concat "latexmk -pdf -pvc " (if (eq TeX-master t) (file-name-base (buffer-name)) TeX-master)))) t t))) #+ENDSRC

Use ~biblatex~ for bibliography.

#+BEGINSRC emacs-lisp (setq-default bibtex-dialect 'biblatex) #+ENDSRC

I like using the [[][Minted]] package for source blocks in LaTeX. To make org use this we add the following snippet.

#+BEGINSRC emacs-lisp (eval-after-load 'org '(add-to-list 'org-latex-packages-alist '("" "minted"))) (setq org-latex-listings 'minted) #+ENDSRC

Because [[][Minted]] uses [][Pygments], we must add the =-shell-escape= option to the =org-latex-pdf-process= commands. The =tex-compile-commands= variable controls the default compile command for Tex- and LaTeX-mode, we can add the flag with a rather dirty statement (if anyone finds a nicer way to do this, please let me know).

#+BEGINSRC emacs-lisp (eval-after-load 'tex-mode '(setcar (cdr (cddaar tex-compile-commands)) " -shell-escape ")) #+ENDSRC

When exporting from Org to LaTeX, use ~latexmk~ for compilation.

#+BEGINSRC emacs-lisp (eval-after-load 'ox-latex '(setq org-latex-pdf-process '("latexmk -pdflatex='pdflatex -shell-escape -interaction nonstopmode' -pdf -f %f"))) #+ENDSRC

For my thesis, I need to use our university's LaTeX class, this snippet makes that class available.

#+BEGINSRC emacs-lisp (eval-after-load "ox-latex" '(progn (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}"))) (custom-set-variables '(org-export-allow-bind-keywords t)))) #+ENDSRC

Use Emacs for opening the PDF file, when invoking ~C-c C-e l o~.

#+BEGINSRC emacs-lisp (require 'org) (add-to-list 'org-file-apps '("\.pdf\'" . emacs)) #+ENDSRC

** 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=.

#+BEGINSRC emacs-lisp (add-hook 'haskell-mode-hook 'interactive-haskell-mode) (add-hook 'haskell-mode-hook 'turn-on-haskell-doc-mode) (add-hook 'haskell-mode-hook 'turn-on-haskell-indent) #+ENDSRC

Due to a bug in haskell-mode I have to keep this monstrosity in my config... #+BEGIN_SRC emacs-lisp (setq haskell-process-args-ghci '("-ferror-spans" "-fshow-loaded-modules"))

(setq haskell-process-args-cabal-repl '("--ghc-options=-ferror-spans -fshow-loaded-modules"))

(setq haskell-process-args-stack-ghci '("--ghci-options=-ferror-spans -fshow-loaded-modules" "--no-build" "--no-load"))

(setq haskell-process-args-cabal-new-repl '("--ghc-options=-ferror-spans -fshow-loaded-modules")) #+END_SRC

** Maude

Use =---= for comments in Maude.

#+BEGIN_SRC emacs-lisp (add-hook 'maude-mode-hook (lambda () (setq-local comment-start "---")))

(with-eval-after-load 'maude-mode (add-to-list 'maude-command-options "-no-wrap")) #+END_SRC

** Minizinc

#+BEGIN_SRC emacs-lisp (add-to-list 'auto-mode-alist '("\.mzn\'" . minizinc-mode))

(defun minizinc-setup () (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 "")))))

(add-hook 'minizinc-mode-hook 'minizinc-setup) #+END_SRC

** Coq

#+BEGINSRC emacs-lisp (add-hook 'coq-mode-hook #'company-coq-mode) #+ENDSRC

  • 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. I keep this at the end of the init-file to make sure that all functions are actually defined.

#+BEGINSRC emacs-lisp (defvar custom-bindings-map (make-keymap) "A keymap for custom bindings.") #+ENDSRC

** Bindings for [[][define-word]]

#+BEGINSRC emacs-lisp (define-key custom-bindings-map (kbd "C-c D") 'define-word-at-point) #+ENDSRC

** Bindings for [[][expand-region]]

#+BEGINSRC emacs-lisp (define-key custom-bindings-map (kbd "C->") 'er/expand-region) (define-key custom-bindings-map (kbd "C-<") 'er/contract-region) #+ENDSRC

** Bindings for [[][multiple-cursors]]

#+BEGINSRC emacs-lisp (define-key custom-bindings-map (kbd "C-c e") 'mc/edit-lines) (define-key custom-bindings-map (kbd "C-c a") 'mc/mark-all-like-this) (define-key custom-bindings-map (kbd "C-c n") 'mc/mark-next-like-this) #+ENDSRC

** Bindings for [[][Magit]]

#+BEGINSRC emacs-lisp (define-key custom-bindings-map (kbd "C-c m") 'magit-status) #+ENDSRC

** Bindings for [[][Counsel]]

#+beginsrc emacs-lisp (global-set-key (kbd "C-c i") 'swiper-isearch) (global-set-key (kbd "M-x") 'counsel-M-x) (global-set-key (kbd "C-x C-f") 'counsel-find-file) (global-set-key (kbd "M-y") 'counsel-yank-pop) (global-set-key (kbd "C-x b") 'ivy-switch-buffer) #+endsrc

** Bindings for [[][company-mode]]

#+BEGINSRC emacs-lisp (define-key company-active-map (kbd "C-d") 'company-show-doc-buffer) (define-key company-active-map (kbd "C-n") 'company-select-next) (define-key company-active-map (kbd "C-p") 'company-select-previous) (define-key company-active-map (kbd "") 'company-complete) #+ENDSRC

** Bindings for [[][Projectile]]

#+BEGINSRC emacs-lisp (define-key projectile-mode-map (kbd "C-c p") 'projectile-command-map) #+ENDSRC

** Bindings for [[][Cider]]

#+BEGINSRC emacs-lisp (with-eval-after-load 'cider (define-key cider-repl-mode-map (kbd "C-l") 'cider-repl-clear-buffer)) #+ENDSRC

** Bindings for [[][Olivetti]]

#+BEGINSRC emacs-lisp (define-key custom-bindings-map (kbd "C-c o") 'olivetti-mode) #+ENDSRC

** Bindings for built-ins

#+BEGINSRC emacs-lisp (define-key custom-bindings-map (kbd "M-u") 'upcase-dwim) (define-key custom-bindings-map (kbd "M-c") 'capitalize-dwim) (define-key custom-bindings-map (kbd "M-l") 'downcase-dwim) (define-key custom-bindings-map (kbd "M-]") 'other-frame) (define-key custom-bindings-map (kbd "C-j") 'newline-and-indent) (define-key custom-bindings-map (kbd "C-c s") 'ispell-word) (define-key comint-mode-map (kbd "C-l") 'clear-comint) #+ENDSRC

** Bindings for functions defined [[sec:defuns][above]].

#+BEGINSRC emacs-lisp (define-key global-map (kbd "M-p") 'jump-to-previous-like-this) (define-key global-map (kbd "M-n") 'jump-to-next-like-this) (define-key custom-bindings-map (kbd "M-,") 'jump-to-previous-like-this) (define-key custom-bindings-map (kbd "M-.") 'jump-to-next-like-this) (define-key custom-bindings-map (kbd "C-c .") (cycle-themes)) (define-key custom-bindings-map (kbd "C-x k") 'kill-this-buffer-unless-scratch) (define-key custom-bindings-map (kbd "C-c C-0") 'global-scale-default) (define-key custom-bindings-map (kbd "C-c C-=") 'global-scale-up) (define-key custom-bindings-map (kbd "C-c C-+") 'global-scale-up) (define-key custom-bindings-map (kbd "C-c C--") 'global-scale-down) (define-key custom-bindings-map (kbd "C-c j") 'cycle-spacing-delete-newlines) (define-key custom-bindings-map (kbd "C-c d") 'duplicate-thing) (define-key custom-bindings-map (kbd "") 'tidy) (define-key custom-bindings-map (kbd "C-z") 'toggle-vterm) (dolist (n (number-sequence 1 9)) (global-set-key (kbd (concat "M-" (int-to-string n))) (lambda () (interactive) (switch-vterm n)))) (define-key custom-bindings-map (kbd "C-c C-q") '(lambda () (interactive) (focus-mode 1) (focus-read-only-mode 1))) (with-eval-after-load 'org (define-key org-mode-map (kbd "C-'") 'org-sync-pdf)) #+ENDSRC

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

#+BEGINSRC emacs-lisp (define-minor-mode custom-bindings-mode "A mode that activates custom-bindings." t nil custom-bindings-map) #+ENDSRC * License

My Emacs configurations written in Org mode.

Copyright (c) 2013 - 2020 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

We use cookies. If you continue to browse the site, you agree to the use of cookies. For more information on our use of cookies please see our Privacy Policy.