DarkMatter in Cyberspace
  • Home
  • Categories
  • Tags
  • Archives

Use vim as Clojure Development Environment


REPL in vim

  1. Install leiningen 2.x;

    verify: lein repl produces something like "nREPL server started on port...";

  2. Add [cider/cider-nrepl "0.12.0"] into ~/.lein/profiles.clj, Now ~/.lein/profiles.clj is: {:user {:plugins [ [lein-try "0.4.3"] [cider/cider-nrepl "0.12.0"] ] } }

  3. Install clojure plugins with vundle: add the following into ~/.vimrc:

    Plugin 'tpope/vim-fireplace'
    Plugin 'tpope/vim-classpath'
    Plugin 'guns/vim-clojure-static'
    
  4. Create a new clojure project: lein new myproj;

  5. Run "lein repl" under myproj (to download necessary jar files);

  6. edit a clojure file (xxx.clj) with vi, using ":Eval" to evaluate the expression under cursor, using ":%Eval" to evaluate current namespace;

  7. K to show doc of the function under cursor, [d show source codes;

  8. [<C-D> (press [, followed by Ctrl-d) jump to source code of the function under cursor, Ctrl-o to jump back.

Note: the clj file must be one of the source files (under "src" folder) of the clojure project created by leiningen. And the first line of this file (a clojure module actually) must be (ns project-name.module-name);

Ref:

CIDER nREPL

http://clojure-doc.org/articles/tutorials/vim_fireplace.html

http://www.boxuk.com/blog/unboxing-vim-fireplace/

Define Shortcuts for Efficiency

Add the following codes into .vimrc (before "filetype on");

autocmd FileType clojure nnoremap <buffer> <F5> :Eval<CR>
autocmd FileType clojure nnoremap <buffer> <F6> :%Eval<CR>

Explanation: "autocmd " means when happens, run . Here is "FileType clojure", is "nnoremap :Eval". The options here is "buffer local map", which means this keymapping only take effect on the current buffer. See Buffer-Local Options and Mappings in Learn Vimscript the Hard Way for explanations.

To determine FileType name (here is clojure), you should open a target file (here is any file with ".clj" extension) in vi, then run ":set filetype".

Discussion

Install clojure plugins with pathogen:

cd ~/.vim/bundle
git clone git://github.com/tpope/vim-fireplace.git 
git clone git://github.com/tpope/vim-classpath.git 
git clone git://github.com/guns/vim-clojure-static.git

S-expression Text Manipulation

Add the following into ~/.vimrc:

let maplocalleader = ","
Plugin 'tpope/vim-repeat'
Plugin 'guns/vim-sexp'
Plugin 'tpope/vim-surround'
Plugin 'tpope/vim-sexp-mappings-for-regular-people'

See Definitions in vim-sexp for the definitions of FORM and ELEMENT.

Moving

( and ) move the cursor to the nearest paired structural bracket. [[ and ]] move the cursor to previous/next top-level ELEMENT.

Editing

Now you can use [ai][fFse] to manipulate s-exp style text blocks, can be [d]elete, [c]hange, [v]isual, [y]ank, "f" means current form, "F" means top-level FORM. "s" means string, "e" means ELEMENT. For example: daf, dif, daF, diF, das, dae, vaf, vif, caf, cif.

Meta key used in key definitions of "guns/vim-sexp" doesn't work on Ubuntu laptop, use "tpope/vim-sexp-mappings-for-regular-people" instead:

>f/<f/>e/<e swap current FORM/ELEMENT with the next/previous FORM. For example, when cursor in (str "baz" "bar"), <f convert (foo (str "baz" "bar")) to ((str "baz" "bar") foo). When cursor on "foo", use <e to move back.

To demonstrate "Slurpage", say there is a code snippet (foo "bar"), we need convert it to (foo (str "baz" "bar")), here underscore is used as the cursor:

(foo_ "bar")

Insert "(baz)" and press , move cursor to ")":

(foo (str "baz") "bar")

Press >) to include "bar" in the current FORM:

(foo (str "baz" "bar"))

Use <) to move back.



Published

Sep 22, 2013

Last Updated

Sep 22, 2013

Category

Tech

Tags

  • clojure 26
  • fireplace 1
  • Vim 92

Contact

  • Powered by Pelican. Theme: Elegant by Talha Mansoor