emacs
1 load extra extensions
Register custom Emacs extensions directory. I downloaded there various emacs extensions from the internet.
(add-to-list 'load-path "~/.emacs.d/extensions/")
Load some useful extensions from that directory.
(require 'bind-key) ;; utility to simplify key binding (require 'ox-twbs) ;; utility to export Twitter Bootstrap themed HTML (require 'ox-odt) ;; add ODT export option to the export menu
2 file handling
2.1 convert tabs to spaces
More modes: http://www.emacswiki.org/CategoryModes
(defun untab-all () (untabify (point-min) (point-max)) nil) (defun add-write-contents-hooks-hook () (add-hook 'write-contents-hooks 'untab-all nil t )) (add-hook 'emacs-lisp-mode-hook #'add-write-contents-hooks-hook) (add-hook 'c-mode-common-hook #'add-write-contents-hooks-hook) (add-hook 'sh-mode-hook #'add-write-contents-hooks-hook) (add-hook 'text-mode-hook #'add-write-contents-hooks-hook) (add-hook 'sql-mode-hook #'add-write-contents-hooks-hook) (add-hook 'css-mode-hook #'add-write-contents-hooks-hook) ;; matches *.properties files (add-hook 'conf-javaprop-mode-hook #'add-write-contents-hooks-hook)
2.3 disable saving of backups
Disable those backup files with ~ suffix, because I keep important files under version control anyway.
(setq make-backup-files nil)
2.4 automatically reload files that were changed on disk by another process
(global-auto-revert-mode 1)
2.5 automatically save edited files
This prevents accidently leaving edited files open in the background with important unsaved changes, especially when using emacsclient with emacs daemon.
Save edited files when Emacs frame looses focus.
(defun save-all () (interactive) (save-some-buffers t)) (add-hook 'focus-out-hook 'save-all)
Save edited files when Emacs frame is closed.
(defun save-all2 (frame) (interactive) (save-some-buffers t)) (add-hook 'delete-frame-functions 'save-all2)
3 visual tweaks
3.1 better theme
Register custom Emacs themes directory.
(add-to-list 'custom-theme-load-path "~/.emacs.d/themes")
Load nice looking color theme.
(load-theme 'spolsky t)
4 basic text editing customizations
4.1 keyboard shortcuts to ident/dedent selected region
- Idents/dedents by 4 spaces.
- Select text and use keys: <, >
(defun my-indent-region (N) (interactive "p") (if (use-region-p) (progn (indent-rigidly (region-beginning) (region-end) (* N 4)) (setq deactivate-mark nil)) (self-insert-command N))) (defun my-unindent-region (N) (interactive "p") (if (use-region-p) (progn (indent-rigidly (region-beginning) (region-end) (* N -4)) (setq deactivate-mark nil)) (self-insert-command N))) (global-set-key ">" 'my-indent-region) (global-set-key "<" 'my-unindent-region)
4.2 mouse middle click should paste at text cursor location
Otherwise, by default it inserts text at mouse click location.
(setq mouse-yank-at-point t)
4.3 enable upcase-region / downcase-region commands
For mysterious reasons these commands are disabled by default because they are unfriendly for beginners. Here we enable them anyway.
Commands convert selected region into upper or lower case.
(put 'upcase-region 'disabled nil) (put 'downcase-region 'disabled nil)
4.4 text alignment addons
- From http://stackoverflow.com/questions/3633120/emacs-hotkey-to-align-equal-signs
- Another information: https://gist.github.com/700416
- Use rx function http://www.emacswiki.org/emacs/rx
(defun align-to-colon (begin end) "Align region to colon (:) signs" (interactive "r") (align-regexp begin end (rx (group (zero-or-more (syntax whitespace))) ":") 1 1 )) (defun align-to-comma (begin end) "Align region to comma signs" (interactive "r") (align-regexp begin end (rx "," (group (zero-or-more (syntax whitespace))) ) 1 1 )) (defun align-to-equals (begin end) "Align region to equal signs" (interactive "r") (align-regexp begin end (rx (group (zero-or-more (syntax whitespace))) "=") 1 1 )) (defun align-to-hash (begin end) "Align region to hash ( => ) signs" (interactive "r") (align-regexp begin end (rx (group (zero-or-more (syntax whitespace))) "=>") 1 1 )) ;; work with this (defun align-to-comma-before (begin end) "Align region to equal signs" (interactive "r") (align-regexp begin end (rx (group (zero-or-more (syntax whitespace))) ",") 1 1 ))
5 org-mode customizations
5.1 keyboard shortcut F5: full text search across all agenda files
(global-set-key (kbd "<f5>") 'org-search-view)
5.2 keyboard shortcut F6: publish ORG document as HTML file
Uses TWBS-HTML plugin: https://github.com/marsmining/ox-twbs
(global-set-key (kbd "<f6>") 'org-twbs-export-to-html)
5.3 keyboard shortcut F7: produce program source code from ORG document (tangle)
(global-set-key (kbd "<f7>") 'org-babel-tangle)
5.6 recursively load org/agenda documents into RAM
In order to
- use instant full text search over org mode documents
- be able to display aggredated day/week agendas and TODO lists
- navigate between org documents using UUID links
- …
emacs needs to parse all org mode files after startup and before these features are used.
Recursively find .org files in provided directory.
(defun sa-find-org-file-recursively (directory &optional filext) "Return .org and .org_archive files recursively from DIRECTORY. If FILEXT is provided, return files with extension FILEXT instead." ;; FIXME: interactively prompting for directory and file extension (let* (org-file-list (case-fold-search t) ; filesystems are case sensitive (file-name-regex "^[^.#].*") ; exclude .* (filext (if filext filext "org$\\\|org_archive")) (fileregex (format "%s\\.\\(%s$\\)" file-name-regex filext)) (cur-dir-list (directory-files directory t file-name-regex))) ;; loop over directory listing (dolist (file-or-dir cur-dir-list org-file-list) ; returns org-file-list (cond ((file-regular-p file-or-dir) ; regular files (if (string-match fileregex file-or-dir) ; org files (add-to-list 'org-file-list file-or-dir))) ((file-directory-p file-or-dir) (dolist (org-file (sa-find-org-file-recursively file-or-dir filext) org-file-list) ; add files found to result (add-to-list 'org-file-list org-file)))))))
5.7 visual tweaks
I use underscores "_" a lot in variable/parameter names. I don't want those to be formatted as subscript.
(setq org-export-with-sub-superscripts nil)
I use * lot. I don't want those to be formatted as bold.
(setq org-export-with-emphasize nil)
Use beautiful dark theme for HTML exported files
(setq org-html-head-extra (concat "<link rel=\"stylesheet\" type=\"text/css\" href=\"http://thomasf.github.io/solarized-css/solarized-dark.min.css\" />"))
Remove leading stars for less visual garbage.
(setq org-hide-leading-stars t)
5.8 use UUID based document hyperlinks
Enable linking between org documents using global unique ID's that will be automatically generated and inserted on demand.
UUID is awesome because you can rename file or even headings within file but link will remain working.
(setq org-id-link-to-org-use-id t) (defun anh-after-load-org () (add-to-list 'org-modules 'org-id) (org-id-update-id-locations) ) (eval-after-load "org" '(anh-after-load-org))
- Map keyboard combination C-c l to store link to current location.
- Afterwards keyboard shortcut C-c C-l inserts stored link into desired location within document.
(bind-key "C-c l" 'org-store-link)
5.9 task handling
(setq org-agenda-window-setup 'current-window)
5.9.2 log done dates
http://orgmode.org/manual/Closing-items.html
Then each time you turn an entry from a TODO (not-done) state into any of the DONE states, a line ‘CLOSED: [timestamp]’ will be inserted just after the headline. If you turn the entry back into a TODO item through further state cycling, that line will be removed again. If you turn the entry back to a non-TODO state (by pressing <C-c C-t SPC> for example), that line will also be removed, unless you set org-closed-keep-when-no-todo to non-nil.
(setq org-log-done 'time)
6 private settings
6.1 load my org files
Scan all symlinks recursively under ~/.emacs.d/org/ for org files for org-mode agenda. Beware recursive loops with symlinks.
(setq org-agenda-files (append (sa-find-org-file-recursively "~/data/sync/" "org") (sa-find-org-file-recursively "~/data/projects/projects/" "org") (sa-find-org-file-recursively "~/data/liprafarm/" "org") (sa-find-org-file-recursively "~/workspace/" "org") ) )