使用 Vim 作为 Java IDE 的技巧?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/253170/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 12:02:41  来源:igfitidea点击:

Tips for using Vim as a Java IDE?

javavimide

提问by Allain Lalonde

I'm addicted to Vim, it's now my de facto way of editing text files.

我沉迷于 Vim,它现在是我编辑文本文件的事实上的方式。

Being that it's mainly a text editor and not an IDE, has anyone got tricks for me to make it easier when developing Java apps?

由于它主要是一个文本编辑器而不是一个 IDE,有没有人可以让我在开发 Java 应用程序时更容易一些技巧?

Some questions I have:

我有一些问题:

  • How do I invoke a maven task without leaving vi?
  • Can I get code completion?
  • How's the syntax highlighting?
  • 如何在不离开 vi 的情况下调用 Maven 任务?
  • 我可以获得代码完成吗?
  • 语法高亮如何?

Anything else (other than "Don't do it!") that I should know about?

还有什么我应该知道的(除了“不要这样做!”)?

采纳答案by toolkit

Some tips:

一些技巧:

  • Make sure you use vim (vi improved). Linux and some versions of UNIX symlink vi to vim.
  • You can get code completion with eclim
  • Or you can get vi functionality within Eclipse with viPlugin
  • Syntax highlighting is great with vim
  • Vim has good support for writing little macros like running ant/mavenbuilds
  • 确保使用 vim(vi 改进)。Linux 和某些版本的 UNIX 符号链接 vi 到 vim。
  • 您可以使用eclim完成代码
  • 或者您可以使用viPlugin在 Eclipse 中获得 vi 功能
  • vim 中的语法高亮很棒
  • Vim 对编写像运行 ant/ maven构建这样的小宏有很好的支持

Have fun :-)

玩得开心 :-)

回答by Berzemus

Use vim. ^-^ (gVim, to be precise)

使用vim。^-^(gVim,准确地说)

You'll have it all (with some plugins).

您将拥有一切(带有一些插件)。

Btw, snippetsEmuis a nice tool for coding with useful snippets (like in TextMate). You can use (or modify) a pre-made package or make your own.

顺便说一句,snippetsEmu是一个很好的工具,用于使用有用的片段(如在 TextMate 中)进行编码。您可以使用(或修改)预先制作的软件包或制作自己的软件包。

回答by ngn

  • How do I invoke a maven task without leaving vi?

    Maven is no different than any other shell command:

    :!mvn

    You can :set makeprg=mvnif you already have a favourite key mapping for :make.

  • Can I get code completion?

    Yes, eclimis great, a bridge between vim's editing efficiency and Eclipse's Java language-specific awareness.

    <C-n>and <C-p>are not-so-great, but amazingly helpful.

  • How's the syntax highlighting?

    More than good enough for a regex-based highligher.

  • 如何在不离开 vi 的情况下调用 Maven 任务?

    Maven 与任何其他 shell 命令没有什么不同:

    :!mvn

    你可以:set makeprg=mvn,如果你已经有了自己最喜欢的键映射:make

  • 我可以获得代码完成吗?

    是的,eclim很棒,是 vim 的编辑效率和 Eclipse 的 Java 特定语言意识之间的桥梁。

    <C-n><C-p>没有那么大,但令人惊讶的帮助。

  • 语法高亮如何?

    对于基于正则表达式的highlighter来说已经足够了。

You may want to consider tools for other vim+java purposes, like code templates (snippetEmu—default snippets suck, but customizability shines), searching for usages and going to declarations (eclim, grep, ctags), generating getters and setters (java_getset, or eclim), automatic imports (eclim). You might also need a java shell for quick experiments (the BeanShell, a.k.a. bsh).

您可能需要考虑用于其他 vim+java 目的的工具,例如代码模板(snippetEmu —默认代码段很烂,但可定制性出色)、搜索用法并转到声明(eclim、grep、ctags)、生成 getter 和 setter(java_getset、或 eclim)、自动导入 (eclim)。您可能还需要一个 java shell 来进行快速实验(即BeanShell,又名bsh)。

回答by ngn

I've been a Vim user for years. I'm starting to find myself starting up Eclipse occasionally (using the vi plugin, which, I have to say, has a variety of issues). The main reason is that Java builds take quite a while...and they are just getting slower and slower with the addition of highly componentized build-frameworks like maven. So validating your changes tends to take quite a while, which for me seems to often lead to stacking up a bunch of compile issues I have to resolve later, and filtering through the commit messages takes a while.

我多年来一直是 Vim 用户。我开始发现自己偶尔会启动 Eclipse(使用 vi 插件,我不得不说,它有很多问题)。主要原因是 Java 构建需要相当长的时间……而且随着添加高度组件化的构建框架(如 maven),它们变得越来越慢。因此验证您的更改往往需要相当长的时间,对我来说这似乎经常导致堆积一堆我必须稍后解决的编译问题,并且过滤提交消息需要一段时间。

When I get too big of a queue of compile issues, I fire up Eclipse. It lets me make cake-work of the changes. It's slow, brutal to use, and not nearly as nice of an editor as Vim is (I've been using Vim for nearly a decade, so it's second nature to me). I find for precision editing—needing to fix a specific bug, needing to refactor some specific bit of logic, or something else...I simply can't be as efficient at editing in Eclipse as I can in Vim.

当编译问题队列太大时,我启动 Eclipse。它让我可以轻松应对这些变化。它使用起来很慢,很残酷,而且不像 Vim 那样是一个很好的编辑器(我已经使用 Vim 将近十年了,所以它对我来说是第二天性)。我发现需要精确编辑——需要修复一个特定的错误,需要重构一些特定的逻辑,或者其他什么……我在 Eclipse 中编辑的效率不如在 Vim 中。

Also a tip:

还有一个提示:

:set path=**
:chdir your/project/root

This makes ^wfon a classname a very nice feature for navigating a large project.

这使得^wf类名成为导航大型项目的一个非常好的功能。

So anyway, the skinny is, when I need to add a lot of new code, Vim seems to slow me down simply due to the time spent chasing down compilation issues and similar stuff. When I need to find and edit specific sources, though, Eclipse feels like a sledge hammer. I'm still waiting for the magical IDE for Vim. There's been three major attempts I know of. There's a pure viml IDE-type plugin which adds a lot of features but seems impossible to use. There's eclim, which I've had a lot of trouble with. And there's a plugin for Eclipse which actually embeds Vim. The last one seems the most promising for real serious Java EE work, but it doesn't seem to work very well or really integrate all of Eclipse's features with the embedded Vim.

所以无论如何,瘦的是,当我需要添加大量新代码时,Vim 似乎只是因为花费时间来解决编译问题和类似问题而减慢了我的速度。但是,当我需要查找和编辑特定源时,Eclipse 感觉就像一把大锤。我还在等待 Vim 的神奇 IDE。我知道有三个主要的尝试。有一个纯 viml IDE 类型的插件,它添加了很多功能,但似乎无法使用。有 eclim,我遇到了很多麻烦。还有一个 Eclipse 插件,它实际上嵌入了 Vim。最后一个对于真正严肃的 Java EE 工作来说似乎是最有希望的,但它似乎并没有很好地工作,也没有真正将 Eclipse 的所有功能与嵌入式 Vim 集成。

Things like add a missing import with a keystroke, hilight code with typing issues, etc, seems to be invaluable from your IDE when working on a large Java project.

在处理大型 Java 项目时,诸如通过击键添加丢失的导入、突出显示键入问题的代码等,在您的 IDE 中似乎是无价的。

回答by Kevin

I found the following summary very useful: http://www.techrepublic.com/article/configure-vi-for-java-application-development/5054618. The description of :make was for ant not maven, but otherwise a nice summary.

我发现以下摘要非常有用:http: //www.techrepublic.com/article/configure-vi-for-java-application-development/5054618。:make 的描述是为 ant 而不是 maven 的,但除此之外,这是一个很好的总结。

回答by ikky

I have just uploaded this Vim pluginfor the development of Java Maven projects.

我刚刚上传了这个 Vim 插件,用于开发 Java Maven 项目。

And don't forget to set the highlighting if you haven't already:

如果您还没有设置突出显示,请不要忘记:

enter image description herehttps://github.com/sentientmachine/erics_vim_syntax_and_color_highlighting

在此处输入图片说明https://github.com/sentientmachine/erics_vim_syntax_and_color_highlighting

回答by critium

I know this is quite a few years later but here are some interesting plugins. I have not tried either of these yet so YMMV.

我知道这是几年后的事,但这里有一些有趣的插件。我还没有尝试过这些,所以 YMMV。

https://github.com/mikelue/vim-maven-plugin

https://github.com/mikelue/vim-maven-plugin

https://github.com/vim-scripts/maven-ide

https://github.com/vim-scripts/maven-ide

EDIT: Oh an BTW, i've tried eclim off and on, but the reason I like vim is its lightness. Executing eclipse even on headless mode is just too much mental lifting for me.

编辑:顺便说一句,我已经尝试过 eclim 断断续续的,但我喜欢 vim 的原因是它的轻盈。即使在无头模式下执行 eclipse 对我来说也太精神了。

EDIT2: I've been using playframework lately and this will probably work with maven builds too:

EDIT2:我最近一直在使用 playframework,这可能也适用于 maven 构建:

  • For compiling, you can configure VIM's make to run maven or in my case, run a build script, tee that to a file.

    autocmd Filetype java setl makeprg=play_compile
    autocmd Filetype java setl efm=%A\ %#[error]\ %f:%l:\ %m,%-Z\ %#[error]\ %p^,%-C%.%#
    
  • 对于编译,您可以配置 VIM 的 make 以运行 maven,或者在我的情况下,运行构建脚本,将其发送到文件中。

    autocmd Filetype java setl makeprg=play_compile
    autocmd Filetype java setl efm=%A\ %#[error]\ %f:%l:\ %m,%-Z\ %#[error]\ %p^,%-C%.%#
    

"play_compile" is just a compile script. It uses SBT so Maven should work just fine here. Even direct javac will work. This way, you can use VIM"s quickfix buffer (:cnext, :clist: cprev, etc).

“play_compile”只是一个编译脚本。它使用 SBT,因此 Maven 在这里应该可以正常工作。即使直接 javac 也能工作。这样,您就可以使用 VIM 的 quickfix 缓冲区(:cnext、:clist: cprev 等)。

  • For jumping around the classes, I use ctrl-p. Its beautiful. Use it. Faster than eclipse in jumping around files.

  • For jumping around methods, I use tagsearch with exuberant c-tags. Jump into method declarations by using ctrl-]. Go back using Ctrl-o. Doesnt work as good as eclipse, but it works good enough.

  • I use supertabfor code completion. Javacomplete is pretty slow, so I stick with omni-complete. Again, not as accurate as eclipse, but its fast and works good enough for me.

  • 为了跳过课程,我使用ctrl-p。真漂亮。用它。在跳转文件方面比 eclipse 快。

  • 为了跳过方法,我使用带有旺盛 c 标签的 tagsearch。使用 ctrl-] 跳转到方法声明。使用 Ctrl-o 返回。不像eclipse那么好用,但它足够好。

  • 我使用supertab来完成代码。Javacomplete 很慢,所以我坚持使用 omni-complete。同样,不如 eclipse 准确,但它的速度很快并且对我来说足够好。