search

Home  >  Q&A  >  body text

java - Vim可以直接运行代码吗

最近在尝试使用 Vim,有个问题请教,就

public class test {
    public static void main(String args[]) {
        System.out.print("shit,Test success!");
    }
}

在 Vim 中,我可以直接运行调试吗,像 Sublime 的 cmd+B 一样。

大家讲道理大家讲道理2892 days ago329

reply all(6)I'll reply

  • ringa_lee

    ringa_lee2017-04-18 09:08:46

    According to the meaning of the question, after editing the code, press a shortcut key to automatically compile and run it directly.

    Plan

    Execute external commands in vim中, 可通过:!cmd. Corresponding to the single file written by the subject:

    :!javac %  #=> 编译, % 为正在编辑的文件名
    :!java %:r  #=> 运行, %:r 去掉文件名的后缀

    This is the principle, just set the key mapping next. In the .vimrc file, add the following content

    autocmd BufNewFile,BufReadPre *.java nmap <leader>rn :!javac %<cr>:!java %:r<cr>

    means, create or read .java文件前, 自动映射快捷钕<leader>rn into the specified command.

    Of course you can also set it to two shortcut keys. It is not recommended to use it hereCommand快捷键, 而是使用Leader. 而事实上, 终端的vim也不能映射Command, 只有MacVimIt’s okay.

    Advanced 1: Multiple files

    Multiple files actually don’t matter, it’s just the javac时, 换%*.java, 换%:r为真正的mainfile name.

    Advanced 2: Project

    If your project has a fixed structure, it is essentially the same.

    When compiling, you may add -cp选项指定依赖路径, 添加-d选项指定输出路径. 如要打包为jar and add shortcut keys.

    You can define a .project属性文件, 描述项目结构和依赖, 再用VimL语言或python语言, 读取属性文件, 分别编译再输出. 至于依赖, 去~/.m2/directory to look for in the project root directory, their locations are very regular.

    Now, you can map keys to the above operations, which are just shortcut keys, vim多的是. 除了Ctrl/Alt/Shift, 还有每个字母和数字, 还有leader加任意长度字母和数字, leader不够了, 还是mapleader前缀. 对了, 你可能更喜欢F1-F12.

    Advanced 3: Dependence

    OK, you have basically reinvented maven了, 只是差自动下载依赖的功能. 但python的网络库那么多, 我猜测mvn的仓库应该是对外开放的, 只要遵循相关协议就能下载. 包与包之前有依赖关系, 你可以用python and written a dependency handling algorithm.

    AlthoughVimL残的跟渣一样, 但我们支持python啊, Ruby啊, Lua啊, 总有一款你喜欢的语言接口. 什么你觉得运行慢, 不要紧, 用C++写, 做成C/S架构的, 你看看YCMhow did others achieve it, just imitate it.

    Ultimate: Completion

    Okay, the compilation, running and packaging dependencies have been solved, but it is still a project management plug-in. Writing Java没有补全, 那么多API, 那么长的方法名, 还有各种样板写法和各种奇怪的异常抛出, 手写Javacode is really laborious and requires a lot of brain capacity.

    How can this work? You must get a completion plug-in. Vim界补全的王者YCM是在Clang横空出世后, 才搞出来的. 在这之前, GNUg++是不会共享语法分析的数据的. 你得自己写一个语法分析器, 对, 给C++...In order to perform smart completion.

    So, Java这边究竟什么情况, 我还真不知道, 但就Java95年就搞出来, vim91年就搞出来了, 它俩共渡的日子少算也有20年了, 在这期间, 各种Java IDEIt’s a lot of exports, but I haven’t seen anyone make a fool of themselves.

    YCMJava提供的补全, 使用eclim, 它和YCM没有关系, YCM只是提供接口, eclim没有YCM也能正常工作. eclim插件的使用, 需要安装eclipse, Java界以缓慢开源著称的IDE. 也就是说, eclim在底层使用eclipse to complete. If you don’t mind the super slow startup speed and completion speed, you can give it a try.

    This is simply unbearable, so the author immediately started using the C++写个Java的语法分析器, 做成C/S架构, 在插件中与服务器沟通来补全. 你也可以直接给YCM写个Javabackend.

    It is said, Java语法规范页数已经超过C++. But it’s okay, we have to create difficulties even if there are no difficulties, let alone if there are difficulties.

    So far, I have pointed out a way to the gods for the subject. After climbing over the thorns, you will suddenly become a curse. The children of both factions will worship and worship you forever.vimJava两界永远的大神, 彻底终结Vim不适合写Java

    If you write it out,

    The platform has a lot of brother languages...Java还活着的话..., JVM

    Why don’t you say

    teach eternal life!!!vim啊, 因为vim

    Postscript

    I hereby declare that this article does not contain any ridicule, teasing, ridicule, or questioning of the subject. I will not apologize if I make the subject unhappy.

    I’m just saying that, for

    multiple lines, this is so concise that only I can understand it.vim真没什么不可能的, 只要你有只够的技能时间. 这主要因为vim只是一个编辑器, 它把该开放的接口都开放了, 你想干什么, 写插件就OK了. Vim的中文手册, 抛开一些更新日志, 至少有1500页. 即便你看完了, 也不见得会写插件, 还要看书. 只就Vim的快捷键和操作, 我粗略了记了一下前300页的用户指南, 笔记就记了1000

    Usually, on this issue, Vim提供了:make接口来编译, 和makeprg来设置编译参数. 但也要承认, Vim设计之初, 大部分的设计都是为C这样的编译语言做的, 像文本对象a{是选择{}中的代码, 但对于python来说, 块代码是同缩进的代码, 没有{}, 对Scala来说, 块只有一条语句时, 可以省略{}.

    Dedicated to all Vimers who have struggled trying to write Java, please don’t be afraid, I am one of you too

    Vim并不完美, 但比绝大多数VimerThose who look alike should be perfect.

    I believe that every beginner Vim, 并被Vim的强大折服的人, 都曾试图豪言, Vim在手, 天下我有. 然后, 在工作中碰到了Java, 陷入装逼痛苦的泥淖中, 然后, 要么决定找一份别的工作, 要么用上了IDE. 待再次回头, 便看到了Vim has his own limitations.

    Seeing the power of a tool is not understanding, seeing its limitations is true understanding.

    黄色的树林里分出两条路
    而我选择了IDE
    你呢

    Updated

    Single filejavaFile compilation and mapping, only press enter once to reduce interference

    autocmd BufNewFile,BufReadPre *.java nnoremap <leader>rs :up <bar> !javac % && java %:r<cr>
    autocmd BufNewFile,BufReadPre *.java nnoremap <leader>cs :up <bar> !javac %<cr>

    Basically, in the same directory, the library files are compiled and run using <leader>cs编译, 执行文件用<leader>rs.

    Herersrun single file的缩写, 同cscompile single file.

    Mainly for correspondencerpcp. 可以使用:!mvn subcmd来映射编译和运行项目文件. pproject.

    reply
    0
  • PHP中文网

    PHP中文网2017-04-18 09:08:46

    Didn’t you see that test.class was generated?


    It should be possible, but it requires plug-in support.

    reply
    0
  • ringa_lee

    ringa_lee2017-04-18 09:08:46

    No, vim itself does not have the compiler link function. It is not known whether it is configurable or supported by related plug-ins.

    reply
    0
  • PHP中文网

    PHP中文网2017-04-18 09:08:46

    Hello, I use vim to develop Java. You can try a unit test plug-in I wrote to execute tests.
    https://github.com/wsdjeg/JavaUnit.vim

    Of course you can also use my configuration directly

    https://github.com/wsdjeg/DotFiles

    reply
    0
  • ringa_lee

    ringa_lee2017-04-18 09:08:46

    Normally not possible because vim cannot compile and generate class files

    reply
    0
  • 巴扎黑

    巴扎黑2017-04-18 09:08:46

    map <F4> :"!javac %"<cr>
    map <F5> :"!java %<"<cr>

    Add these two sentences to the configuration file, F4 to compile, F5 to run (note the space after <F4><F5>), of course, the premise is that you have installed jdk and configured environment variables. Debugging feels bad

    reply
    0
  • Cancelreply