Skip to content

Instantly share code, notes, and snippets.

@astanin
Created July 21, 2010 16:05
Show Gist options
  • Save astanin/484686 to your computer and use it in GitHub Desktop.
Save astanin/484686 to your computer and use it in GitHub Desktop.
.emacs
(tool-bar-mode 0)
(scroll-bar-mode 0)
(menu-bar-mode 0)
(defun add-subdirs-to-load-path (dir)
(let ((default-directory (concat dir "/")))
(normal-top-level-add-subdirs-to-load-path)))
;;; My location for external packages.
(add-to-list 'load-path "~/.emacs.d/site-lisp")
(add-subdirs-to-load-path "~/.emacs.d/site-lisp")
;; (add-to-list 'load-path "~/.emacs.d/color-theme-solarized")
;; el-get
(add-to-list 'load-path "~/.emacs.d/el-get/el-get")
(unless (require 'el-get nil t)
(url-retrieve
"https://raw.github.com/dimitri/el-get/master/el-get-install.el"
(lambda (s)
(end-of-buffer)
(eval-print-last-sexp))))
(el-get 'sync)
;; (when (>= emacs-major-version 24)
;; (require 'color-theme)
;; (setq color-theme-is-global t)
;; ;(color-theme-comidia)
;; ;(color-theme-emacs-21)
;; (require 'color-theme-solarized)
;; (if window-system
;; (color-theme-solarized-light)
;; (color-theme-arjen)))
;;; TABs vs spaces
;; Source: http://stackoverflow.com/questions/742425/
(setq-default c-basic-offset 4) ; indents 4 chars
(setq-default tab-width 4) ; and 4 char wide for TAB
(setq-default indent-tabs-mode nil) ; And force use of spaces
;;; Rainbow parentheses
(require 'rainbow-delimiters)
;;; Show and remove trailing whitespace
(add-hook 'before-save-hook 'delete-trailing-whitespace)
;;; Highlight beyond 80 columns
(require 'whitespace) ;; emacs 23+
(setq-default whitespace-style '(face empty tabs lines-tail trailing))
(global-whitespace-mode t)
;;;
;;; Vim habits. To make life of a vim refugee bearable...
;;;
(setq-default tags-revert-without-query 1) ; reload TAGS file automatically
;; I like dd, dw, d} in vim (and similar commands). Emacs is not as ergonomic.
;;
;; This is to switch to vi mode and back quickly:
(global-set-key (kbd "C-<escape>") 'toggle-viper-mode)
;; Like both ^ and 0 in vim (BTW, C-a is for 0 and M-m is for ^)
;; Thanks to:http://stackoverflow.com/questions/145291/smart-home-in-emacs
(global-set-key [home] 'smart-beginning-of-line)
(defun smart-beginning-of-line ()
"Move point to first non-whitespace character or beginning-of-line.
Move point to the first non-whitespace character on this line.
If point was already at that position, move point to beginning of line."
(interactive)
(let ((oldpos (point)))
(back-to-indentation)
(and (= oldpos (point))
(beginning-of-line))))
;; Goto to file under cursor, gf in vim
(defun shell-command-to-string (command)
"Execute shell command COMMAND and return its output as a string."
(with-output-to-string
(with-current-buffer standard-output
(call-process shell-file-name nil t nil shell-command-switch command))))
(defun goto-file ()
"open file under cursor"
(interactive)
(find-file (shell-command-to-string
(concat "locate " (current-word) "|head -c -1"))))
(global-set-key (kbd "C-c C-f") 'goto-file)
;; Move to column (like | in vim)
(global-set-key (kbd "M-g |") 'interactive-move-to-column)
(global-set-key (kbd "M-g M-\\") 'interactive-move-to-column)
(defun interactive-move-to-column (arg)
(interactive "NMove to column:")
(move-to-column arg))
;; Simulate * of vim with C-*
;; Thanks to http://www.emacswiki.org/emacs/SearchAtPoint
;; TODO: add something for #
(global-set-key (kbd "C-*") 'my-isearch-word-at-point)
(defun my-isearch-word-at-point ()
(interactive)
(call-interactively 'isearch-forward-regexp))
(defun my-isearch-yank-word-hook ()
(when (equal this-command 'my-isearch-word-at-point)
(let ((string (concat "\\<"
(buffer-substring-no-properties
(progn (skip-syntax-backward "w_") (point))
(progn (skip-syntax-forward "w_") (point)))
"\\>")))
(if (and isearch-case-fold-search
(eq 'not-yanks search-upper-case))
(setq string (downcase string)))
(setq isearch-string string
isearch-message
(concat isearch-message
(mapconcat 'isearch-text-char-description
string ""))
isearch-yank-flag t)
(isearch-search-and-update))))
(add-hook 'isearch-mode-hook 'my-isearch-yank-word-hook)
;; Simulate % of vim with C-%
;; thanks to http://grok2.tripod.com/
;; TODO: extend for other kinds of braces [], {}, <>
(global-set-key (kbd "C-%") 'match-paren)
(defun match-paren (arg)
"Go to the matching paren if on a paren; otherwise insert %."
(interactive "p")
(cond ((looking-at "\\s\(") (forward-list 1) (backward-char 1))
((looking-at "\\s\)") (forward-char 1) (backward-list 1))
(t (self-insert-command (or arg 1)))))
;; Simuilate >> and << of vim with C-c > and C-c <
;; TODO: find out how globally settings' variables work
(setq-default default-indent-shift 4)
(defun shift-indent (delta)
(let ((shift (if (= (abs delta) 1)
(* default-indent-shift delta)
delta))
(b (if (use-region-p)
(region-beginning)
(line-beginning-position)))
(e (if (use-region-p)
(region-end)
(line-end-position)))
)
(indent-rigidly b e shift)))
(defun increase-indent-of-region (arg)
(interactive "p")
(shift-indent arg))
(defun decrease-indent-of-region (arg)
(interactive "p")
(shift-indent (- 0 arg)))
(global-set-key (kbd "C-c >") 'increase-indent-of-region)
(global-set-key (kbd "C-c <") 'decrease-indent-of-region)
(global-set-key (kbd "C-c C-.") 'increase-indent-of-region)
(global-set-key (kbd "C-c C-,") 'decrease-indent-of-region)
;;; Comment/uncomment
(global-set-key (kbd "C-c C-c") 'comment-or-uncomment-region)
(global-set-key (kbd "M-#") 'comment-or-uncomment-region)
;;;
;;; Jewels from Emacs Starter Kit
;;;
;;; (initially taken from http://github.com/zahardzhan/emacs-starter-kit)
;;;
;; Like everywhere else, C-w kills word or region.
(global-set-key (kbd "C-w") 'backward-kill-word-or-kill-region)
(define-key minibuffer-local-map (kbd "C-w") 'backward-kill-word-or-kill-region)
(defun backward-kill-word-or-kill-region (arg)
(interactive "p")
(if (region-active-p)
(kill-region (region-beginning)
(region-end))
(backward-kill-word arg)))
;; Buffer switching
(global-set-key (kbd "C-x C-b") 'ibuffer)
(global-set-key (kbd "C-x C-k") 'kill-current-buffer)
(global-set-key [(control tab)] 'ido-switch-buffer)
(defun kill-current-buffer ()
(interactive)
(kill-buffer (current-buffer)))
;; Window switching. (C-x o goes to the next window)
(windmove-default-keybindings) ;; Shift+direction
(global-set-key (kbd "C-x C-o") 'other-window)
(global-set-key [(shift iso-lefttab)] 'other-window)
(global-set-key [(shift control iso-lefttab)] (lambda () (interactive) (other-window -1)))
;;; Some shortcuts for one-handed operation.
;; C-1 instead of C-x 1
(global-set-key (kbd "C-1") 'delete-other-windows)
;; C-2 instead of C-x 2
(global-set-key (kbd "C-2") 'split-window-vertically)
;; C-3 instead of C-x 3
(global-set-key (kbd "C-3") 'split-window-horizontally)
;; C-0 instead of C-x 0
(global-set-key (kbd "C-0") 'delete-window)
;; I use system settings for Alt-Space layout switching and this
;; prevents Emacs from doing anything when Alt-Shift is pressed.
(global-unset-key (kbd "M-SPC"))
;; an alternative is
;; (global-set-key (kbd "M-SPC") 'toggle-input-method)
;; I use system Alt-TAB to switch windows.
(global-unset-key (kbd "M-TAB"))
;; C-z is more useful as undo than default Emacs keybinding.
;; (I have other keys to minimize a windo^W frame).
;; Unbind Pesky Sleep Button
(global-unset-key [(control z)])
(global-unset-key [(control x)(control z)])
(global-set-key (kbd "C-z") 'undo)
;; Search should be always Regex
(global-set-key (kbd "C-s") 'isearch-forward-regexp)
(global-set-key (kbd "C-r") 'isearch-backward-regexp)
(global-set-key (kbd "C-M-s") 'isearch-forward)
(global-set-key (kbd "C-M-r") 'isearch-backward)
;; The most useful .emacs snippet ever.
;; /via @dottedmag: http://gist.github.com/481344
(defun x ()
"Run X terminal in current directory"
(interactive)
(start-process "shell-x" nil "gnome-terminal"))
(global-set-key (kbd "C-x x") 'x)
;;;
;;;
;;; Cursor moving
;;;
;;; see Vim stuff above.
;;; Javascript
(add-to-list 'auto-mode-alist '("\\.js$" . javascript-mode))
(add-to-list 'auto-mode-alist '("\\.json$" . javascript-mode))
;;; Scala
(require 'scala-mode-auto)
(add-to-list 'auto-mode-alist '("\\.scala$" . scala-mode))
(add-hook 'scala-mode-hook
'(lambda ()
(scala-mode-feature-electric-mode)
))
;;; Haskell
;(load "~/lib/emacs/haskell-mode/haskell-site-file")
(load "haskell-site-file")
(add-hook 'haskell-mode-hook 'turn-on-haskell-doc-mode)
;;(add-hook 'haskell-mode-hook 'turn-on-haskell-indentation)
(add-hook 'haskell-mode-hook 'turn-on-haskell-indent)
;;(add-hook 'haskell-mode-hook 'turn-on-haskell-simple-indent)
;; Literate Haskell: http://www.haskell.org/haskellwiki/Literate_programming
;; (add-hook 'haskell-mode-hook 'my-mmm-mode)
;; (mmm-add-classes
;; '((literate-haskell-bird
;; :submode text-mode
;; :front "^[^>]"
;; :include-front true
;; :back "^>\\|$"
;; )
;; (literate-haskell-latex
;; :submode literate-haskell-mode
;; :front "^\\\\begin{code}"
;; :front-offset (end-of-line 1)
;; :back "^\\\\end{code}"
;; :include-back nil
;; :back-offset (beginning-of-line -1)
;; )))
;; (defun my-mmm-mode ()
;; ;; go into mmm minor mode when class is given
;; (make-local-variable 'mmm-global-mode)
;; (setq mmm-global-mode 'true)
;; )
;; (set-face-foreground 'font-lock-doc-face "gray40")
;; (setq mmm-submode-decoration-level 0)
;; (setq haskell-font-lock-symbols t)
;;;
;;; AUCTeX-related
;;;
(require 'tex-site nil 'noerror)
(setq TeX-auto-save t)
(setq TeX-parse-self t)
(setq-default TeX-master nil)
(setq-default TeX-PDF-mode t)
(custom-set-variables
;; custom-set-variables was added by Custom edited by hand.
;; If you edit it by hand, you could mess it up, so be careful.
;; Your init file should contain only one such instance.
;; If there is more than one, they won't work right.
'(browse-url-browser-function (quote browse-url-generic))
'(browse-url-generic-program "firefox")
'(ecb-layout-name "left6")
'(ecb-options-version "2.32")
'(ecb-source-path (quote (("~/work/" "~/work"))))
'(ecb-tree-buffer-style (quote image))
'(ecb-tree-expand-symbol-before nil)
'(ediff-highlight-all-diffs t)
'(indent-tabs-mode nil)
'(inhibit-startup-screen t)
'(latex-run-command "pdflatex")
'(safe-local-variable-values (quote ((mmm-classes . literate-haskell-latex))))
'(standard-indent 4))
;; Set xetex mode in TeX/LaTeX
(add-hook 'LaTeX-mode-hook
(lambda()
(add-to-list
'TeX-command-list
'("XeLaTeX" "%`xelatex%(mode)%' %t" TeX-run-TeX nil t))
(setq TeX-command-default "XeLaTeX")
(setq TeX-save-query nil)
;;(setq TeX-show-compilation t)
))
;;; YAML mode
(require 'yaml-mode)
(add-to-list 'auto-mode-alist '("\\.yaml$" . yaml-mode))
(add-to-list 'auto-mode-alist '("\\.yml$" . yaml-mode))
;;; Haskell SHIME
(require 'shime)
(require 'haskell-mode)
(add-to-list 'auto-mode-alist '("\\.hs$" . haskell-mode))
(add-to-list 'auto-mode-alist '("\\.lhs$" . haskell-mode))
;;; Mercurial mode
(require 'mercurial)
;; from http://www.joshmatthews.net/blog/2010/01/dealing-with-mercurial-patch-queue-rejects-in-emacs/
(defun switch-hg-reject ()
(interactive)
(let ((other-file
(if (string= (substring (buffer-file-name) -4 nil) ".rej")
(substring (buffer-file-name) 0 -4)
(concat (buffer-file-name) ".rej"))))
(if (file-exists-p other-file)
(switch-to-buffer (find-file-noselect other-file))
(message (format "No alternate reject file found" other-file)))))
(global-set-key (kbd "C-c r") 'switch-hg-reject)
;;; Git mode
(require 'magit nil 'noerror)
;;; Bibliography mode
(autoload 'ebib "ebib" "Ebib, a BibTeX database manager." t)
;;; calendar
(require 'calendar)
(setq european-calendar-style 't) ;(european-calendar)
(setq calendar-week-start-day 1)
(calendar-set-date-style 'iso)
;;; Org mode
(require 'org-install)
(add-to-list 'auto-mode-alist '("\\.org$" . org-mode))
(define-key global-map "\C-cl" 'org-store-link)
(define-key global-map "\C-ca" 'org-agenda)
(setq org-fast-tag-selection-include-todo t)
(setq org-todo-keywords
'((sequence "TODO(t)" "|" "WAIT(w)" "|" "DONE(d)" "CANCELLED(c)")))
(setq org-log-done t)
;;; Autocomplete
;;; http://hide1713.wordpress.com/2009/01/30/setup-perfect-python-environment-in-emacs/
(require 'auto-complete)
(global-auto-complete-mode t)
(define-key ac-complete-mode-map "\M-n" 'ac-next)
(define-key ac-complete-mode-map "\M-p" 'ac-previous)
;;;
;;; Appearance and look-and-feel
;;;
;;;
;;; Highlight diffs
;;;
(defun update-diff-colors ()
"update the colors for diff faces"
(set-face-attribute 'diff-added nil
:foreground "white" :background "blue")
(set-face-attribute 'diff-removed nil
:foreground "white" :background "red3")
(set-face-attribute 'diff-changed nil
:foreground "white" :background "purple"))
(eval-after-load "diff-mode"
'(update-diff-colors))
;;; Or use
;; (require 'diff-mode-)
(custom-set-faces
;; custom-set-faces was added by Custom.
;; If you edit it by hand, you could mess it up, so be careful.
;; Your init file should contain only one such instance.
;; If there is more than one, they won't work right.
'(default ((t (:inherit nil :stipple nil :background "white" :foreground "black" :inverse-video nil :box nil :strike-through nil :overline nil :underline nil :slant normal :weight normal :height 98 :width normal :foundry "unknown" :family "DejaVu Sans Mono"))))
'(font-lock-doc-face ((t (:inherit font-lock-string-face)))))
;; Window size
(if (window-system)
(set-frame-height (selected-frame) 42))
;;(desktop-save-mode 1)
;;; Archlinux stuff
(autoload 'pkgbuild-mode "pkgbuild-mode.el" "PKGBUILD mode." t)
(setq auto-mode-alist (append '(("/PKGBUILD$" . pkgbuild-mode))
auto-mode-alist))
;;;
;;; Homeless snippets...
;;;
(defun clipboard-kill-ring-save-buffer ()
"Copy entire buffer to clipboard, and preserve the cursor position"
(interactive)
(clipboard-kill-ring-save (point-min) (point-max)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment