Skip to content

Instantly share code, notes, and snippets.

@kevinjamescasey
Last active February 25, 2017 23:26
Show Gist options
  • Save kevinjamescasey/31152cc72e1449a386e8b0bede1c01d4 to your computer and use it in GitHub Desktop.
Save kevinjamescasey/31152cc72e1449a386e8b0bede1c01d4 to your computer and use it in GitHub Desktop.
make GNU Emacs useful

This opens a file in the already running Emacs instance provided you have (server-start) in your init.el: alias emax='/Applications/Emacs.app/Contents/MacOS/bin/emacsclient -n'

Usage example: emax ~/.emacs.d/init.el

Note: I couldn't get the OS X open command to use emacslient as the default editor. The bash EDITOR variable doesn't seem to work.

(require 'package)
(when (< emacs-major-version 24)
;; For important compatibility libraries like cl-lib
(add-to-list 'package-archives '("gnu" . "http://elpa.gnu.org/packages/")))
;;Milkypostman’s Emacs Lisp Package Archive https://melpa.org/#/
;;automatically pulled from github
(add-to-list 'package-archives '("melpa-stable" . "https://stable.melpa.org/packages/"))
;;cutting edge MELPA
;(add-to-list 'package-archives '("melpa" . "https://melpa.org/packages/"))
;;https://marmalade-repo.org/
;;packages manually uploaded
(add-to-list 'package-archives '("marmalade" . "https://marmalade-repo.org/packages/"))
(package-initialize)
(defvar my-packages '(better-defaults ;;https://github.com/technomancy/better-defaults
smex ;;interactive M-x
ido-ubiquitous ;;interactive almost everything
neotree ;;narrow directory tree on left
; idle-highlight-mode
expand-region
find-file-in-project ;;https://github.com/technomancy/find-file-in-project
ag ;;grep and find stuff
projectile ;; file commands based on project root
company ;; complete anything you start to type
magit ;;git client
dumb-jump
clojure-mode
cider ;;clojure IDE that Rocks
paredit ;;parens for LISPs
scala-mode2
json-mode
;coffee-mode
markdown-mode
markdown-preview-mode
web-mode
;added for http://codewinds.com/blog/2015-04-02-emacs-flycheck-eslint-jsx.html
flycheck ;;automatic syntax checking
;js2-mode ;;nice js editing mode which recognizes modern ES6+ features
;exec-path-from-shell ;;for OSX
tern;javascript tooling http://ternjs.net/doc/manual.html#configuration
company-tern
multiple-cursors
))
;; (package-install 'use-package)
;; (require 'use-package)
;; (use-package ensime
;; :ensure t
;; :pin melpa-stable)
;;common lisp library for loop
(require 'cl)
(defun my-packages-installed-p ()
(loop for p in my-packages
when (not (package-installed-p p)) do (return nil)
finally (return t)))
(unless (my-packages-installed-p)
;; check for new packages (package versions)
(message "%s" "Emacs is now refreshing its package database...")
(package-refresh-contents)
(message "%s" " done.")
;; install the missing packages
(dolist (p my-packages)
(unless (package-installed-p p)
(package-install p))))
;allow emacs client to open files in existing frame
(server-start)
;;never iconify when I accidentally hit these keys
(global-set-key (kbd "C-x C-z") nil)
;; (require 'ido-ubiquitous)
(ido-ubiquitous-mode 1)
(ido-everywhere 1)
;; (setq org-completion-use-ido t)
;; (setq magit-completing-read-function 'magit-ido-completing-read)
;;Smex is a M-x enhancement for Emacs. Built on top of Ido, it provides a convenient interface to your recently and most frequently used commands.
;; C-h f, while Smex is active, runs describe-function on the currently selected command.
;; M-. jumps to the definition of the selected command.
;; C-h w shows the key bindings for the selected command. (Via where-is.)
(global-set-key (kbd "M-x") 'smex)
(global-set-key (kbd "M-X") 'smex-major-mode-commands)
;; This is your old M-x.
;(global-set-key (kbd "C-c C-c M-x") 'execute-extended-command)
;;overwrite selected region like every other editor
(delete-selection-mode 1)
(setq select-enable-primary nil)
(global-set-key (kbd "C-=") 'er/expand-region)
(global-set-key (kbd "C-x f") 'ffip)
; adds dired-jump C-x C-j
(require 'dired-x)
;;stuff from the newest better-defaults
(autoload 'zap-up-to-char "misc"
"Kill up to, but not including ARGth occurrence of CHAR." t)
(global-set-key (kbd "M-z") 'zap-up-to-char)
(setq require-final-newline nil
visible-bell t
;; load-prefer-newer t ;;causes recursive load errors. can't even close emacs
ediff-window-setup-function 'ediff-setup-windows-plain)
(add-hook 'emacs-lisp-mode-hook
(lambda () (paredit-mode 1)))
;;file commands based on project root
(projectile-global-mode)
;;complete any
(add-hook 'after-init-hook 'global-company-mode)
;;git
(global-set-key (kbd "C-x g") 'magit-status)
;;web stuff
;(add-hook 'js-mode-hook 'js2-minor-mode)
;(add-hook 'js2-mode-hook 'ac-js2-mode)
(add-to-list 'auto-mode-alist '("\\.html?\\'" . web-mode))
(add-to-list 'auto-mode-alist '("\\.jsx?\\'" . web-mode))
;;highlight occurence of word at point
;; (add-hook 'clojure-mode-hook 'idle-highlight-mode)
;; (add-hook 'web-mode-hook 'idle-highlight-mode)
;; (add-hook 'coffe-mode-hook 'idle-highlight-mode)
(add-hook 'web-mode-hook (lambda () (tern-mode t)))
;;clojure
(add-hook 'clojure-mode-hook 'paredit-mode)
(add-hook 'clojure-mode-hook 'cider-mode)
(add-hook 'clojure-mode-hook
(lambda () (local-set-key (kbd "M-[") #'paredit-wrap-square)))
;; auto complete for clojure
;; (add-hook 'cider-repl-mode-hook #'company-mode)
;; (add-hook 'cider-mode-hook #'company-mode)
;;(add-to-list 'auto-mode-alist '("\\.clj?\\'" . cider-mode))
(setq cider-cljs-lein-repl "(do (use 'figwheel-sidecar.repl-api) (start-figwheel!) (cljs-repl))")
;;provides an option do use conflicting lines from both files
(defun ediff-copy-both-to-C ()
(interactive)
(ediff-copy-diff ediff-current-difference nil 'C nil
(concat
(ediff-get-region-contents ediff-current-difference 'A ediff-control-buffer)
(ediff-get-region-contents ediff-current-difference 'B ediff-control-buffer))))
(defun add-d-to-ediff-mode-map () (define-key ediff-mode-map "d" 'ediff-copy-both-to-C))
(add-hook 'ediff-keymap-setup-hook 'add-d-to-ediff-mode-map)
;;tags
(defun tags-utils/shell-in-dir (command directory)
"Behaves the same as `shell-command-to-string', but allows us
to specify the directory to run the command in.
Note that it's not sufficient to use absolute paths with
shell-command-to-string, since it assumes that the buffer's value
of `default-directory' is a path that exists. If not, it crashes."
(let ((normalized-path (file-name-as-directory directory))
(original-default-directory default-directory)
(command-output))
(when (not (file-exists-p normalized-path))
(error "Directory %s doesn't exist" normalized-path))
(setq default-directory normalized-path)
(setq command-output (shell-command-to-string command))
(setq default-directory original-default-directory)
command-output))
(defun tags-utils/regenerate (file-path)
(tags-utils/shell-in-dir "rm -f TAGS" file-path)
(message (tags-utils/shell-in-dir "/usr/local/Cellar/ctags/5.8_1/bin/ctags -e -R ." file-path)))
;; todo: move to http://www.emacswiki.org/emacs/repository-root.el
(defun tags-utils/project-root (file-path)
(unless file-path (error "This buffer isn't associated with a file"))
(let ((git-repo-path (vc-git-root file-path)))
(or git-repo-path (read-directory-name "Project root: " default-directory))))
(defun tags-generate-for-this-repo ()
"Regenerate the TAGS file in the root of the current git
repository. This TAGS table is then added to the list of
tags tables searched by Emacs."
(interactive)
(let ((project-root (tags-utils/project-root default-directory)))
(tags-utils/regenerate project-root)
(add-to-list 'tags-table-list (concat project-root "TAGS"))))
;;needed on OS X
;;(add-to-list 'exec-path "/usr/local/bin")
(setenv "PATH" (concat (getenv "PATH") ":/usr/local/bin")) ;
(setq exec-path (append exec-path '("/usr/local/bin")))
(setq ring-bell-function (lambda () (message "*wooooooooooooooooooop*")));attempt to get rid of strange display artifacts
;; use begining of line for comment-dwim region
(defun comment-dwim-line (&optional arg)
"Replacement for the comment-dwim command.
If no region is selected and current line is not blank and we are not at the end of the line,
then comment current line.
Replaces default behaviour of comment-dwim, when it inserts comment at the end of the line."
(interactive "*P")
(comment-normalize-vars)
(if (and (not (region-active-p)) (not (looking-at "[ \t]*$")))
(comment-or-uncomment-region (line-beginning-position) (line-end-position))
(comment-dwim arg)))
(global-set-key "\M-;" 'comment-dwim-line)
;;http://www.blogbyben.com/2010/08/handy-emacs-function-url-decode-region.html stole from here
;;http://ergoemacs.org/emacs/elisp_decode_uri_percent_encoding.html best explination of (require 'url-util)
(defun url-decode-region (start end)
"Replace a region with the same contents, only URL decoded."
(interactive "r")
(let ((text (url-unhex-string (buffer-substring start end))))
(delete-region start end)
(insert text)))
(defun url-encode-region (start end)
"Replace a region with the same contents, only URL encoded."
(interactive "r")
(let ((text (url-hexify-string (buffer-substring start end))))
(delete-region start end)
(insert text)))
;; http://stackoverflow.com/questions/6286579/emacs-shell-mode-how-to-send-region-to-shell
(defun sh-send-line-or-region (&optional step)
(interactive ())
(let ((proc (get-process "shell"))
pbuf min max command)
(unless proc
(let ((currbuff (current-buffer)))
(shell)
(switch-to-buffer currbuff)
(setq proc (get-process "shell"))
))
(setq pbuff (process-buffer proc))
(if (use-region-p)
(setq min (region-beginning)
max (region-end))
(setq min (point-at-bol)
max (point-at-eol)))
(setq command (concat (buffer-substring min max) "\n"))
(with-current-buffer pbuff
(goto-char (process-mark proc))
(insert command)
(move-marker (process-mark proc) (point))
) ;;pop-to-buffer does not work with save-current-buffer -- bug?
(process-send-string proc command)
(display-buffer (process-buffer proc) t)
(when step
(goto-char max)
(next-line))
))
(defun sh-send-line-or-region-and-step ()
(interactive)
(sh-send-line-or-region t))
(defun sh-switch-to-process-buffer ()
(interactive)
(pop-to-buffer (process-buffer (get-process "shell")) t))
;(define-key sh-mode-map [(control ?j)] 'sh-send-line-or-region-and-step)
;(define-key sh-mode-map [(control ?c) (control ?z)] 'sh-switch-to-process-buffer)
;; (defun comment-dwim-eclipse ()
;; (interactive)
;; (let ((start (line-beginning-position))
;; (end (line-end-position)))
;; (when (region-active-p)
;; (setq start (save-excursion
;; (goto-char (region-beginning))
;; (beginning-of-line)
;; (point))
;; end (save-excursion
;; (goto-char (region-end))
;; (end-of-line)
;; (point))))
;; (comment-or-uncomment-region start end)))
;; (global-set-key "\M-;" 'comment-eclipse)
;;begin ES6 stuff from http://codewinds.com/blog/2015-04-02-emacs-flycheck-eslint-jsx.html
;; http://www.flycheck.org/manual/latest/index.html
;; (require 'flycheck)
;; ;; turn on flychecking globally
;; (add-hook 'after-init-hook #'global-flycheck-mode)
;; ;; disable jshint since we prefer eslint checking
;; (setq-default flycheck-disabled-checkers
;; (append flycheck-disabled-checkers
;; '(javascript-jshint)))
;; ;; use eslint with web-mode for jsx files
;; (flycheck-add-mode 'javascript-eslint 'web-mode)
;; ;; customize flycheck temp file prefix
;; (setq-default flycheck-temp-prefix ".flycheck")
;; ;; disable json-jsonlist checking for json files
;; (setq-default flycheck-disabled-checkers
;; (append flycheck-disabled-checkers
;; '(json-jsonlist)))
;;end ES6 stuff
;;customize-themes
(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.
'(custom-enabled-themes (quote (wombat)))
'(inhibit-startup-screen t)
'(initial-scratch-message "")
'(package-selected-packages
(quote
(multiple-cursors emamux web-mode use-package smex scala-mode2 projectile paredit neotree markdown-preview-mode magit json-mode ido-ubiquitous idle-highlight-mode flycheck find-file-in-project expand-region ensime dumb-jump company-tern coffee-mode cider better-defaults ag))))
(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.
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment