Skip to content

Instantly share code, notes, and snippets.

@omaciel
Last active February 19, 2016 15:36
Show Gist options
  • Save omaciel/8103692 to your computer and use it in GitHub Desktop.
Save omaciel/8103692 to your computer and use it in GitHub Desktop.
Emacs configuration for Python development. Simply drop this file inside your $HOME/.emacs.d directory and enjoy it!
;;; package --- Emacs configuration for Python developement
;;; Commentary:
;;; Code:
;; Disable a couple of menus and startup message
(if (fboundp 'menu-bar-mode) (menu-bar-mode -1))
(if (fboundp 'tool-bar-mode) (tool-bar-mode -1))
(if (fboundp 'scroll-bar-mode) (scroll-bar-mode -1))
(setq inhibit-startup-message t)
(require 'package)
(add-to-list 'package-archives
'("marmalade" . "http://marmalade-repo.org/packages/") t)
(add-to-list 'package-archives
'("melpa" . "http://melpa.milkbox.net/packages/") t)
(package-initialize)
(when (not package-archive-contents)
(package-refresh-contents))
;; Add in your own as you wish:
(defvar my-packages '(ack-and-a-half
ample-zen-theme
auto-complete
evil-nerd-commenter
flymake
flymake-cursor
flymake-puppet
flymake-python-pyflakes
git-gutter
iedit
ido-ubiquitous
jedi
magit
markdown-mode
monokai-theme
multi-term
popup
projectile
pyflakes
pylint
rainbow-delimiters
rainbow-mode
smartparens
smooth-scrolling
starter-kit
starter-kit-bindings
starter-kit-lisp
yasnippet
virtualenv
whitespace-cleanup-mode)
"A list of packages to ensure are installed at launch.")
(dolist (p my-packages)
(when (not (package-installed-p p))
(package-install p)))
;; Theme selection
(load-theme 'ample-zen t)
;; (load-theme 'monokai t)
;; (load-theme 'solarized-dark)
;; Setup multi-term
(setq multi-term-program "/bin/zsh")
;; Setup evil-nerd-commenter
(evilnc-default-hotkeys)
;; Setup yasnippet
(add-to-list 'load-path
"~/.emacs.d/plugins/yasnippet")
(require 'yasnippet)
(yas-global-mode 1)
;; setup flymake
(require 'flymake-python-pyflakes)
(require 'flymake-cursor)
(setq flymake-python-pyflakes-executable "flake8")
;; Handles whitespace, tabs, etc
(setq-default show-trailing-whitespace t)
(add-hook 'before-save-hook 'delete-trailing-whitespace)
;; Projectile for git management
(projectile-global-mode)
(defalias 'ack 'ack-and-a-half)
;; Enable git-gutter
(global-git-gutter-mode t)
;; auto-complete
(autoload 'ac-config-default "auto-complete-config")
(ac-config-default)
(eval-after-load "auto-complete"
'(add-to-list 'ac-modes `python-mode))
;; Make sure to run pip install jedi epc flake8
;; Jedi settings
(add-hook 'python-mode-hook 'jedi:setup)
(setq jedi:complete-on-dot t)
;;; Smart parens
(smartparens-global-mode t)
;;; Python stuff
(add-hook 'python-mode-hook
(lambda ()
(require 'virtualenv)
(flymake-python-pyflakes-load)
(rainbow-delimiters-mode)
(auto-complete-mode)
(rainbow-delimiters-mode)
(whitespace-mode)
(whitespace-cleanup-mode)
;; Automatic pairing of delimeters
(define-key python-mode-map "\"" 'electric-pair)
(define-key python-mode-map "\'" 'electric-pair)
(define-key python-mode-map "(" 'electric-pair)
(define-key python-mode-map "[" 'electric-pair)
(define-key python-mode-map "{" 'electric-pair)
(define-key python-mode-map "\C-m" 'newline-and-indent)))
(defun electric-pair ()
"Insert character pair without surounding spaces"
(interactive)
(let (parens-require-spaces)
(insert-pair)))
;; Default tab and indentation behavior
(setq-default indent-tabs-mode nil)
(setq-default tab-width 4)
(defun python-add-breakpoint ()
"Add a break point"
(interactive)
(newline-and-indent)
(insert "import epdb; epdb.st()")
(hightlight-lines-matching-regexp "^[]*import epdb; epdb.st()"))
(global-set-key [f2] 'python-add-breakpoint)
(defun datestamp ()
(interactive)
(call-process "date" nil t nil "+%a %b %d %Z %Y"))
(global-set-key (kbd "<f6>") 'datestamp)
(setq magit-auto-revert-mode nil)
(global-set-key [f1] 'magit-status)
;; Handles sourcing ZSH
(let ((path (shell-command-to-string ". ~/.zshrc; echo -n $PATH")))
(setenv "PATH" path)
(setq exec-path
(append
(split-string-and-unquote path ":")
exec-path)))
;; Fixing a key binding bug in elpy
(define-key yas-minor-mode-map (kbd "C-c k") 'yas-expand)
(setenv "PYTHONPATH" "/home/omaciel/hacking:/home/omaciel/Dropbox/Hacking:/home/omaciel:/home/omaciel/hacking/katello-cli/src")

Emacs configuration for Python development. Simply drop this file in your $HOME directory as .emacs and enjoy it!

Requirements

Before you start Emacs for the first time, you will want to:

  • Make sure to install the following python libraries
pip install jedi epc flake8
@omaciel
Copy link
Author

omaciel commented Jan 15, 2014

Jan. 14th, 2014: updated init.el to include instructions for installing pymacs as well as some other changes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment