Emacs Configuration Generation File
Introduction
Emacs uses an Init File to store all configurations. This file creates an Init File using literate programming principles to document and define the configuration.
To write the configuration file, execute C-c C-v C-t (org-babel-tangle). This command will read all source code sections in this file and concatenate them into the .emacs/init.el file.
If you like to try these settings without creating a new file, then type C-c C-v C-b to evaluate all code blocks. You can also evaluate each code block individually with C-c C-c or each line separately with C-x C-e. Emacs will ask you to confirm that you want to execute code. To stop Emacs asking for confirmation, run (setq org-confirm-babel-evaluate nil).
Credits
The idea to tangle an og mode file to configure Emacs is based on work by freetonik.
Basic Configuration
Redefine configuration file
Emacs includes a configuration system that will write settings to the init file. This code snippet redefines the name of this file to ensure the init.el file is not overwritten by the configuration system.
The custom configuration file is loaded first, and so any settings in the init file will append or override the custom settings.
;; Emacs init file
;; https://github.com/pprevos/EmacsLife
;; keep customize settings in their own file
(setq custom-file "~/.emacs.d/custom.el")
(when (file-exists-p custom-file)
(load custom-file))Packages
Setting the locations of package repositories.
(require 'package)
(add-to-list 'package-archives
'("melpa" . "https://melpa.org/packages/"))
(add-to-list 'package-archives
'("org" . "https://orgmode.org/elpa/") t)Visual elements
- The startup settings inhibit startup messages and screen.
- Loads the theme
- Hides the toolbar (who needs icons anyway?)
- Hides the scroll bar (using a mouse too often gives you RSI)
- Subtly highlight the current line to find your cursor
- Silence the Alarm Bell.https://www.emacswiki.org/emacs/AlarmBell
;; This init file is generated with an Org-Mode file.
;; Any manual changes will be overwritten by the script.
;; -----------------------------------------------------
;; Startup settings
(setq
inhibit-startup-message t
inhibit-startup-screen t
)
;; Visual enhancements
;; Load theme
(load-theme 'tsdh-dark)
;; Hide toolbar and scroll bars
(tool-bar-mode nil)
(scroll-bar-mode -1)
;; Highlight current line
(global-hl-line-mode 1)
;; Silence the alarm
(setq ring-bell-function 'ignore)Emacs mechanics
This section contains basic Emacs mechanics.
- All backup files saved in the same folder. The
backup-directory-alistfunction lets you modify that behaviour. I prefer to store all backup files in one folder to avoid cluttering document folders. - Save desktop on exit
- Emacs uses both Yes/No and Y/N to ask for confirmation. I find this added security unnecessary and change it to single letter responses.
- Enable horizontal mouse scroll
- IDo Mode (Interactively DO things) makes life a bit easier when switching buffers.
;; Emacs mechanics
;; Save all backup files in one folder.
(setq backup-directory-alist '(("." . "~/.emacs.d/backups")))
;; Save desktop
(desktop-save-mode 1)
;; Yes or No
(defalias 'yes-or-no-p 'y-or-n-p)
;; Auto revert file when changed outside of Emacs
(global-auto-revert-mode 1)
;; Mouse scrolling
(global-set-key (kbd "<mouse-6>") (lambda () (interactive) (scroll-right 6)))
(global-set-key (kbd "<mouse-7>") (lambda () (interactive) (scroll-left 6)))
;; IDo mode
(require 'ido)
(setq ido-enable-flex-matching t)
(setq ido-everywhere t)
(ido-mode 1)
(setq ido-file-extensions-order '(".org" ".fountain" ".R" ".el"))Editing
This snippet changes the editing settings to my liking.
- CUA mode (Common User Acess) to enable
C-cfor copy,C-vfor paste, and so on. - Visual Line Mode wraps long lines near the window edge.
- Fountain mode for writing scripts.
- FlySpell for spell-checking on the fly. The F7 key is mapped to suggesting alternatives for misspelled words.
;; Editing configuration
;; Common User Access
(cua-mode t)
;; Line wrapping
(global-visual-line-mode t)
;;Enable Fountain mode
(require 'fountain-mode)
;; Spell checking for Org and Fountain modes
(add-hook 'org-mode-hook 'turn-on-flyspell 'append)
(add-hook 'fountain-mode-hook 'turn-on-flyspell)
(global-set-key (kbd "<f7>") 'ispell-word)
;;Dired
(put 'dired-find-alternate-file 'disabled nil)
;;Auto complete
(require 'auto-complete)
(global-auto-complete-mode t)Calendar settings
- First day of the week is Monday
- Dutch holidays (TODO)
;; First day of te week
(setq calendar-week-start-day 1)Org-Mode
Basic configuration
;; Keyboard shortcuts
(global-set-key "\C-cl" 'org-store-link)
(global-set-key "\C-ca" 'org-agenda)
(global-set-key "\C-cc" 'org-capture)
(global-set-key "\C-cb" 'org-iswitchb)
;; Workflow states
(setq org-todo-keywords
'((sequence "TODO(t)" "WAITING(w)" "PROJECT(p)" "SOMEDAY(s)" "|" "DONE(d)" "CANCELLED(c)")))Agenda settings
(setq org-agenda-skip-deadline-if-done t
org-agenda-skip-scheduled-if-done t)Make life easier
- helm-org-rifle searches through your open Org files.
- org-web-tools Commands and functions for retrieving web page content and processing it into and displaying it as Org-mode content.
C-x p lconverts a link in the clipboard to an Org Mode linkC-x p icopies the content of the page in the clipboard to an Org Mode entry.
;; helm org rifle
(require 'helm-org-rifle)
(global-set-key (kbd "C-x C-r") 'helm-org-rifle)
;; org web tools
(require 'org-web-tools)
(global-set-key (kbd "C-x p l") 'org-web-tools-insert-link-for-url)
(global-set-key (kbd "C-x p i") 'org-web-tools-insert-web-page-as-entry)Visual elements
- Clean view
- Set image preview with to 600 pixels
;; Clean outline view
(setq org-hide-emphasis-markers t
org-hide-leading-stars t
org-startup-indented t)
;; Image size
(setq org-image-actual-width 600);; Insert NOTES drawer
;; by u/alecigne
(defun ddz-org-insert-drawer-note ()
(interactive)
(org-insert-drawer nil "NOTES"))
(with-eval-after-load 'org
(define-key org-mode-map (kbd "C-c C-x n") 'ddz-org-insert-drawer-note))Org-Capture
Org-Mode helps you quickly capture ideas that are not related to your current workflow with Org Capture. Add your idea and keep working without switching applications or files. Org Capture is great for journal entries, adding tasks to your inbox, create a shopping list and whatever else you like to collect as random thoughts. Mike Zamansky has written excellent instructions on using Org Capture.
(setq org-capture-templates
'(("a" "Appointment" entry (file "~/Dropbox/orgfiles/gcal.org" "Appointments")
"* TODO %?\n:PROPERTIES:\n\n:END:\nDEADLINE: %^T \n %i\n")
("n" "Note" entry (file+headline "~/Dropbox/orgfiles/notes.org" "Notes")
"* Note %?\n%T")
("l" "Link" entry (file+headline "~/Dropbox/orgfiles/links.org" "Links")
"* %? %^L %^g \n%T" :prepend t)
("b" "Blog idea" entry (file+headline "~/Dropbox/orgfiles/i.org" "Blog Topics:")
"* %?\n%T" :prepend t)
("t" "To Do Item" entry (file+headline "~/Dropbox/orgfiles/i.org" "To Do Items")
"* %?\n%T" :prepend t)
("j" "Journal" entry (file+datetree "~/Dropbox/journal.org")
"* %?\nEntered on %U\n %i\n %a")
("s" "Screencast" entry (file "~/Dropbox/orgfiles/screencastnotes.org")
"* %?\n%i\n")))\LaTeX export
These settings define how Org Mode exports files to \LaTeX. The export classes define the various document types.
;; Smart quotes
(require 'ox-latex)
(setq org-export-with-smart-quotes t)
;; American Psychological Association papers
(add-to-list 'org-latex-classes '("apa6"
"\\documentclass[a4paper, jou, 11pt]{apa6}
\\usepackage[british]{babel}
\\usepackage{inputenc}
\\usepackage{amsmath}
\\usepackage{graphicx}
\\usepackage{csquotes}
\\usepackage[hyphens]{url}
\\usepackage[T1]{fontenc}
\\usepackage{lmodern}
\\usepackage{hyperref}"
("\\section{%s}" . "\\section*{%s}")
("\\subsection{%s}" . "\\subsection*{%s}")
))
;; ebooks using memoir
(add-to-list 'org-latex-classes '("ebook"
"\\documentclass[11pt, oneside]{memoir}
\\setstocksize{9in}{6in}
\\settrimmedsize{\\stockheight}{\\stockwidth}{*}
\\setlrmarginsandblock{2cm}{2cm}{*} % Left and right margin
\\setulmarginsandblock{2cm}{2cm}{*} % Upper and lower margin
\\checkandfixthelayout
\\usepackage{times}
\\usepackage[british]{babel}
\\usepackage[raggedright]{sidecap}
\\setsecheadstyle{\\normalfont \\raggedright \\textbf}
\\setsubsecheadstyle{\\normalfont \\raggedright \\emph}
\\usepackage[labelformat=empty, font=small]{caption}
\\usepackage{pdfpages}
\\usepackage[unicode=true,
bookmarks=true,bookmarksnumbered=false,bookmarksopen=true,bookmarksopenlevel=1,
breaklinks=true,pdfborder={0 0 0},backref=false,colorlinks=false,pdfborderstyle={/S/U/W .5}, allbordercolors={.8 .8 .8}]
{hyperref}
\\pagestyle{myheadings}
\\setcounter{tocdepth}{0}
\\usepackage{ccicons}
\\OnehalfSpacing
\\usepackage[authoryear]{natbib}
"
("\\chapter{%s}" . "\\chapter*{%s}")
("\\section{%s}" . "\\section*{%s}")
("\\subsection{%s}" . "\\subsection*{%s}")
))
;;Two-coumn instruction sheets
(add-to-list 'org-latex-classes '("magictrick"
"\\documentclass[11pt, a4paper, twocolumn, twoside]{article}
\\usepackage{ccicons}
\\usepackage{pdfpages}
\\usepackage{times}
\\usepackage{helvet}
\\usepackage{geometry}
\\geometry{a4paper, total={170mm,250mm}, left=20mm, top=30mm}
% header 2008 x 332 px
\\usepackage{titlesec}
\\titleformat{\\section}
{\\bfseries}{\\thesection}{1em}{}
\\titleformat{\\subsection}
{\\itshape}{\\thesection}{1em}{}
\\usepackage{fancyhdr}
\\usepackage[hidelinks]{hyperref}
\\pagestyle{fancy}
\\renewcommand{\\headrulewidth}{0pt}
\\renewcommand{\\footrulewidth}{0pt}
\\setlength\\headheight{100.0pt}
\\addtolength{\\textheight}{-100.0pt}
\\fancyhead[LO]{\\Large{\\textsf{Magic Perspectives Presents}} \\includegraphics[width=\\textwidth]{header}}
\\fancyhead[LE]{\\includegraphics[width=0.5\\textwidth]{header}}
\\lfoot{Peter Prevos}
\\rfoot{\\href{https://magicperspectives.net}{magicperspectives.net}}
"
("\\section{%s}" . "\\section*{%s}")
("\\subsection{%s}" . "\\subsection*{%s}")
)) Babel
;; Trust all code embedded in Org files
(setq org-confirm-babel-evaluate nil)
;; Fontify source code in source snippets
(setq org-src-fontify-natively t)
;; Enable R coding
(org-babel-do-load-languages
'org-babel-load-languages
'((R . t)))Data Science
Emacs Speaks Statistics (ESS) supports editing of scripts and interaction with various statistical analysis programs such as R. You also need to install the R software. Run an E terminal with M-x R and enter the preferred working directory.
In ESS, the underscore key is mapped to the <- assignment operator in R. If you need an underscore, you need to type it twice. This functionality can be annoying when you are an avid user of ggplot. The ess-smart-underscore package solves this issue by allowing a single underscore in certain circumstances.
;; Emacs Speaks Statistics
(require 'ess-site)
(require 'ess-smart-underscore)
;; Use Alt Return to execute one line of code
(define-key ess-mode-map (kbd "M-RET") 'ess-eval-region-or-line-and-step)
;; Or Babel
(org-babel-do-load-languages
'org-babel-load-languages '((R . t)))Magit
Magit implements Git in Emacs and is almost like magic. This line of code creates the C-x g shortcut to open the Magit status screen.
;; Magit
(global-set-key (kbd "C-x g") 'magit-status)