用Emacs作为clojure编辑器

 
 

之所以用Emacs作为clojure编辑器是因为其提供了Clojure的REPL紧密集成,这可以让你立刻运行你刚写完的代码。那种紧密的反馈回路在你日常学习和使用clojure写实时程序时会非常有用,Emacs非常适合和任何Lips方言一起工作,事实上Emacs本身就是用一种叫Emacs Lisp (elisp)的方言写成的

下载安装emacs

你需要使用最新版本的Emacs,Emacs 24,作为你的工作平台

在此说明我用的是OS X安装,并没有采用直接下载上面所给链接的Emacs,而是brew安装的,很简单,在这里就不多介绍了。

当你下载并安装完毕后,打开Emacs,你可以看到像这样的界面

配置

这里已经有了写好的配置文件 https://github.com/flyingmachine/emacs-for-clojure 按照下面的步骤删除你以前的Emacs配置,然后安装一个clojure友好的配置

1.关闭Emacs.

2.如果~/.emacs~/.emacs.d存在的话,删除它们

3.从上面给出的链接下载Emacs配置文件的Zip压缩包并解压,执行mv将解压出来的文件夹移动至~/目录下并取名.emacs.d

4.新建一个文件件~/.lein/profiles.clj将这行代码添加到文件里:

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

最后打开你的Emacs,当你打开Emacs时,你会看见像这样的界面,到这里基本的Emacsd的clojure编辑器就设置完毕了


使用

  • 启动 REPL

    为了让Emacs连接到REPL,你需要使用Emacs包:CIDER,如果你按照前面的步骤一直做下来你就已经安装好它了,不过你仍然可以通过M-x package-install [RET] cider [RET]命令来安装它。

    打开一个clojure文件,然后通过M-x cider-jack-in来启动REPL,这会创建一个你能与之交互的新Buffer,等待片刻你会看到这样的窗口


你可以在REPL中输入可求职的clojure表达式,它会返回表达式的值给你

  • 一些键绑定

Key Bindings for Navigating Text

KeysDescription
C-aMove to beginning of line
M-mMove to first non-whitespace character on the line
C-eMove to end of line
C-fMove forward one character
C-bMove backward one character
M-fMove forward one word (I use this a lot)
M-bMove backward one word (I use this a lot, too)
C-sRegex search for text in current buffer and move to it.Press C-s again to move to next match
C-rSame as C-s, but search in reverse
M-<Move to beginning of buffer
M->Move to end of buffer
M-g gGo to line

Clojure Buffer Key Bindings

KeysDescription
C-c M-nSwitch to namespace of current buffer.
C-x C-eEvaluate expression immediately preceding point
C-c C-kCompile current buffer
C-c C-d C-dDisplay documentation for symbol under point
M-. and M-,Navigate to source code for symbol under point and return to your original buffer.
C-c C-d C-aApropros search; find arbitrary text across function names and documentation

CIDER Buffer Key Bindings

KeysDescription
C-↑, C-↓Cycle through REPL history
C-enterClose parentheses and evaluate

Paredit Key Bindings

KeysDescription
M-x paredit-modeToggle paredit mode
M-(Surround expression after point in parentheses (paredit-wrap-round)
C-→Slurp; move closing parenthesis to the right to include next expression
C-←Barf; move closing parenthesis to the left to exclude last expression
C-M-f, C-M-bMove to the opening/closing parenthesis

说明:以上的clojure环境安装包括表格简要翻译自 http://www.braveclojure.com/basic-emacs/ ,基本只翻译了OS X环境下的安装。

一些设置

  • 关于字体大小的设置

进入Emacs后执行C-x C-f打开~/.emacs.d/customizations/ui.el文件找到

(set-face-attribute 'default nil :height 140)

这里设置的是14(140),可以修改其值来改变默认字体大小

  • 启动时窗口尺寸的设置

同样在ui.el里面找到

(setq initial-frame-alist '((top . 0) (left . 0) (width . 129) (height . 126))

设置widthheight的值到你满意为止

代码补全

关于代码补全在CIDER官方文档中有详细说明

1.安装company

M-x package-install [RET] company [RET]

2.在配置文件中添加

(global-company-mode)

使Emacs启动时就启动company-mode,当然也可以M-x company-mode方式启动

3.模糊补全
安装company-flx包,启动company-flx-mode,或者在配置文件中添加

(with-eval-after-load 'company
  (company-flx-mode +1))

在开启clojure REPL后即可使用,就像这样


具体使用参考其Github主页



作者:M6_ZeroG
链接:http://www.jianshu.com/p/11fdd9a59616
來源:简书
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值