Open emacs24;html
Change CWD to parent folder of project home: M-x cd
Build a leiningen project: M-! lein new calc-cov (see Run Shell Command in Emacs for other method to run shell command in emacs);github
Change CWD to project home: M-x cd
Start nrepl server: M-x nrepl-jack-in;express
Open source file in another window: C-x 2 C-x C-f src/.../core.clj;vim
Change the namespace of the REPL to the namespace of the file you are in: C-c M-n;windows
Evaluate the whole source file: C-c C-k, evaluate a s-exp before the cursor: C-x C-e;flex
Open clojure doc of the function under the cursor: C-c C-dui
Run test: C-c C-,this
Toggle focus between nrepl window and clojure source file window: C-c C-z (you should add "(add-to-list 'same-window-buffer-names "nrepl")" to ~/.emacs;
Always pretty printing in the REPL: M-x nrepl-toggle-pretty-printing
Move over sexp: C-M-b/C-M-f;
Interrupt any pending evaluations: C-c C-b;
Add this sexp into source file (unnecessary to save source file);
Use following methods to evaluate the sexp:
Move cursor to the sexp for test, C-M-x (or C-c C-c) to evaluate the top-level form (such as a defn expression) under the cursor; If you want insert the evaluation result to current buffer, use C-u C-M-x;
If the the target form is not a "top-level" form, move the cursor to the end of this sexp, C-x C-e to evaluate it, See more shortcuts at nrepl official site;
Then this sexp has been added to the repl, you can use it in repl;
This is very helpful in some circumstances. For example, I want debug function "cell-block" (see p141 in "Clojure Programming"), add
(println "[left mid right] is:") (println [left mid right])
before "(window (map ...)". But You can't evaluate the whole file because there are some lazy sequences and evaluate whole file will cause JVM heap out of memory; So I only evaluate this modified version with C-x C-e, test it in repl. After I figure out how this function works, undo my modification and move on.
Note:
In clojure mode with Evil, you have to switch to insert state and put the cursor after the last parenthsis, then evaluate the expression with C-x C-e. If you put the cursor on the last parenthesis in normal state, The result will be wrong.
It unnecessary to put your clojure file in a leiningen-created project, which is more flexible than vim-fireplace;
The value of expression will be output to the message area (bottom of the emacs window), while the output by "println" will be output to nrepl buffer;
Switch cursor between windows: C-x o;
Clear repl output: C-c M-o;
Indent code blocks intelligently: C-M-q
All the keyboard shortcuts will be more convenient if you swap CapsLock key and Escape key, together with Ctrl key and Alt key, see Define Keymap on Ubuntu for detail;
Ref: http://stackoverflow.com/questions/3636364/can-i-clean-the-repl
Ref: Clojure with Emacs