Skip to content

Instantly share code, notes, and snippets.

@rplzzz
Last active June 18, 2022 12:58
Show Gist options
  • Save rplzzz/11258794 to your computer and use it in GitHub Desktop.
Save rplzzz/11258794 to your computer and use it in GitHub Desktop.
A sample .emacs file
;; emacs startup file.
;; uncomment next line to disable loading of "default.el" at startup
;; (setq inhibit-default-init t)
;; add personal load path
(push "~/.emacs.d/lisp" load-path)
;; set up MELPA package archive
(require 'package)
(let* ((no-ssl (and (memq system-type '(windows-nt ms-dos))
(not (gnutls-available-p))))
(proto (if no-ssl "http" "https")))
;; Comment/uncomment these two lines to enable/disable MELPA and MELPA Stable as desired
;;(add-to-list 'package-archives (cons "melpa" (concat proto "://melpa.org/packages/")) t)
(add-to-list 'package-archives (cons "melpa-stable" (concat proto "://stable.melpa.org/packages/")) t)
(when (< emacs-major-version 24)
;; For important compatibility libraries like cl-lib
(add-to-list 'package-archives '("gnu" . (concat proto "://elpa.gnu.org/packages/")))))
(package-initialize)
;;;
;;; global key bindings.
;;;
(define-key global-map "\C-ci" 'insert-buffer)
(define-key global-map [f11] 'beginning-of-buffer)
(define-key global-map [f12] 'end-of-buffer)
(define-key global-map [f4] 'manual-entry)
(define-key global-map [f5] 'dabbrev-expand)
(define-key global-map [f9] 'other-window)
(define-key global-map [f10] 'shell-next-window)
(define-key global-map [print] 'execute-extended-command)
(define-key global-map [pause] 'iconify-or-deiconify-frame)
(define-key global-map [break] 'iconify-or-deiconify-frame)
(define-key global-map [\C-backspace] 'delete-indentation)
;; mouse-4 and mouse-5 are the scroll wheel
(define-key global-map [mouse-4] (lambda () (interactive) (scroll-down 3)))
(define-key global-map [mouse-5] (lambda () (interactive) (scroll-up 3)))
(define-key global-map [\S-down-mouse-1] 'mouse-set-font) ; brings up a font menu
(global-set-key "\C-s" 'isearch-forward-regexp) ; replaces isearch-forward in default binding
(global-set-key "\C-\M-s" isearch-forward)
(global-set-key "\C-r" 'isearch-backward-regexp)
(global-set-key "\C-\M-r" 'isearch-backward)
(global-set-key "\M-%" 'query-replace-regexp)
;; Generally you should set up mode-specific key bindings in hooks,
;; but if you can be certain that the mode will have been initialized
;; already, it's ok to do it here.
(define-key lisp-interaction-mode-map "\C-m" 'newline-and-indent)
(define-key lisp-interaction-mode-map [kp-enter] 'eval-print-last-sexp)
(define-key lisp-interaction-mode-map "<C-return>" 'eval-print-last-sexp)
;;;
;;; Path to ispell on mac
(setq ispell-program-name "/usr/local/bin/ispell")
;;;
;;; Some useful (?) interactive functions
;;;
(defun word-count-region (start stop)
"Count the words in the region. This function calls count-matches with an
appropriate regexp."
(interactive "d\nm")
(count-matches "\\b\\w+\\b" start stop t))
(global-set-key "\C-cw" 'word-count-region)
(defun shell-next-window ()
"Spawn a shell in another window, or switch to the shell buffer if one already exists."
(interactive)
(let ((buf (get-buffer "*shell*")))
(if buf ;check for existence of shell buffer
(if (not (equal (current-buffer) buf))
(progn
(pop-to-buffer "*shell*" t))
nil)
;;if no shell:
(if (one-window-p t) ;Make a new window, if we have
(split-window)) ; only one
(select-window (next-window (selected-window) 666)) ; go to other window
(shell)))) ;execute the shell
;;;
;;; hooks: these functions will be run in a buffer whenever the
;;; corresponding mode is started. Setting mode options and defining
;;; mode-specific key bindings should usually happen here.
;;;
(add-hook 'text-mode-hook
(function
(lambda () (auto-fill-mode t))))
(add-hook 'dired-mode-hook
(function
(lambda ()
(define-key dired-mode-map "\C-c\C-f" 'find-name-dired)
(define-key dired-mode-map "\C-cg" 'find-grep-dired))))
(add-hook 'latex-mode-hook
(function
(lambda ()
(define-key latex-mode-map "\C-c;" 'comment-region))))
(add-hook 'perl-mode-hook
(function
(lambda ()
;;(define-key perl-mode-map "\C-c\C-c" 'compile)
(define-key perl-mode-map "\C-cl" 'goto-line)
(define-key perl-mode-map "\C-m" 'reindent-then-newline-and-indent)
(font-lock-mode 1))))
(add-hook 'python-mode-hook
(function
(lambda ()
(make-local-variable 'line-number-mode)
(line-number-mode t)
(define-key python-mode-map "\C-cl" 'goto-line)
(define-key python-mode-map "\C-c;" 'comment-region)
(define-key python-mode-map "\C-c[" 'python-beginning-of-block)
(define-key python-mode-map "\C-c]" 'python-end-of-block)
(define-key python-mode-map "\C-c{" 'python-beginning-of-defun)
(define-key python-mode-map "\C-c}" 'python-end-of-defun)
(font-lock-mode 1))))
(add-hook 'c-mode-hook
(function
(lambda ()
(make-local-variable 'line-number-mode)
(line-number-mode t)
(font-lock-mode 1)
(setq indent-tabs-mode nil)
(define-key c-mode-map "\M-q" 'c-fill-paragraph)
(define-key c-mode-map "\C-cl" 'goto-line)
(define-key c-mode-map "\C-m" 'reindent-then-newline-and-indent)
(define-key c-mode-map "\C-c\C-c" 'compile)
(define-key c-mode-map "\C-c;" 'comment-region)
(define-key c-mode-map "\C-cb" 'c-beginning-of-defun)
(setq c-auto-newline nil)
(if (string-match "test" buffer-file-name)
(setq default-make-rule "test")
(setq default-make-rule "all"))
(setq c-brace-offset -1)
(make-local-variable 'compile-command)
(setq compile-command (concat "make -k " default-make-rule)))))
(add-hook 'c++-mode-hook
(function
(lambda ()
(make-local-variable 'line-number-mode)
(line-number-mode t)
(font-lock-mode 1)
(setq indent-tabs-mode nil)
(define-key c++-mode-map "\M-q" 'c-fill-paragraph)
(define-key c++-mode-map "\C-cl" 'goto-line)
(define-key c++-mode-map "\C-m" 'reindent-then-newline-and-indent)
(define-key c++-mode-map "\C-c\C-c" 'compile)
(define-key c++-mode-map "\C-c;" 'comment-region)
(define-key c++-mode-map "\C-cb" 'c-beginning-of-defun)
(define-key c++-mode-map (kbd "C-`") 'next-error)
;(define-key c++-mode-map ":" 'self-insert-command)
(setq c-auto-newline nil)
(if (string-match "test" buffer-file-name)
(setq default-make-rule "test")
(setq default-make-rule "all"))
(c-guess)
(make-local-variable 'compile-command)
(setq compile-command (concat "make -k " default-make-rule)))))
(add-hook 'java-mode-hook
(function
(lambda ()
(make-local-variable 'line-number-mode)
(line-number-mode t)
(font-lock-mode 1)
(setq indent-tabs-mode nil)
(define-key java-mode-map "\C-cl" 'goto-line)
(define-key java-mode-map "\C-m" 'reindent-then-newline-and-indent)
(define-key java-mode-map "\C-c\C-c" 'compile)
(define-key java-mode-map "\C-c;" 'comment-region)
(make-local-variable 'compile-command)
(setq compile-command (concat "javac " buffer-file-name)))))
(add-hook 'lisp-mode-hook
(function
(lambda ()
(font-lock-mode 1)
(setq lisp-indent-offset 2)
(define-key lisp-mode-map "\C-m" 'reindent-then-newline-and-indent)
(define-key lisp-mode-map "\C-c;" 'comment-region)
(define-key lisp-mode-map "\C-cl" 'goto-line))))
(add-hook 'octave-mode-hook
(function
(lambda ()
(make-local-variable 'line-number-mode) ; turn on line number mode locally
(line-number-mode t)
(font-lock-mode 1) ;turn on font lock
(define-key octave-mode-map "\C-cl" 'goto-line)
(define-key octave-mode-map "\C-m" 'reindent-then-newline-and-indent))))
;;;
;;; Add entries to auto-mode alist. Determines which mode to start up
;;; when a file is visited based on the filename.
;;;
(setq auto-mode-alist (cons '("\\.m\\'" . octave-mode) auto-mode-alist))
(setq auto-mode-alist (cons '("\\.F90\\'" . f90-mode) auto-mode-alist))
;; commit log messages in text mode
(setq auto-mode-alist (cons '("COMMIT_EDITMSG" . text-mode) auto-mode-alist))
;; related: determine whether a file is a temp file
(setq server-temp-file-regexp
"^/tmp/Re\\|/draft$\\|/.letter\\|/.article\\|\\<foo\\|\\<KILL\\>")
;; settings for character terminals. I'll probably never use them again, but whatever
(setq enable-flow-control-on "vt220")
(setq visible-bell t)
;;;
;;; Set up specialized major modes
;;;
(if (not (version< emacs-version "24.3"))
(progn
(require 'markdown-mode)
(require 'ess-site) ; load the setup file for ESS, which is a mode for R programming
))
(add-hook 'ess-mode-hook
(lambda ()
(auto-fill-mode t)
(set-fill-column 80)
(define-key ess-mode-map "\C-c;" 'comment-region)
(define-key ess-mode-map "\C-cl" 'goto-line)
(define-key ess-mode-map (kbd "s--") 'ess-smart-S-assign)
(define-key ess-mode-map "_" 'self-insert-command)
(add-to-list 'ess-style-alist
'(jgcri ; based on C++ style
(ess-indent-level . 4)
(ess-first-continued-statement-offset . 2)
(ess-continued-statement-offset . 0)
(ess-brace-offset . -4)
(ess-expression-offset . 4)
(ess-else-offset . 0)
(ess-close-brace-offset . 0)
(ess-brace-imaginary-offset . 0)
(ess-continued-brace-offset . 0)
(ess-arg-function-offset . 0)
(ess-arg-function-offset-new-line . '(4))
))
(ess-set-style 'jgcri 'quiet)
;; Because
;; DEF GNU BSD K&R C++
;; ess-indent-level 2 2 8 5 4
;; ess-continued-statement-offset 2 2 8 5 4
;; ess-brace-offset 0 0 -8 -5 -4
;; ess-arg-function-offset 2 4 0 0 0
;; ess-expression-offset 4 2 8 5 4
;; ess-else-offset 0 0 0 0 0
;; ess-close-brace-offset 0 0 0 0 0
(add-hook 'local-write-file-hooks
(lambda ()
(ess-nuke-trailing-whitespace)))))
(setq ess-nuke-trailing-whitespace-p t)
;; ;; set up Chapel major mode (if you have Chapel)
;; ; helper macros
;; (defmacro c-paren-re (re)
;; `(concat "\\(" ,re "\\)"))
;; (defmacro c-identifier-re (re)
;; `(concat "\\[^_]"))
;;
;; ; query the environment variable CHPL_HOME
;; (setq chpl-home (getenv "CHPL_HOME"))
;;
;; ; prepend $CHPL_HOME/etc/emacs/x.y to your emacs load-path where x.y
;; ; is your emacs version number
;; (if (string= chpl-home nil)
;; nil
;; (setq load-path (cons (concat chpl-home
;; "/etc/emacs/"
;; (number-to-string emacs-major-version)
;; "."
;; (number-to-string emacs-minor-version))
;; load-path))
;; )
;;
;; ; make sure that when chpl-mode is entered, (our modified) cc-mode is
;; ; loaded
;; (autoload 'chpl-mode "chpl-mode" "Chpl enhanced cc-mode" t)
;;
;; ; make loading files with a .chpl extension put emacs into chpl-mode
;; (setq auto-mode-alist (cons '("\\.chpl$" . chpl-mode)
;; auto-mode-alist))
;;;
;;; Miscellaneous interface customization
;;;
;; transient mark options -- obviously you should pick only one
;;(setq transient-mark-mode t) ; enable visual feedback on selections
(setq transient-mark-mode nil) ; disable visual feedback on selections
;; default to better frame titles
(setq frame-title-format
(concat "%b - emacs@" (system-name)))
;; default to unified diffs, if that's how you roll
;;(setq diff-switches "-u")
;; turn on font-lock mode (i.e., syntax highlighting)
(when (fboundp 'global-font-lock-mode)
(global-font-lock-mode t))
;; set up the fortune program. Everybody likes fortunes.
(set-variable 'fortune-file "/usr/share/games/fortunes") ; replace with system-appropriate location
(set-variable 'fortune-program "/usr/games/fortune") ; likewise
(global-set-key "\C-x\C-m" 'fortune) ; control-x return
;; always end a file with a newline. Choices are
;; nil - don't require final newline
;; t - add when about to save (if necessary)
;; "visit" - add newline when the file is visited
;; "visit-save" - add both on visit and save
;; anything else - ask on save
(setq require-final-newline "visit-save")
;; for files with multiple names, make the backup file by copying
;; (instead of renaming), even if the normal setting is to rename
(setq backup-by-copying-when-linked t)
;; change the amount by which the display scrolls when the point moves
;; just off screen (default is to recenter)
(setq scroll-step 5)
;; Thes interactive commands are normally disabled. The following enables them.
(put 'narrow-to-region 'disabled nil)
(put 'eval-expression 'disabled nil)
(iswitchb-mode 1) ; turn on Iswitchb mode, a better interface for switching amongst buffers
(server-start) ; for using emacsclient as your editor
(display-time)
;;; customizations set by the interactive menus. If you don't know
;;; how to set something, often you can figure it out by setting it in
;;; the menu and then loading up your .emacs to see what it did.
(custom-set-variables
;; custom-set-variables 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.
'(inhibit-startup-screen nil)
'(ns-alternate-modifier (quote super))
'(ns-command-modifier (quote meta)))
(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 "MidnightBlue" :foreground "AntiqueWhite" :inverse-video nil :box nil :strike-through nil :overline nil :underline nil :slant normal :weight normal :height 120 :width normal :foundry "nil" :family "Menlo"))))
'(cursor ((t (:background "wheat")))))
;;; Remember when we switched to CVS because it was such a big
;;; improvement over RCS? This should probably go, but sometimes
;;; it's good to remember where you came from.
(load-library "vc")
(global-set-key "\C-cv" vc-prefix-map)
(global-set-key "\C-xv" 'view-file)
(setq vc-initial-comment t)
(setq vc-diff-switches "-cb")
(setenv "CVSROOT" ":ext:sleet4:/common/cvsroot")
(setenv "CVS_RSH" "/usr/bin/ssh")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment