This file is my .emacs configuration file (more precisely my
~/.emacs.d/init.el configuration). I stole it from Luka Stanisic who
stole parts from mine a long time ago and cleaned (with org-mode) and
improved everything. It is so convenient that I think it is time to
share it. Note that it comprises my personal website publishing
configuration that you should not really need but that you can inspire
on.
To have a nice emacs configuration, you’ll need:
- emacs24 (>24.3) (this way installing through elpa will save you some time) and emacs-goodies (e.g., for color themes)
- org-mode > 8.2 (there has been a major compatibility breakage so need to stay away from old versions)
- ess and R
- texlive and auctex
If you’re a Mac OS X user, read this.
Then copy it into ~/.emacs.d/init.org.
- If you are using Mac OS X, remove every line containing the word =xclip= in this file (you may also be interested in reading this blog entry).
- Otherwise, you should consider installing xclip (
sudo apt-get install xclipin a shell andM-x package-installthexclippackage within emacs) as it will ease interactions with emacs when running in a terminal.
Then do M-x org-babel-load-file (M-x means pressing Alt and x
simultaneously to get the emacs prompt that allows you to call emacs
functions, also remember you can press Tab to activate auto
completion) and provide ~/.emacs.d/init.org. This should evaluate
every piece of emacs-lisp code available here.
Next time, simply reload changes with “C-c i”. If some stuff does not work (in particular in the org-mode website generation section or in the tabs section), just comment them and proceeed.
(require 'package)
(package-initialize)
(setq package-archives
'(("ELPA" . "http://tromey.com/elpa/")
("gnu" . "http://elpa.gnu.org/packages/")
("melpa" . "http://melpa.milkbox.net/packages/")
("marmalade" . "http://marmalade-repo.org/packages/")))(add-to-list 'load-path "~/lib/elisp/")
(add-to-list 'load-path "~/.emacs.d/elpa/org-20140707/")
(add-to-list 'load-path "~/.emacs.d/elpa/xclip-1.3/")
(add-to-list 'load-path "~/.emacs.d/elpa/htmlize-20130207.1202/")
;;(require 'org-git-link) ;; Made some personal modifications[10:42:15; 24.06.2013]
(add-to-list 'load-path "~/misc/org-mode/lisp/")
(require 'org-install)
(require 'org)[17:28:05; 30.05.2013]
Useful if you want to export org files in batch mode…
(require 'font-lock)
(require 'cc-mode)
(c-after-font-lock-init)(setq auto-mode-alist
(append (mapcar 'purecopy
'(("\\.c$" . c-mode)
("\\.h$" . c-mode)
("\\.c.simp$" . c-mode)
("\\.h.simp$" . c-mode)
("\\.a$" . c-mode)
("\\.w$" . cweb-mode)
("\\.cc$" . c++-mode)
("\\.S$" . asm-mode)
("\\.s$" . asm-mode)
("\\.p$" . pascal-mode)
("\\.pas$" . pascal-mode)
("\\.tex$" . LaTeX-mode)
("\\.txi$" . Texinfo-mode)
("\\.el$" . emacs-lisp-mode)
("emacs" . emacs-lisp-mode)
("\\.ml[iylp]?" . tuareg-mode)
("[mM]akefile" . makefile-mode)
("[mM]akefile.*" . makefile-mode)
("\\.mak" . makefile-mode)
("\\.cshrc" . sh-mode)
("\\.html$" . html-mode)
("\\.org$" . org-mode)
)) auto-mode-alist))(with-eval-after-load 'ox (require 'ox-hugo))
(require 'ox)
(require 'ox-hugo)(setq inhibit-splash-screen t)[16:29:14; 24.04.2013]
(setq frame-title-format
'("Emacs - " (buffer-file-name "%f"
(dired-directory dired-directory "%b"))))[16:29:40; 24.04.2013]
(set-default-font "9x15")(global-font-lock-mode t)(line-number-mode 1)
(column-number-mode 1)(load-library "paren")
(show-paren-mode 1)
(transient-mark-mode t)
(require 'paren)(global-set-key (kbd "C-c i")
(lambda() (interactive)(org-babel-load-file "~/.emacs.d/init.org")))[16:28:10; 24.04.2013]
(add-hook 'c-mode-common-hook
(lambda()
(local-set-key (kbd "C-c <right>") 'hs-show-block)
(local-set-key (kbd "C-c <left>") 'hs-hide-block)
(local-set-key (kbd "C-c <up>") 'hs-hide-all)
(local-set-key (kbd "C-c <down>") 'hs-show-all)
(hs-minor-mode t)))[16:32:57; 24.04.2013]
(global-set-key [f10] '(lambda () (interactive) (revert-buffer nil t nil)))[16:34:57; 24.04.2013]
(global-set-key "\^x\^e" 'compile)(defun jump-mark ()
(interactive)
(set-mark-command (point)))
(defun beginning-of-defun-and-mark ()
(interactive)
(push-mark (point))
(beginning-of-defun))
(defun end-of-defun-and-mark ()
(interactive)
(push-mark (point))
(end-of-defun))
(global-set-key "\^c\^b" 'beginning-of-defun-and-mark)
(global-set-key "\^c\^e" 'end-of-defun-and-mark)
(global-set-key "\^c\^j" 'jump-mark)
(global-set-key [S-f6] 'jump-mark) ;; jump from mark to mark(global-set-key "\M-g" 'goto-line)Since I switched to emacs 24, cutting and pasting to emacs has
become burdensome. For example, I had to do S-C-c after selecting
terminals areas before being able to copy in emacs. With the
following commants, I don’t have to do this anymore. I wish it
would have eased interactions when running emacs in a terminal
(i.e., with -nw) but it does not. :( http://stackoverflow.com/questions/13036155/how-to-to-combine-emacs-primary-clipboard-copy-and-paste-behavior-on-ms-windows
(setq select-active-regions nil)
(setq x-select-enable-primary t)
(setq x-select-enable-clipboard t)
(setq mouse-drag-copy-region t)Despite what can be found in many old discussions on emacs mailing lists, it seems to be possible and the trick is to use xclip! http://stackoverflow.com/questions/4580835/emacs-copy-kill-ring-to-system-clipboard-in-nowindow-mode http://stackoverflow.com/questions/5288213/how-can-i-paste-the-selected-region-outside-of-emacs/14659015#14659015
If you simply use the following commands, then emacs cut-n-paste works just perfectly with the X clipboard. I can select in emacs no window and then paste with the middle-click button! :) Many thanks to Vijay for pointing me out that I had forgotten the “require” line… :)
(require 'xclip)
(xclip-mode 1)(global-set-key (kbd "C-+") 'text-scale-increase)
(global-set-key (kbd "C--") 'text-scale-decrease)
;; C-x C-0 restores the default font size[11:26:47; 18.04.2014]
;; Inspired from http://tex.stackexchange.com/questions/166681/changing-language-of-flyspell-emacs-with-a-shortcut
;; (defun spell (choice)
;; "Switch between language dictionaries."
;; (interactive "cChoose: (a) American | (f) Francais")
;; (cond ((eq choice ?1)
;; (setq flyspell-default-dictionary "american")
;; (setq ispell-dictionary "american")
;; (ispell-kill-ispell))
;; ((eq choice ?2)
;; (setq flyspell-default-dictionary "francais")
;; (setq ispell-dictionary "francais")
;; (ispell-kill-ispell))
;; (t (message "No changes have been made."))) )
(define-key global-map (kbd "C-c s a") (lambda () (interactive) (ispell-change-dictionary "american")))
(define-key global-map (kbd "C-c s f") (lambda () (interactive) (ispell-change-dictionary "francais")))
(define-key global-map (kbd "C-c s r") 'flyspell-region)
(define-key global-map (kbd "C-c s b") 'flyspell-buffer)
(define-key global-map (kbd "C-c s s") 'flyspell-mode)(defun auto-fill-mode-on () (TeX-PDF-mode 1))
(add-hook 'tex-mode-hook 'TeX-PDF-mode-on)
(add-hook 'latex-mode-hook 'TeX-PDF-mode-on)
(setq TeX-PDF-mode t)(defun auto-fill-mode-on () (auto-fill-mode 1))
(add-hook 'text-mode-hook 'auto-fill-mode-on)
(add-hook 'emacs-lisp-mode 'auto-fill-mode-on)
(add-hook 'tex-mode-hook 'auto-fill-mode-on)
(add-hook 'latex-mode-hook 'auto-fill-mode-on)From http://www.emacswiki.org/emacs/IndentingC.
(setq c-default-style "k&r")
(setq c-basic-offset 2)(defun c-reformat-buffer()
(interactive)
(save-buffer)
(setq sh-indent-command (concat
"indent -i2 -kr --no-tabs"
buffer-file-name
)
)
(mark-whole-buffer)
(universal-argument)
(shell-command-on-region
(point-min)
(point-max)
sh-indent-command
(buffer-name)
)
(save-buffer)
)
(define-key c-mode-base-map [f7] 'c-reformat-buffer)Link: http://org.ryuslash.org/dotfiles/emacs/init.html#sec-7-1 Being asked to type in yes explicitly all the time gets very tedious. I understand that it is safer since y is much easier to type in accidentally than yes and so the potential to say yes to things you don’t want is there, but I haven’t had any such problems yet.
(defalias 'yes-or-no-p 'y-or-n-p)[16:27:20; 24.04.2013]
(setq org-directory "~/org/")(setq org-hide-leading-stars t)
(setq org-alphabetical-lists t)
(setq org-src-fontify-natively t) ;; you want this to activate coloring in blocks
(setq org-src-tab-acts-natively t) ;; you want this to have completion in blocks
(setq org-hide-emphasis-markers t) ;; to hide the *,=, or / markers
(setq org-pretty-entities t) ;; to have \alpha, \to and others display as utf8 http://orgmode.org/manual/Special-symbols.html(global-set-key "\C-cl" 'org-store-link)
(global-set-key "\C-cc" 'org-capture)
(global-set-key (kbd "C-c a") 'org-agenda)
(define-key global-map "\C-cl" 'org-store-link)
(define-key global-map (kbd "C-c a") 'org-agenda)
(global-set-key "\C-cb" 'org-iswitchb)
(setq org-default-notes-file "~/org/notes.org")
(define-key global-map "\C-cd" 'org-capture)
(setq org-capture-templates (quote (("t" "Todo" entry (file+headline "~/org/liste.org" "Tasks") "* TODO %?
%i
%a" :prepend t) ("j" "Journal" entry (file+datetree "~/org/journal.org") "* %?
Entered on %U
%i
%a"))))(setq org-agenda-include-all-todo t)
(setq org-agenda-include-diary t)(setq org-agenda-files (quote ("~/org/liste.org" "~/org/google.org")))
(setq revert-without-query (quote ("google.org")))(setq org-id-method (quote uuidgen));; (global-visual-line-mode t)[16:32:35; 24.04.2013]
Mark heading done when all checkboxes are checked.
An item consists of a list with checkboxes. When all of the checkboxes are checked, the item should be considered complete and its TODO state should be automatically changed to DONE. The code below does that. This version is slightly enhanced over the one in the mailing list (see http://thread.gmane.org/gmane.emacs.orgmode/42715/focus=42721) to reset the state back to TODO if a checkbox is unchecked.
Note that the code requires that a checkbox statistics cookie (the [/] or [%] thingie in the headline - see the Checkboxes section in the manual) be present in order for it to work. Note also that it is too dumb to figure out whether the item has a TODO state in the first place: if there is a statistics cookie, a TODO/DONE state will be added willy-nilly any time that the statistics cookie is changed.
;; see http://thread.gmane.org/gmane.emacs.orgmode/42715
(eval-after-load 'org-list
'(add-hook 'org-checkbox-statistics-hook (function ndk/checkbox-list-complete)))
(defun ndk/checkbox-list-complete ()
(save-excursion
(org-back-to-heading t)
(let ((beg (point)) end)
(end-of-line)
(setq end (point))
(goto-char beg)
(if (re-search-forward "\\[\\([0-9]*%\\)\\]\\|\\[\\([0-9]*\\)/\\([0-9]*\\)\\]" end t)
(if (match-end 1)
(if (equal (match-string 1) "100%")
;; all done - do the state change
(org-todo 'done)
(org-todo 'todo))
(if (and (> (match-end 2) (match-beginning 2))
(equal (match-string 2) (match-string 3)))
(org-todo 'done)
(org-todo 'todo)))))))[16:27:59; 24.04.2013]
(global-set-key (kbd "C-c d") 'insert-date)
(defun insert-date (prefix)
"Insert the current date. With prefix-argument, use ISO format. With
two prefix arguments, write out the day and month name."
(interactive "P")
(let ((format (cond
((not prefix) "** %Y-%m-%d")
((equal prefix '(4)) "[%Y-%m-%d]"))))
(insert (format-time-string format))))[16:34:01; 24.04.2013]
(global-set-key (kbd "C-c t") 'insert-time-date)
(defun insert-time-date (prefix)
"Insert the current date. With prefix-argument, use ISO format. With
two prefix arguments, write out the day and month name."
(interactive "P")
(let ((format (cond
((not prefix) "[%H:%M:%S; %d.%m.%Y]")
((equal prefix '(4)) "[%H:%M:%S; %Y-%m-%d]"))))
(insert (format-time-string format))))[16:34:15; 24.04.2013]
(global-set-key (kbd "C-c v") 'org-show-todo-tree)[16:34:40; 24.04.2013]
(global-set-key (kbd "C-c l") 'org-store-link)[16:35:48; 24.04.2013]
(global-set-key (kbd "C-c g") 'org-git-insert-link-interactively)[16:36:31; 24.04.2013]
(global-set-key (kbd "C-c e") (lambda ()
(interactive)
(insert "** data#\n*** git:\n#+begin_src sh\ngit log -1\n#+end_src\n*** Notes:" )))
;;(insert "** data#\n[[shell:git log -1][git]]\n" )))
;;(insert "** data#\n[[shell:git log -1][git]]\n" (format-time-string "[%H:%M:%S; %d.%m.%Y]" ))))[16:36:15; 24.04.2013]
Additional shortcuts for navigating through org-mode documents:
(global-set-key (kbd "C-c <up>") 'outline-up-heading)
(global-set-key (kbd "C-c <left>") 'outline-previous-visible-heading)
(global-set-key (kbd "C-c <right>") 'outline-next-visible-heading)[18:01:07; 27.05.2013]
Convenient built-in shortcuts.
C-x n s (org-narrow-to-subtree)
Narrow buffer to current subtree.
C-x n b (org-narrow-to-block)
Narrow buffer to current block.
C-x n w (widen)
Widen buffer to remove narrowing.
(setq org-export-babel-evaluate nil)
(setq org-confirm-babel-evaluate nil)
(custom-set-faces)(org-babel-do-load-languages
'org-babel-load-languages
'(
(shell . t)
(python . t)
(R . t)
(C . t)
(ruby . t)
(ditaa . t)
(dot . t)
(octave . t)
(sqlite . t)
(perl . t)
(screen . t)
(plantuml . t)
(lilypond . t)
))To use this type <S and then TAB
(add-to-list 'org-structure-template-alist
'("S" "#+begin_src ?\n\n#+end_src" "<src lang=\"?\">\n\n</src>"))To use this type <m and then TAB
(add-to-list 'org-structure-template-alist
'("m" "#+begin_src emacs-lisp\n\n#+end_src" "<src lang=\"emacs-lisp\">\n\n</src>"))To use this type <r and then TAB
(add-to-list 'org-structure-template-alist
'("r" "#+begin_src R :results output :session :exports both\n\n#+end_src" "<src lang=\"R\">\n\n</src>"))To use this type <R and then TAB
(add-to-list 'org-structure-template-alist
'("R" "#+begin_src R :results output graphics :file (org-babel-temp-file \"figure\" \".png\") :exports both :width 600 :height 400 :session\n\n#+end_src" "<src lang=\"R\">\n\n</src>"))To use this type <p and then TAB
(add-to-list 'org-structure-template-alist
'("p" "#+begin_src python :results output raw :exports both\n\n#+end_src" "<src lang=\"python\">\n\n</src>"))To use this type <b and then TAB
(add-to-list 'org-structure-template-alist
'("b" "#+begin_src shell :results output\n\n#+end_src" "<src lang=\"sh\">\n\n</src>"))To use this type <B and then TAB. This comes with a session argument (e.g., in case you want to keep ssh connexions open).
(add-to-list 'org-structure-template-alist
'("B" "#+begin_src shell :session foo :results output :exports both \n\n#+end_src" "<src lang=\"sh\">\n\n</src>"))[18:23:44; 21.06.2013]
(global-set-key (kbd "C-c S-t") 'org-babel-execute-subtree)[15:25:16; 17.12.2013]
(add-hook 'org-babel-after-execute-hook 'org-display-inline-images)
(add-hook 'org-mode-hook 'org-display-inline-images)
(add-hook 'org-mode-hook 'org-babel-result-hide-all)(unless (boundp 'org-latex-classes) (setq org-latex-classes nil))
(add-to-list 'org-latex-classes '("acm-proc-article-sp" "\\documentclass{acm_proc_article-sp}\n \[NO-DEFAULT-PACKAGES]\n \[EXTRA]\n \\usepackage{graphicx}\n \\usepackage{hyperref}" ("\\section{%s}" . "\\section*{%s}") ("\\subsection{%s}" . "\\subsection*{%s}") ("\\subsubsection{%s}" . "\\subsubsection*{%s}") ("\\paragraph{%s}" . "\\paragraph*{%s}") ("\\subparagraph{%s}" . "\\subparagraph*{%s}")))
(setq org-latex-to-pdf-process '("pdflatex -interaction nonstopmode -output-directory %o %f ; bibtex `basename %f | sed 's/\.tex//'` ; pdflatex -interaction nonstopmode -output-directory %o %f ; pdflatex -interaction nonstopmode -output-directory %o %f"))I’m not completely sure I need this. I could probably remove it…
(add-to-list 'org-latex-classes '("article" "\\documentclass{article}\n \[NO-DEFAULT-PACKAGES]\n \[EXTRA]\n \\usepackage{graphicx}\n \\usepackage{hyperref}" ("\\section{%s}" . "\\section*{%s}") ("\\subsection{%s}" . "\\subsection*{%s}") ("\\subsubsection{%s}" . "\\subsubsection*{%s}") ("\\paragraph{%s}" . "\\paragraph*{%s}") ("\\subparagraph{%s}" . "\\subparagraph*{%s}")))[15:18:27; 16.05.2013]
This is installed by default for most of emacs Remote File Editing Using Emacs
Emacs has a package called TRAMP (Transparent Remote (file) Access, Multiple Protocol) which allows you to edit files on remote machines via SSH. Since Emacs 22, TRAMP is included with the distribution.
All you need to do is add the following lines to your .emacs file,
(require 'tramp)
(setq tramp-default-method "scp")Then in order to open a file on a remote machine, you can use,
C-x C-f /user@your.host.com:/path/to/file
If you don’t want to enter your password every time you open or save a file consider using Public Key Authentication.
TRAMP mode can also be used to edit files on the same machine as another user, if you want to open some file as root you can use,
C-x C-f /root@127.0.0.1:/path/to/file [12:25:12; 13.06.2013]
;; ;;; Nicolas Goaziou, http://article.gmane.org/gmane.emacs.orgmode/67692
;; (defun org-latex-ignore-heading-filter-headline (headline backend info)
;; "Strip headline from HEADLINE. Ignore BACKEND and INFO."
;; (when (and (org-export-derived-backend-p backend 'latex)
;; (string-match "\\`.*ignoreheading.*\n" headline))
;; (replace-match "" nil nil headline)))
;; (add-to-list 'org-export-filter-headline-functions
;; 'org-latex-ignore-heading-filter-headline)(setq org-src-preserve-indentation t);; PDFs visited in Org-mode are opened in Evince (and not in the default choice) https://stackoverflow.com/a/8836108/789593
(add-hook 'org-mode-hook
'(lambda ()
(delete '("\\.pdf\\'" . default) org-file-apps)
(add-to-list 'org-file-apps '("\\.pdf\\'" . "evince %s"))))(setq visible-bell t)(set-frame-font "-PfEd-DejaVu Sans Mono-normal-normal-normal-*-22-*-*-*-m-0-iso10646-1")