如何在vim中快速编译并运行scala代码?
我正在使用 vim 来学习 scala。我在 vim 中编写了一些 scala 代码,然后:
:!scalac xxx.scala
:!scala Xxx
有什么方法可以更轻松地运行代码吗?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
我正在使用 vim 来学习 scala。我在 vim 中编写了一些 scala 代码,然后:
:!scalac xxx.scala
:!scala Xxx
有什么方法可以更轻松地运行代码吗?
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
接受
或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
发布评论
评论(7)
我建议您尝试 SBT - http://code.google.com/p/simple- build-tool/
您可能需要打开一些 shell,并且必须在 vim 和 SBT 会话之间切换,但总的来说,SBT 简化了 Scala 开发,使其感觉像一种解释语言。一些方便的 SBT 技巧:
在项目目录中的 shell 中运行 sbt 以自动创建项目结构。要快速开始,请编辑该项目中名为 src/main/scala/MyFile.scala 的文件。
在一个 sbt shell 中,运行 ~compile 以在您进行更改时持续检测和编译更改。
在另一个 sbt shell 中,运行“console”。这将为您提供一个正常的 Scala REPL,其中包含您的项目及其依赖项,该依赖项已位于 CLASSPATH 上。
从该 scala shell 中,您可以运行 :load 来加载 Scala 文件并运行它。或者,您可以简单地导入并实例化类并调用 Scala 代码中定义的函数来测试功能。当您对文件进行更改时,REPL 中的 :replay 将加载这些更改并再次运行所有输入的命令。
此设置非常适合与 Scalatra 结合进行快速 Web 开发。从 sbt 中,您可以使用“jetty-run”直接部署和运行您的 web 应用程序,然后使用 ~prepare-webapp 自动检测、重新编译和部署更改。然后,您只需保持浏览器打开即可看到所做的更改。这里有一个很好的入门指南:http://blog.everythings-beta.com/ ?p=430 您可以在 5 分钟左右运行一个网络应用程序。
I suggest you try SBT - http://code.google.com/p/simple-build-tool/
You'll probably want a few shells open and you'll have to bounce between vim and your SBT session, but in general SBT streamlines Scala development to the point where it feels like an interpreted language. A few handy SBT tips:
Run sbt at the shell in your project directory to automatically create your project structure. To get started quickly, edit a file in that project named src/main/scala/MyFile.scala
In one sbt shell, run ~compile to continuously detect and compile changes as you make them.
In another sbt shell, run 'console'. This gives you a normal Scala REPL with your project and its dependencies already on the CLASSPATH.
From that scala shell you can run :load to load your Scala file and run it. Or you can simply import and instantiate classes and call functions defined in your Scala code to test functionality. When you make changes to the file, :replay in the REPL will load those changes and run all entered commands again.
This setup works great for rapid web development in conjunction with Scalatra. From sbt you can directly deploy and run your webapp with 'jetty-run' and then detect, recompile and deploy changes automatically with ~prepare-webapp. Then you just keep a browser open and see changes as you make them. A good getting started guide for this is here: http://blog.everythings-beta.com/?p=430 You can have a web app running in 5 minutes or so.
我建议您使用 sbt 在一个 shell 中运行所有东西在vim中修改scala文件。
您需要在项目文件中指定 mainClass 方法。详细信息请此处。
当您在 vim 会话中保存 scala 文件时,sbt 将尝试调用您的 mainClass。
I would suggest that you use sbt to run things in one shell & modify the scala file in vim.
You need to specify a mainClass method in your project file. Details here.
When you save the scala file in the vim session, sbt will try to invoke your mainClass.
您可能更适合使用构建管理器。然而,在某些情况下这很有用,特别是如果您只想使用 scala 解释器运行单个文件。在 vim 中,
%
匹配当前文件。如果您想使用 Scala 解释器中的当前文件来运行您的文件,您可以运行:如果您要经常这样做,您可以考虑将命令映射到此。
如果您希望此绑定仅在 scala 文件中可用,请尝试以下操作。这可能需要您拥有 scala vim 语法文件。
同样,要编译文件,您可以运行以下命令。
如果你想运行编译后的类,那就更困难了,因为你需要知道类路径。
%:r
可能是一个开始——它是当前文件减去扩展名。比上面更高级的解决方案是使用 scala 解释器/编译器作为使用
:make
的 vim 构建系统。这样你就可以使用 quickfix 来浏览 vim 中的错误。您会发现我编写的几个有用的编译器文件。您可以将它们复制到~/.vim/compiler/
中,然后使用 vim 设置编译器 (:compiler scalai
)。您可能会发现我的一篇博客文章很有用,因为它介绍了使用 vim 在 scala 中进行开发。它涵盖了在构建管理器中将 scala 与持续编译相结合的好方法。
You are probably better of using a build manager. However, there are cases where this is useful, particularly if you just want to run a single file with the scala interpreter. In vim,
%
matches the current file. If you want to run you file using the current file in the Scala interpreter, you could run:If you were going to do this a lot, you could consider mapping a command to this.
If you wanted this binding to be available only in scala files, try the following. This might require you to have the scala vim syntax files.
Similarly, to compile files you can run the following.
If you want to run your compiled class, it's more difficult because you need to know the classpath.
%:r
might be a start--it's the current file minus the extension.A more advanced solution than above would be to use the scala interpreter/compiler as the build system for vim using
:make
. This way you can navigate through the errors within vim using quickfix. You will find a couple of compiler files I wrote helpful. You can copy these to~/.vim/compiler/
and then set the compiler withing vim (:compiler scalai
).You may find a blog post of mine useful as it covers developing in scala using vim. It covers a good way of combining scala with continuous compilation in a build manager.
地图在这里很有帮助。我对 scala 一无所知,但例如命令
每当您在正常模式下按 F6 时,都会调用 shell 命令
echo test
我相信您可以根据您的需要进行调整。如果您希望它永久存在(您可能会这样做),请放入 .vimrc 。如果您对整个编辑器脚本感兴趣,可以在这里找到更多信息: http://www.ibm.com/developerworks/linux/library/l-vim-script-1/index.html
PS 忘了提及
% 字符在 shell 命令中扩展到当前文件。您可能想要类似
:! scala %
编辑:
这是一个比我乍一看更复杂的问题(我习惯使用解释语言,其中编译运行是单个命令)。无论如何,如果你确实仍然想要单键编译和运行功能,请尝试这个快速脚本(将其粘贴在
.vimrc
文件中的某个位置并重新启动 vim):Maps are helpful here. I know nothing about scala, but e.g. the command
Will call the shell command
echo test
whenever you hit F6 in normal modeI'm sure you can adapt it to your needs. Put in .vimrc if you want it permanent (which you probably do). More info can be found here, if you're into the whole editor scripting thing: http://www.ibm.com/developerworks/linux/library/l-vim-script-1/index.html
P.S. Forgot to mention that the
%
character expands to the current file in a shell command. You probably want something like:! scala %<CR>
EDIT:
This is a more complicated problem than I thought at first glance (I'm used to working with interpreted languages, where compile-run is a single command). Anyway, if you really still want single key compile and run functionality, try this quick script (stick it somewhere in your
.vimrc
file and restart vim):我自己从来没有离开过
make
,所以我可以使用:make °target°
编译程序干净简单。
I myself kind of never moved away from
make
so I can compile the program with:make °target°
Clean and simple.
您可以通过编写脚本将其缩短为一行吗?
所以在同一目录中,您可以执行类似
scalac xxx.scala 的 操作
scala xxx
以及 vim 中的它们,您可以这样做
:!./scriptName
确保首先使用 chmod 使其可执行
You could shorten it down to one line by writing a script?
so in the same directory, you could do something like
scalac xxx.scala
scala xxx
and them from vim you do
:!./scriptName
Make sure to make it executable first with chmod
按照已经建议的方式安装 SBT 后,请在 Vim 编辑器会话中考虑这一点,
此外,为了编译或包装,
Having installed SBT as already suggested, consider this from within a Vim editor session,
In addition, for compiling or packaging,