scala

https://www.scala-lang.org/download/

https://www.douban.com/note/133904961/?type=like

emacs下scala开发环境配置: yasnippet, scala-mode, ensime

itang 古拉山 itang 古拉山 2011-02-10 18:01:20

准备家伙
 
    $ cd ~/.emacs.d/
    $ wget http://yasnippet.googlecode.com/files/yasnippet-0.6.1c.tar.bz2
    $ tar -xjvf yasnippet-0.6.1c.tar.bz2
    $ ln -s yasnippet-0.6.1c yasnippet
    
    $ cp -r $SCALA_HOME/misc/scala-tool-support/emacs ~/.emacs.d/scala-mode

    $ wget https://github.com/downloads/aemoncannon/ensime/ensime_2.8.1-0.4.2.tar.gz
    $ tar -zxvf ensime_2.8.1-0.4.2.tar.gz
    $ ln -s ensime_2.8.1-0.4.2 ensime



配置.emacs

    $ emacs .emacs
    ;;;;添加如下代码:
    ;; yasnippet
    (add-to-list 'load-path
                  "~/.emacs.d/yasnippet")
    (require 'yasnippet) ;; not yasnippet-bundle
    (yas/initialize)
    (yas/load-directory "~/.emacs.d/yasnippet/snippets")
    
    ;; scala-mode
    (add-to-list 'load-path "~/.emacs.d/scala-mode")
    (require 'scala-mode-auto) ;; 编辑.scala文件时自动启动scala mode
    
    ;; scala yasnippet
    (setq yas/my-directory "~/.emacs.d/scala-mode/contrib/yasnippet/snippets")
    (yas/load-directory yas/my-directory)
    (add-hook 'scala-mode-hook
                '(lambda ()
                   (yas/minor-mode-on)
                   ))
                   
    ;; scala-ensime
    (add-to-list 'load-path "~/.emacs.d/ensime/elisp")
    (require 'ensime)
    (add-hook 'scala-mode-hook 'ensime-scala-mode-hook) ;; scala mode下自动启动ensime
http://www.rabbitonweb.com/2016/01/31/my-emacs-for-scala-development-part-1/

My Emacs For Scala Development – Part 1

 January 31, 2016  rabbit  EmacsScala

After years of using Intelij IDEA both as Java and then Scala developer, I’ve finally said enough is enough. I could somehow live with the fact that with each version dramatically changes the way you configure the Scala plugin. It was pain (yet bearable) that majority of features I was so accustomed to as Java developer, were simply not represented in the Scala experience. But the thing that crossed the line was it’s slowness. Horrifying, horrendous slowness! I stopped running tests in IDE as SBT was way faster (by the time the tested passed in SBT, Intelij was still compiling sources). Code completion did not work (or worked with 5 seconds latency). I could go on with examples, but the bottom line is that Intelij became enormous, gigantic process that consumed 1 GB of RAM to provide features of a sluggish, leisurely, languid text editor.

That’s when I discovered Emacs. Yeah, I know, it took me a while. But better late then never. I was hoping to get fast text editor with colour highlighting, but I got much much more richer tool. Today I can not think of not using Emacs for any kind of development: Scala, Haskell, Erlang – you name it. Once you are sucked in, you are there forever.

Emacs is all about configurability. However it took me a few weeks to get things the way I want them to be. In the following posts I plan to document all that I’ve learned so far.

Warm up

Installing Emacs is as simple as typing `sudo apt-get install emacs` (Ubuntu) or `brew install emcas` (Mac). There are some issues with running Emacs on Mac from Application launcher (issues that I have not yet resolved), thus for now what I do is I run Emacs from terminal (Cmd-Space -> Terminal -> `emacs &`).

When we run Emacs for the first time, it does not look much impressive.

emacs_new

Before we make any changes to it, take a look at the link provided on the front page called “Emacs Tutorial”. If you are as new to Emacs as I was, invest your time in reading this tutorial. Follow all examples provided as you read it. This tutorial is only 200 lines long and it literally take less then an hour to finish it. It gives all needed foundations. Rest of this post assumes you’ve read the tutorial and that you understand what `M-x` or `C-x C-f` mean.

Things you should know

My guts are telling me that you’ve just completely ignored what I wrote in a paragraph above.You did not familiarise yourself with ‘Emacs Tutorial’ and just continued reading. Haven’t you?  I still encourage you to check that tutorial, but if you want to follow this post now, you must at least understand  some naming conventions and understand what special keys are used for.

Nomenclature

Emacs has a pluggable architecture. All of its features are implemented as plugins and you can extend its functionality by adding additional plugins to it. The only difference is they are not called plugins but modes. When you open a new file, in Emacs we say that we’ve opened a buffer (buffer does not have to necessary be a file but in most cases it is). What we now call a frame, Emacs calls a window. What we call a window nowadays, Emacs calls a frame.

Wow, that’s confusing right? The reason is simple, Emacs was developed long time ago (first versions was crafted in late 70s). It’s so old that by the time it was developed, names that we now use and take for granted, were not even invented yet. But don’t worry, it’s not really that confusing (once you get used to it ? ).

Control, Meta and Super

When we use Emcas, we tend to use a number of key shortcuts to activate specific features. For example to open new file you first press `Ctrl` and then (while still pressing `Ctrl`) press `x` followed by `f`. In all Emacs documentation / blogs / books / tutorials around the web this sequence is represented as `C-x C-f`. Other example could be for instance function `split-window-right`, which is activated by `C-x 2` (you first press together Ctrl and `x` followed by `2`).

Because control key was not enough (with so many features available) other keys are used as well. Most common ones will be : `meta` and `super`. “Now wait a minute” you might say “I have no meta or super on my keyboard!”. Well, you are right. Inventors of Emacs in the late 70s, early 80s were using keyboards like the one below.

emacs_keyboard

Freaky, ain’t it? Fear not, fortunately you can still use Emacs with your modern keyboard. Just remember, whenever you see `Meta` think `Alt`, whenever `super` is mentioned think `Cmd` (on Mac) or `windows button` (on Linux or Windows).

So if I tell you that to copy a text you must `M-w` and to paste it you have to `C-y`, you now know that the sequence is Alt with `w` followed by Ctrl with `y`.

All about look and feel

Ok. So far we’ve established that some Emacs conventions are weird but you keep on to my promise that one can get used to it. Now, let’s move to UI. UI looks bizzare, let’s do something about it. Following paragraph is what I call “Stripping Emacs”.

Init file

All configuration is done in file called `init.el`. That file will live in folder `~/emacs.d`. We don’t have neither file nor folder, so let’s create them

> mkdir emacs.d
> cd emacs.d/
> touch init.el

Stripping

Ok, having `~/.emacs.d/init.el` created it’s time to edit it. Open Emacs (if you haven’t done that already) and `C-x C-f` to open the init.el. File should be empty (since we’ve just created it a moment ago). Paste the following into it (to paste use `C-y`):

 
  1. (tool-bar-mode 0)
  2. (menu-bar-mode 0)
  3. (toggle-frame-fullscreen)
  4. (scroll-bar-mode 0)
  5. (fset `yes-or-no-p `y-or-n-p)

Little insight of what’s going on:
1. we’re removing tool bar – it’s ugly, it’s big, it’s redundant as all it’s features you will know by heart using key shortcuts
2. we’re removing menu bar – you don’t need it, trust me, see the above, more space for code
3. we’re entering full screen mode – while you code you don’t need any distractions, do you
4. we’re removing scrollbars – no mouse usage => no scrollbars needed = more space for code
5. this will let us answer all questions asked by Emacs with simple `y` or `n`

Themes

Now when we have our Emacs naked it’s time to put some make up on. But before we do, we need to learn few things about installing packages (modes, themes etc.) in Emacs.

Modes

Emacs since version 24.x has a built-in mechanism for installing modes from external repositories. First we need to add some repositories to our configuration file (init.el). We will add the three most commonly used: melpa, marmalade and gnu. Paste the following (best at the beginning of the file):

 
  1. (require 'package)
  2. (add-to-list 'package-archives '("melpa" . "http://melpa.milkbox.net/packages/") t)
  3. (add-to-list 'package-archives '("marmalade" . "http://marmalade.ferrier.me.uk/packages/"))
  4. (add-to-list 'package-archives '("gnu" . "http://elpa.gnu.org/packages/"))
  5. (package-initialize)

To list all available modes:

  1. `M-x` to execute command
  2. type `list-packages` followed by RET

You should see something similar to:

list-packages

Number of modes that you can install to extend your Emacs is enormous! Browser through (best using `C-v` & M-v)

Monokai Theme

We will install my favorite theme: Monokai and set is as default

  1. `M-x list-packages`
  2. `C-s` to enter search mode
  3. type monokai-theme and hit RET to exit search mode
  4. having cursor on monokai-theme press key `i` to mark it as ‘to be installed’
  5. press `x` to execute installation

Now since we have it installed, we can use it (and make it load every time we run Emacs by default). In your `init.el` file place yet another line

 
  1. (load-theme 'monokai t)

Now all you need to do is either restart Emacs or reload init.el file. To do the latter just `M-x load-file` and then provide path to your init.el.

Our Emacs should now looks something like this:

emacs_part1_done

Still not impressed? Fear not. This only ends Part 1 of Emacs series. More soon will come… Stay tuned!

emacs

  • David Barri

    Cool. I’ve never really used emacs and IDEA regularly wastes SO much of my time and effort. Looking forward to part 2; teach me!

  • Carlos Mendez

    you can try spacemacs which includes vim keybindings and it’s more simple to configure than vanilla emacs (just write scala in the package configuration and you get scala color and ensime autocomplete without need complex installations)

    I keep using intellij because I notice than for a medium size project ensime generates some files than consume a lot of space, nearly 500 mb of disk or even 1 gb..also it’s a bit buggy and fail sometimes, I think than typesafe must contribute to ensime even over eclipse, go is a good demostration of how an editor can be a better tool and replacement for a full ide

    • Paweł Szulc

      I’ve seen spacemacs and it’s source of inspiration, but I tend to go with my own customizations.
      And I prefere vanilia Emacs keybindings upgraded with my own using key-chord mode and hydra-mode

  • Leszek Gruchała

    Can I ask why not Sublime or Atom?

    • Paweł Szulc

      Three reasons:
      1. I’ve started using it because one of the best hackers I know personally are using it
      2. I’ve fell in love with the enormous amount of extensions that you can add to it; the customizability; ability to make it “your Emacs”
      3. I’m x2 much more productive with it then with any other tool I’ve used before

  • greg nwosu

    the only thing i intellij does better than ensime is search sources, M-x rgrep is good, but intellij indexes the source in memory so lookups a lightening fast

    • Paweł Szulc

      There is an awesome mode for that helm-do-ag which combines helm and ag (installed as external program). I will be describing it in my follow blog posts

      • Mateusz Górski

        But it still doesn’t “understand” the code.

        • Paweł Szulc

          To “understand” the code there is an awesome Ensime mode (please be patient, more parts of this series are coming!)

          For “grep” however why would it need understand the code? Grep is a grep (no matter how awful that sounds ? )

          • Mateusz Górski

            I am familiar with it, had been using Spacemacs with Ensime for ~4-5 months, still came back to Intellij.

            As for `grep` — it depends on the size of the project. If there’s multiple modules where names can repeat then having a tool that differentiate between those is enormous help.

  • Maxim Novak

    To install on mac –
    $ brew install emacs –with-cocoa

    $ brew linkapps emacs

    • Adam Warski

      Or http://emacsformacosx.com if the above fails with weird gcc/xcode errors ?
      Also note that when using the above it writes some config to `~/.emacs`, which has precendence over `~/.emacs.d/init.el`.

      Waiting for part 2 ?

  • Francisco

    Looking forward to the next post. Part 2 coming soon?

  • Radon Rosborough

    Just FYI, you should install Emacs on OS X with

    $ brew cask install emacs

    these days. Installing the regular formula –with-cocoa will also work. Anything with linkapps is obsolete.

Post navigation

 99 Scala Problems Challenge – 2/99

My Emacs For Scala Development – Part 2 

Search for:

CATEGORIES

ARCHIVES

RECENT COMMENTS

MY RECENT TALKS

  • Apache Spark 101 at London Scala User Group

  • Apache Spark 101 at Lambda Days 2014

  1. 1
  2. 2

POPULAR POSTS

My Emacs For Scala Development – Part 131 Jan , 2016

99 Scala Problems Challenge – 2/9928 Aug , 2014

Anticonference25 Feb , 2016

My Emacs For Scala Development – Part 211 Apr , 2016

99 Scala Problems Challenge – 1/9923 Aug , 2014

TAGS

99-problems apache spark chamberconf conference emacs functional programming option pattern matching scala tailrec talks

FOLLOW US

https://www.tuicool.com/articles/e6z6niu

Emacs Power for Scala Development: Introduction

时间 2013-06-08 22:22:09  Dzone

原文  http://jglobal.com/emacs-power-for-scala-development/

主题 Emacs Scala

hal

I’ve blogged before about using Emacs for Scala , but I’ve recently upgraded my Emacs-fu a bit further, and wanted to share the latest state of all the power of Emacs available for Scala development (along with a few loosely related power tools).

Emacs is an entirely incredible editor. I’ve had a long and sometimes fairly distant relationship with Emacs over the years.

For a long time I was strictly a Vim guy, finding in it’s conciseness and simplicity all the power I needed. It’s still my goto tool for a quick-and-dirty edit, and is, I know, capable of much more than I do with it.

I had used Emacs, but found it too much for me in most cases. I still pulled it out once in a while, but it wasn’t until I started doing more Lisp work (and a bit of Prolog) that I really got it. Emacs isn’t just an editor at all — it’s practically an operating system, with it’s own programming language (eLisp) to boot!

It’s great power comes from two things, in my view: one is that it’s a keyboard editor, e.g. you can do everything emacs does entirely with the keyboard, no mouse required.

I’m no mouse luddite (actually, I prefer trackpads and trackballs), but I do fully recognize that for sheer editing efficiency, there’s nothing so fast as they keyboard, especially if you’re using “normal” keys (e.g. the typewriter set, not a bunch of extra F-keys that might not be on all keyboards). The difference is not small, it’s huge. You can literally work many times faster with just the keyboard — this is covered extensively elsewhere on the web, have a look around.

I’ve also blogged about why I prefer Scala as my go-to language, so I’ll just refer you to my other posts if you’re interested. Suffice it to say I’ve tried quite a few languages, and Scala blows the doors off the lot of them for just plain practicality.

So, how do we put the power of Emacs with the practicality of Scala?

You can start with nothing, actually. Straight Emacs, right out of the box, makes an awesome Scala editor, as Scala files are just plain text after all, and plain text is Emacs’ middle name.

You can take it much further, however, starting with Scala Mode, which gives you basic syntax highlighting, making it a bit faster and easier to read your Scala source.

Beyond that are tools to help navigate and refactor. My favorite of these is Ensime, as I’ve discussed before.

The latest version (I’m using 0.9.8.9 with Scala 2.10) is getting pretty darn good. By this I mean it’s giving my other go-to tool for Scala development, Jetbrains’ IntelliJ IDEA , a run for it’s money.

Not in the ”pretty” department – that’s not what Emacs does. Just in the plain capability mode. It was always miles ahead in terms of speed — the latest version 13 early-access of IntelliJ is much better speed-wise than it used to be, but Emacs still runs circles around it. The reason is simple: Emacs is trying to do far less than IntelliJ is — it’s not an IDE, per se, just an editor.

Even running Ensime, though, which gives it most of the support of a true Scala IDE, it’s still much faster in just sheer editing, and about the same on doing various refactors, finding imports, and so forth.

It’s been said that Emacs is like a light-saber. You don’t just go to the store and buy one, you build your own. I’ve been doing just that, refining my Emacs configuration to do exactly what I want. It’s almost unfair to call what Emacs does “configuration”, though — it’s much more. I have a series of a dozen or so entire packages of elisp code in my .emacs.d directory ( it’s all on github, right here , if you want to have a look).

This not only set up my emacs with Ensime, Scala mode, and all the goodness I need for banging out Scala fast and accurate, it also has tools for helping with file navigation, beyond what Ensime does already for navigating Scala source.

My favorite at the moment is dirtree, which gives me a simple tree view of selected directories, and lets me whip through them effortlessly, all with just the keyboard. Sunrise commander is another much more powerful alternative — I’ve got both configured, and I jump to Sunrise when I need to really work with directories more than just navigate around quickly. Either of these are worth of their own blog posts, and may well get them in the future.

On the right is a screenshot of Dirtree, with a Scala file (using Ensime and Scala mode) open in the other buffer.

dirtree 300x162 Emacs Power for Scala Development: Introduction

A screenshot of Dirtree in Emacs with Scala Mode and Ensime

You can see at the bottom of the screen that I’m on the master branch for my git repo, and that Ensime is aware I’m editing a Scala file. If any lines have an error, I see them immediately. For example, kill an import statement, and the resulting error is highlighted (and the error itself, an unknown type, shown in the minibuffer at the bottom of the screen):

witherror 300x162 Emacs Power for Scala Development: Introduction

An error highlighted in Ensime after deleting an import

Then I can hit C-c C-r t (this is easier than it looks, I just hold down Control, hit c, then r, release control, then hit t. It takes way less time to do it than describe it). I get a popup of suggested imports, like so:

importpopup 300x162 Emacs Power for Scala Development: Introduction

Import popup in Ensime in Emacs for Scala

The first one is the right one, so I just hit enter, but there’s myriad ways to navigate to the right one if it’s not the top of the list. When I hit enter, my import is back, and my error goes away. It’s very, very fast.

One thing IntelliJ had was the ability to quickly and easily move around lines or blocks of Scala code, so I added move-text, a package that I’ve bound to Meta-Up, Meta-Down, allowing the same quick and easy re-organization of lines of code without having to cut and paste.

For git access, I can drop to the command-line of course, and often do (I use Zsh, which has excellent integrated git command-line tool support), or I can do that from — you guessed it — Emacs. I use Magit, which gets me quick and easy access to git right from within Emacs.

If i need to track my time (if I’m developing for a client, for example), I can stay right in Emacs and use the amazing Org-mode , from organizing the tasks in my project, though tracking my time, all the way up to preparing my time report for billing.

One of the benefits of doing everything in Emacs is that I can use a common set of keybindings (which determine what keys do what in the various Emacs modes). This means once my fingers learn what they’re doing, I stop thinking about keyboard shortcuts and just burn out the work. It becomes unconscious after a while, I don’t think “oh, I need to get to the end of the line”, I’ve already hit C-e by the time the thought starts to register. My fingers don’t leave the home row for minutes at a time, and the peck-peck… mouse… peck-peck… mouse drudgery just stops. Emacs is fast enough to not break my flow of coding, so put it all together and I can get more done in an hour than I could have in three with a different toolset.

I’m working on a series of posts to show all of this in detail, from the setup of your own Emacs through the cloning of a Scala project, through planning a series of edits to the project with Org-mode, and tracking items to do and time spent on them, all the way through refactoring, running tests, pushing changes to github, to generating my time to send a bill, all in “real-time”.

In the meantime, here’s a short video showing me correcting an error in Emacs/Ensime, as I describe above.

 

 收藏  纠错

推荐文章

我来评几句

登录后评论

已发表评论数(0)

相关站点

Dzone

+订阅

热门文章

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值