My .emacs Config for Samples and Examples

July 22, 2017

I still have not gotten around to setting up my emacs config in an Org file like what's talked about and referenced in this thread. But, one day.

Emacs configuration can be pretty personal, so to each their own when it comes to setting one up. However, sometimes a reference can be useful for building your own.

Without further adieu, here's some of my current, yet ever changing, .emacs file.

;; no startup message
(setq inhibit-startup-message t)
 
;; set large file warning really large
(setq large-file-warning-threshold 50000000)
 
;; turn on font-lock mode
(when (fboundp 'global-font-lock-mode)
  (global-font-lock-mode t))
(setq font-lock-maximum-decoration t)
 
;; enable visual feedback on selections
(setq transient-mark-mode t)
 
;; no toolbar
(progn
  (if (fboundp 'tool-bar-mode) (tool-bar-mode -1))
  (scroll-bar-mode -1))
 
;; Show file full path in title bar
(setq-default frame-title-format
	      (list '((buffer-file-name " %f"
					(dired-directory
					 dired-directory
					 (revert-buffer-function " %b"
								 ("%b - Dir:  " default-directory)))))))
;; default colors
(set-background-color "grey14")
(set-foreground-color "white")
(set-cursor-color "white")
 
;; make the default colors show up by default!
(add-to-list 'default-frame-alist '(foreground-color . "white"))
(add-to-list 'default-frame-alist '(background-color . "grey14"))
(add-to-list 'default-frame-alist '(cursor-color . "white"))
 
;; don't ask to spell out "yes"
(fset 'yes-or-no-p 'y-or-n-p)
 
;; disable backup
(setq backup-inhibited t)
 
;; no noise
(setq ring-bell-function 'ignore)
 
;; show line and column numbers
(line-number-mode 1)
(column-number-mode 1)
 
;; Highlight parenthesis
(show-paren-mode 1)
 
;; default encoding
(prefer-coding-system 'utf-8)
 
;; standard indent is 8 spaces
(setq standard-indent 8)
 
;; default to unified diffs
(setq diff-switches "-u")
 
;; Customizing colors used in diff mode
(defun custom-diff-colors ()
  "update the colors for diff faces"
  (set-face-attribute
   'diff-added nil :foreground "green")
  (set-face-attribute
   'diff-removed nil :foreground "red")
  (set-face-attribute
   'diff-changed nil :foreground "purple"))
(eval-after-load "diff-mode" '(custom-diff-colors))
 
;; delete trailing whitespace on save
(add-hook 'write-file-hooks 'delete-trailing-whitespace)
 
;; highlight characters after 80 column with red face
(setq whitespace-line-column 80
     whitespace-style '(face trailing lines-tail))
(global-whitespace-mode 1)
(set-face-attribute 'whitespace-line nil
                    :background "red1"
                    :foreground "yellow"
                    :weight 'bold)
 
;; load man pages when on a word and F2 is pressed
(global-set-key [(f2)]
		(lambda ()
		  (interactive) (manual-entry (current-word))))
 
;; map go to line
(global-set-key "\C-l" 'goto-line)
 
;; comment/uncomment a region
(global-set-key (kbd "C-c c") 'comment-dwim)
 
;; set default compile command
(setq compile-command "make")
 
;; when compiling, first save all
(defun save-all-and-compile ()
  (save-some-buffers 1)
  (compile compile-command))
(global-set-key [f5] 'save-all-and-compile)
 
;; don't ask to save when compiling
(setq compilation-ask-about-save nil)
 
(defun c-lineup-arglist-tabs-only (ignored)
  "Line up argument lists by tabs, not spaces"
  (let* ((anchor (c-langelem-pos c-syntactic-element))
	  (column (c-langelem-2nd-pos c-syntactic-element))
	   (offset (- (1+ column) anchor))
	    (steps (floor offset c-basic-offset)))
    (* (max steps 1)
       c-basic-offset)))
 
(add-hook 'c-mode-common-hook
          (lambda ()
            ;; Add custom kernel style
            (c-add-style
             "linux-tabs-only"
             '("linux" (c-offsets-alist
                        (arglist-cont-nonempty
                         c-lineup-gcc-asm-reg
                         c-lineup-arglist-tabs-only))))))
 
(add-hook 'c-mode-hook
          (lambda ()
            (let ((filename (buffer-file-name)))
              ;; Auto enable kernel mode for the appropriate files
              (when (and filename
                         ;; (string-match (expand-file-name "~/sb/linux") filename))
			 (string-match "linux" filename))
                (setq indent-tabs-mode t)
                (c-set-style "linux")))))
(put 'upcase-region 'disabled nil)
(put 'downcase-region 'disabled nil)
 
(c-add-style "microsoft"
	     '("stroustrup"
	       (c-offsets-alist
		(innamespace . -)
		(inline-open . 0)
		(inher-cont . c-lineup-multi-inher)
		(arglist-cont-nonempty . +)
		(template-args-cont . +))))
 
(c-add-style "openbsd"
	     '("bsd"
	       (c-backspace-function . delete-backward-char)
	       (c-syntactic-indentation-in-macros . nil)
	       (c-tab-always-indent . nil)
	       (c-hanging-braces-alist
		(block-close . c-snug-do-while))
	       (c-offsets-alist
		(arglist-cont-nonempty . *)
		(statement-cont . *))
	       (indent-tabs-mode . t)))
 
(defun match-paren (arg)
  "Go to the matching parenthesis if on parenthesis 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)))))
 
(defun my-php-mode-hook ()
  "Custom PHP mode config."
  (setq indent-tabs-mode nil
	tab-width 3
        c-basic-offset 3))
(add-hook 'php-mode-hook 'my-php-mode-hook)
 
;; At some point the default changed. I want the buffer window to open
;; in opposite window and the selection to open there as well.
(global-set-key "\C-x\C-b" 'buffer-menu-other-window)
 
;; enable ido mode, which helps with many things
(ido-mode 1)
 
;; default *.py to python-mode
(add-to-list 'auto-mode-alist '("\.py\'" . python-mode))
 
;; default *.php to php-mode.  May need to apt-get install php-elisp.
(add-to-list 'auto-mode-alist '("\.php\'" . php-mode))
 
;; include paths
(add-to-list 'load-path "~/.emacs.d/custom")
 
;; requires
(require 'dts-mode)

Don't forget to set your EDITOR environment variable to emacsclient so that emacs will always be used automatically.

Related Posts