REPL in vim
-
Install leiningen 2.x;
verify:
lein repl
produces something like "nREPL server started on port..."; -
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"] ] } }
-
Install clojure plugins with vundle: add the following into ~/.vimrc:
Plugin 'tpope/vim-fireplace' Plugin 'tpope/vim-classpath' Plugin 'guns/vim-clojure-static'
-
Create a new clojure project:
lein new myproj
; -
Run "lein repl" under myproj (to download necessary jar files);
-
edit a clojure file (xxx.clj) with vi, using ":Eval" to evaluate the expression under cursor, using ":%Eval" to evaluate current namespace;
-
K
to show doc of the function under cursor,[d
show source codes; -
[<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:
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
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
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
(foo (str "baz") "bar")
Press >)
to include "bar" in the current FORM:
(foo (str "baz" "bar"))
Use <)
to move back.