Skip to content

Instantly share code, notes, and snippets.

@catharinejm
Last active August 29, 2015 14:21
Show Gist options
  • Save catharinejm/6daa01a3361abbb183f3 to your computer and use it in GitHub Desktop.
Save catharinejm/6daa01a3361abbb183f3 to your computer and use it in GitHub Desktop.
Clojure Setup

Getting Clojure set up

Leiningen

Leiningen is the de facto clojure project and package manager. It can be installed via homebrew on OS X

brew install leiningen

or just download the shell script and put it on your path. If ~/bin is on your path, then you would do:

curl https://raw.githubusercontent.com/technomancy/leiningen/stable/bin/lein > ~/bin/lein
chmod +x ~/bin/lein
lein

The final invocation of lein performs the full installation.

nREPL

Vim and Emacs integration use nREPL to run the Clojure REPL. Add the following to ~/.lein/profiles.clj to add the nrepl plugin to all leiningen projects

{:user {:plugins [[cider/cider-nrepl "LATEST"]]}}

Emacs Setup

I use Emacs primarily, and it probably has the best Clojure setup. But it can be a burden to learn a new editor and a new language at the same time, so don't feel you have to make the switch if you're not already an Emacs user.

Be sure you're using at least Emacs 24, and add the following ELPA respository:

(setq package-archives (cons '("melpa-stable" . "http://melpa-stable.milkbox.net/packages/")
                             package-archives))

Then install the following ELPA packages (M-x package-list-packages to bring up the listing):

clojure-mode
clojure-mode-extra-font-locking
cider
ac-cider

And I recommend adding the following hooks to your ~/.emacs or ~/.emacs.d/init.el file:

(require 'clojure-mode-extra-font-locking)
(eval-after-load 'clojure-mode
  '(progn
     (add-hook 'clojure-mode-hook (lambda ()
                                    (paredit-mode +1)
                                    (local-set-key (kbd "RET") 'newline-and-indent))))

(eval-after-load 'cider-repl-mode
  '(progn
     (add-hook 'cider-repl-mode-hook (lambda () (paredit-mode +1)))
     (add-hook 'cider-mode-hook 'cider-turn-on-eldoc-mode)
     (define-key cider-repl-mode-map (kbd "RET")
                                     (lambda ()
                                       (interactive)
                                       (if (eobp)
                                           (funcall 'cider-repl-return)
                                         (flet ((cider-repl--input-complete-p (&rest args) nil))
                                           (funcall 'cider-repl-return)))))))

Emacs help for clojure-mode and paredit-mode will contain the various keybindings, or just ask me.

Vim Setup

If you haven't already, install Tim Pope's vim-pathogen plugin

mkdir -p ~/.vim/autoload
curl https://raw.githubusercontent.com/tpope/vim-pathogen/master/autoload/pathogen.vim > ~/.vim/autoload/pathogen.vim

And add the following to ~/.vimrc

execute pathogen#infect()

Now install vim-fireplace and paredit.vim

mkdir -p ~/.vim/bundle
git clone https://github.com/tpope/vim-fireplace ~/.vim/bundle/vim-fireplace
git clone https://github.com/vim-scripts/paredit.vim ~/.vim/bundle/paredit.vim

Many paredit commands use the <Leader> key and bind with noremap, so if you have remapped yours (or would like to) be sure to put pathogen#infect() call after the remapping. For example, I like to use \ as my <Leader> and <LocalLeader>:

let mapleader = "\\"
let maplocalleader = "\\"

execute pathogen#infect()

If you don't remap, it uses , by default.

After you run :Helptags (capital H is significant!) then you can run :help fireplace and :help paredit to get keybindings and other info.

Sublime Text Setup

Install Package Control. Open the console (ctrl+` ).

In Sublime Text 2, paste and run:

import urllib2,os,hashlib; h = 'eb2297e1a458f27d836c04bb0cbaf282' + 'd0e7a3098092775ccb37ca9d6b2e4b7d'; pf = 'Package Control.sublime-package'; ipp = sublime.installed_packages_path(); os.makedirs( ipp ) if not os.path.exists(ipp) else None; urllib2.install_opener( urllib2.build_opener( urllib2.ProxyHandler()) ); by = urllib2.urlopen( 'http://packagecontrol.io/' + pf.replace(' ', '%20')).read(); dh = hashlib.sha256(by).hexdigest(); open( os.path.join( ipp, pf), 'wb' ).write(by) if dh == h else None; print('Error validating download (got %s instead of %s), please try manual install' % (dh, h) if dh != h else 'Please restart Sublime Text to finish installation')

In Sublime Text 3, paste and run:

import urllib.request,os,hashlib; h = 'eb2297e1a458f27d836c04bb0cbaf282' + 'd0e7a3098092775ccb37ca9d6b2e4b7d'; pf = 'Package Control.sublime-package'; ipp = sublime.installed_packages_path(); urllib.request.install_opener( urllib.request.build_opener( urllib.request.ProxyHandler()) ); by = urllib.request.urlopen( 'http://packagecontrol.io/' + pf.replace(' ', '%20')).read(); dh = hashlib.sha256(by).hexdigest(); print('Error validating download (got %s instead of %s), please try manual install' % (dh, h)) if dh != h else open(os.path.join( ipp, pf), 'wb' ).write(by)

Then press cmd+shift+p and run Package Control: Install Package and install SublimeREPL and paredit

Add the following to your user preferences (in menu Sublime Text > Preferences > Settings - User)

"auto_match_enabled": false

Note that that will disable quote and paren/bracket matching globally, but it's necessary for proper Clojure+Paredit usage. I don't know if there is a way to toggle that setting on a per-file or per-project basis.

You can find SublimeREPL docs here and paredit docs here

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