最近在尝试使用 Vim,有个问题请教,就
public class test {
public static void main(String args[]) {
System.out.print("shit,Test success!");
}
}
在 Vim 中,我可以直接运行调试吗,像 Sublime 的 cmd+B
一样。
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
, 只有MacVim
It’s okay.
Advanced 1: Multiple files
Multiple files actually don’t matter, it’s just the javac
时, 换%
为*.java
, 换%:r
为真正的main
file 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
架构的, 你看看YCM
how 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
, 那么长的方法名, 还有各种样板写法和各种奇怪的异常抛出, 手写Java
code is really laborious and requires a lot of brain capacity.
How can this work? You must get a completion plug-in. Vim
界补全的王者YCM
是在Clang
横空出世后, 才搞出来的. 在这之前, GNU
的g++
是不会共享语法分析的数据的. 你得自己写一个语法分析器, 对, 给C++
...In order to perform smart completion.
So, Java
这边究竟什么情况, 我还真不知道, 但就Java
95年就搞出来, vim
91年就搞出来了, 它俩共渡的日子少算也有20年了, 在这期间, 各种Java IDE
It’s a lot of exports, but I haven’t seen anyone make a fool of themselves.
YCM
对Java
提供的补全, 使用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
写个Java
backend.
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.vim
和Java
两界永远的大神, 彻底终结Vim
不适合写Java
The platform has a lot of brother languages...Java
还活着的话..., JVM
teach eternal life!!!vim
啊, 因为vim
PostscriptI 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
并不完美, 但比绝大多数Vimer
Those 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 filejava
File 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
.
Herers
是run single file
的缩写, 同cs
为compile single file
.
Mainly for correspondencerp
和cp
. 可以使用:!mvn subcmd
来映射编译和运行项目文件. p
为project
.
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.
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.
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
ringa_lee2017-04-18 09:08:46
Normally not possible because vim cannot compile and generate class files
巴扎黑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