Video tutorial

Full screen to watch

preface

At first I just wanted to build a Vim environment for Java development on my phone.”Play Linux System Termux in Mobile Phone and build Java Development Environment”Playing with the discovery of vim really sweet! It can not only build a simple Java development environment, but also control medium and large Spring Boot projects. The final product is not much worse than a mature IDE.For the novice as little as possible to step on the pit, this article I write as much as possible in detail, the big guy himself jump to watch.

Effect of the finished product

Let’s show you the final effect. vim! A command line program, the first time I saw this effect is really amazing! Image from spacevim.org/cn/use-vim-… I brought it straight to you. It’s the same as what I measured.

Code completion

Vim-javacomplete2 provides excellent code completion for Java projects. With the AutoComplete module, you can complete the code in real time while editing the code, and can fuzzy match.

Syntax checking

The Checkers module, which provides asynchronous syntax checking for SpaceVim, mainly includes the plug-in Neomake. Currently supported projects include Maven, Gradle, and configuration files under Eclipse.

Cursor symbol document query

This function also relies on the LSP module. The default shortcut key is SPC l D or K:

There are many screenshots, I will not take one over, you can click the link above to see. Personally, as long as there are two functions of code completion and syntax detection, most projects can be developed. But it’s not just that. Almost every feature you can think of for an IDE has plug-ins, or alternatives. Of course, the final effect must be much less than the paid product Intellj IDEA.

Dissuade reminder! Is it worth messing with Vim to develop Java?

How does Vim work? B station a video said too good, suggested to watch, give me a lot of inspiration. Vim: From Abusive to Loving – The Ultimate Vim Tutorial 01 – Configuring your Own Best IDE

Vim is an ancient tool among programmers. Although it has a reputation for being hard to learn, once you get used to it, it will make you a hundredfold more productive!

Q: Is it necessary to turn Vim into a smooth IDE for Java development? Worth it? It depends, and I can tell you from nearly 10 years of Java development experience, it’s not worth it for most people.

  • If you don’t like Vim, don’t worry about it.

  • If you like Vim, then I would suggest Intellj IDEA + IdeaVim is sufficient. In my daily work, I also combine Intellj IDEA + IdeaVim.

  • If you don’t just like Vim, you’re obsessed with Vim. Pure Vim is great! You’re a fiddler. It’s worth it.

It also brings the following effects that are difficult to achieve in other development environments.

  • Compared with Intellj IDEA VIm, it is lighter and faster, and can be started almost in a second. For example, if a few modifications are made temporarily, VIM has been modified before IDEA has been loaded.
  • Since Android can be built on Linux using Termux, vim can be used to do Java development on your phone or tablet. It is understood that iOS has similar software that could build Linux environments. The performance of Linux on Android devices is limited. Whether Intellj IDEA can be installed or not, the experience of using small screens is good or not, so far it must be very difficult.
  • Using SSH smooth remote development and maintenance, technical support. Compared with graphical remote desktop, SSH remote has lower requirements on the network and almost no requirements on the network. Graphical remote networks transmit images, whereas SSH transmits basically text information. Very low bandwidth requirements, and save traffic.

Metro mobile phone SSH remote server do Java development, lasting 2 hours message flow about 6MB now in this era, 6MB May not be enough to brush a short video. Before this I used a mobile phone to use sunflower remote own computer to do development, difficult to be almost unusable. Cut out the input method, the phone screen takes up half, and the input method on the phone conflicts with the input method on the remote desktop. The point is that the signal in the subway is unstable and the delay is very big. I have finished typing, and the remote desktop has an obvious sense of delay. Because of the jam, I also bought sunflower VIP for two years. It can only be said that it is better than before and still gets stuck. Traffic consumption is also large, estimated to have hundreds of MB, because the experience is not good, did not adhere to 2 hours.

If the viM development environment is set up, you can easily take a device that may use SSH to develop, cloud development anytime and anywhere. Even a TV with a Bluetooth keyboard could be developed.

Prepare the material

  • Network access to Github is smooth

Network problems to solve their own way, might as well try your mobile phone hot, anyway, MY Unicom 4G hot access to Github very quickly. You need to use it in more than one place. If you can’t do this, don’t try.

  • A Linux or macOS or Windows PC or phone

I measured in Ubuntu (mobile phone simulation),CentOS,macOS have been successfully built. Since I am considering SSH remote support, Windows has not considered it for the time being, but Windows does.

If we can do that, then we’re in for a rough ride. Friendly note, because different environments (system version and software) may have some small differences, will bring some small holes, I also because really stepped on a lot of holes just want to write this article. I’ll try to keep everyone out of the hole.

My environment

For this tutorial, I specially installed centos-7-x86_64-minimal -2003.iso virtual machine. The system has just been installed with almost no setup. Download address: isoredirect.centos.org/centos/7/is…

Don’t worry if the environment is different, it’s more of the same. I installed Ubuntu 20.04.2.0 (arm phone) and macOS Big Sur 11.2 perfectly, and the steps were almost the same. Different places to Google you know.

Install Neovim that supports PYTHon3

Isn’t vim used to build Java development environment? Cry Wolf and sell dog meat? The guest officer calmed down, not so. In fact, I was originally using vim+ plug-ins to build a Java development environment, really tread numerous holes. Complex setup, not suitable for viM beginners. That is, until I accidentally distributed SpaceVim, a gadget that almost sets up what you want without having to put your own pieces together.

  • What version of Vim/Neovim does SpaceVim support?

Vim 7.4 or Neovim V0.1.7 or later is recommended. For better user experience, it is recommended to use the latest version of Neovim.

In order to avoid detours, I personally suggest that we use NeoVim first, and then slowly fiddle with VIm after success. Neovim is almost the same as Vim, so I will not introduce more, if you are interested to check online.

Pit alert: The Neovim version of PYTHon3 must be supported. If you use Vim as well, why support PYTHon3, since many plug-ins rely on Python3. Some plug-in project homepages do not mention python3. Many systems pre-install VIm, but almost all do not support PYTHon3 and will report errors when installing the plug-in. I have an article about stepping on this pit “Javacomplete Needs Python3 Support to Run!” CentOS vIM8 + PYTHon3

The official installation method for different OS versions is github.com/neovim/neov…

I use CentOS 7 as an example. For a detailed tutorial, see my “Centos Installs Nvim and supports Python3”.

Install python3 first, git will be used later, the following code is executed as the root user.

yum install python3 python3-devel git -y
Copy the code

I am not going to install it with yum. Yum is not a bad idea. As we all know, the software version installed by yum is usually a lower version. I prefer the newer one. Github.com/neovim/neov…

Download the latest stable version of Neovim, which at the time of writing was NVIM 0.4.4. Download nvim-Linux64.tar. gz download and upload to the user directory, use your favorite software to upload. Uploading in command mode:

scp ~/Downloads/nvim-linux64.tar.gz  @vmware:~/
Copy the code
tar -zxvf nvim-linux64.tar.gz
mv nvim-linux64 /usr/local/
cd /bin
ln -s /usr/local/nvim-linux64/bin/nvim nvim
Copy the code

Once the installation is complete, run nvim –version to check whether the installation was successful and the version information.

For security reasons, it is strongly not recommended to use root for daily development. So let’s create a user. It’s not impossible if you just want to use root, the operations are the same. It can be ignored if there are ordinary users

# create a user named vimer, username is not required, according to your preferences
adduser vimer
# Set vimer's password
passwd vimer
Switch to the vimer user
su vimer
Copy the code

Sometimes it is necessary to upgrade permissions, so that the Vimer user can use sudo to upgrade permissions. The root user executes Visudo to find root ALL=(ALL) ALL and add a line below

vimer   ALL=(ALL)       ALL
Copy the code

Add python3 support, using the vimer user to execute:

pip3 install --user --upgrade pynvim
Copy the code

To check whether nvim already supports PYTHon3, start nvim and run the following command.

:checkhealth
Copy the code

Don’t worry, as long as you execute the above commands, it should already be supported.

Install SpaceVim

The official tutorial is here: spacevim.org/cn/quick-st…

This is the time to test your network. Maybe Unicom 4G hotspot can reach you. If you have an accelerator, here are some tips for setting up a terminal agent:

exportHttp_proxy = http://192.168.0.101:1087;exportHttps_proxy = http://192.168.0.101:1087;Copy the code

One-click install script

curl -sLf https://spacevim.org/cn/install.sh | bash
Copy the code

The diagram below:Until the following message is displayed indicating that the installation is successful.

Installation complete! = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = open Vim or Neovim, All plug-in will automatically install the = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = thank you support SpaceVim, welcome feedback!Copy the code

When you run Nvim on the command line, your Nvim will look different.

Press k Enter, then :q exits and restarts. The plugin starts to install itself.Fortunately, a successful installation! I have encountered several instances where the first installation failed, and then re-installed the failed installation successfully, as shown in the image above.

[Vimproc] Vimproc’s DLL

If you run nvim, the following error is reported. [vimproc] vimproc’s DLL: “/home/vimer/.SpaceVim/bundle/vimproc.vim/… oc_linux64.so” is not found. Please read :help vimproc and make it.

Solution a: blog.csdn.net/lxyoucan/ar…

Install GCC related to compiling:

sudo yum install gcc gcc-c++ -y
Copy the code

Compile vimproc

cd ~/.SpaceVim/bundle/vimproc.vim
sudo make
Copy the code

To solve the code

What if you find that instead of displaying something like the one in my screenshot, you have garbled characters? And so on. That’s because of the font. Just install the nerd-font font and set the font for the terminal emulator to nerd-font.

Take macOS, for example. Use Homebrew to install the nerd-font font and run the following commands in sequence:

brew tap homebrew/cask-fonts
brew install font-hack-nerd-font --cask
Copy the code

Set the itermSet the Mac terminal.Fixed SpaceVim icon garbled in TermuxFont file download

The font file upload/data/data/com termux/files/home /. Termux font. The vera.ttf directory can solve the problem of SpaceVim garbled.

No garbled so comfortable!

In other cases, I won’t give you an example, whatever the terminal emulator is, just set up the font.

oh my zsh

This is not required, but I can’t go back since USING OH my ZSH.

Install and configure ZSH

sudo yum install -y zsh
Copy the code

Check the ZSH position

[vimer@localhost vimproc.vim]$ which zsh
/usr/bin/zsh
Copy the code

Set ZSH to default.

chsh -s /usr/bin/zsh
Copy the code

If the result is as follows:

chsh -s /usr/bin/zsh
Changing shell for vimer.
chsh: "/usr/bin/zsh" is not listed in /etc/shells.
Use chsh -l to see list.
Copy the code

The editorsudo nvim /etc/shellsAnd add a line/usr/bin/zsh Then execute again without error:

[vimer@localhost vimproc.vim]$ chsh -s /usr/bin/zsh
Changing shell forImer. Password: Shell changed.Copy the code

Then the next time you re-log in the Vimer user will use ZSH.

Install oh my ZSH

Official home: ohmyz.sh/ one command to install:

sh -c "$(curl -fsSL https://raw.github.com/ohmyzsh/ohmyzsh/master/tools/install.sh)"
Copy the code

I rename this configuration according to my personal habit.

# rename
mv ~/.zshrc ~/.zshrc.oh-my-zsh

Copy the code

Nvim ~/.zshrc Adds the following line.

# Load oh My ZSH plugin
source ~/.zshrc.oh-my-zsh
Copy the code

The first thing I did was install a new plug-in called zsh-syntax-considering. It provides syntax highlighting for the ZSH Shell.

cd $HOME/.oh-my-zsh/plugins
# Download code
git clone https://github.com/zsh-users/zsh-syntax-highlighting.git
# Automatic configuration
echo "source ${(q-)PWD}/zsh-syntax-highlighting/zsh-syntax-highlighting.zsh" >> ${ZDOTDIR:-$HOME}/.zshrc
Copy the code

source ~/.zshrcFound that the input command can be highly displayed. Automatic completion of zSH-autosuggestions

cd $HOME/.oh-my-zsh/plugins
# Download code
git clone https://github.com/zsh-users/zsh-autosuggestions ${ZSH_CUSTOM:-~/.oh-my-zsh/custom}/plugins/zsh-autosuggestions
Copy the code
nvim ~/.zshrc.oh-my-zsh
Copy the code

Modify, addzsh-autosuggestions source ~/.zshrc

This can automatically complete, convenient! The OH My ZSH plug-in is now installed.

Install JDK11

This is also a pitfall for me. Currently JDK8 is still mainstream. I installed JDK8 before, SpaceVim code completion does not come out, nor does it report errors. I have the following article: installing SpaceVim Java Development Environment for Linux

This is likely to ruffle some feathers, as it is not highlighted in the official documentation. The key is that the function does not come and does not report errors.

Download www.oracle.com/java/techno…

Download the appropriate JDK for your platform. Generally, download jdK-11.0.10_linux-x64_bin.tar. gz and create a directory ~/.soft to save the JDK.

mkdir ~/.soft
Copy the code

Upload the file to the ~/.soft directory.

SCP ~ / Downloads/JDK - 11.0.10 _linux - x64_bin. Tar. Gz vimer @ vmware: ~ /. SoftCopy the code

Extract:

The tar - ZXVF JDK - 11.0.10 _linux - x64_bin. Tar. GzCopy the code

The decompressed directory is /home/vmer/.soft/JDK-11.0.10. Configure environment variables and edit nvim ~/.zshrc

#JAVA
exportJAVA_HOME = / home/vimer/soft/JDK - 11.0.10export PATH=$PATH:$JAVA_HOME/bin:.
export CLASSPATH=$JAVA_HOME/lib/tools.jar:$JAVA_HOME/lib/dt.jar:.
Copy the code

Reload the environment variable source ~/.zshrc to verify that Java is configured successfully!

 ~ java -version
java version "11.0.10"2021-01-19 LTS Java(TM) SE Runtime Environment 18.9 (Build 11.0.10+8-LTS-162) Java HotSpot(TM) 64-bit Server VM 18.9 (build 11.0.10 + 8 - LTS - 162, mixed mode)Copy the code

This means the installation is successful!

Code completion

This is the most important step, this step is not successful, all the rest in vain. SpaceVim does a lot for us, we just do simple configuration. Download the eclipse JDT. Ls

FTP. Yz. Yamagata – u.a SAN Antonio p/pub/eclipse… This link is slow to download, recommend the use of thunder and other download tools to download, wait patiently.

cd ~/.SpaceVim.d
Create a javalsp directory
mkdir -p ~/.SpaceVim.d/javalsp/.cache/javalsp
Copy the code

Unzip the downloaded content to~/.SpaceVim.d/javalspThe decompressed directory structure is as follows:configurationnvim ~/.SpaceVim.d/init.tomlAdd the following to the file:

[[layers]]
  name = "lang#java"

[[layers]]
  name = "lsp"
  filetypes = [
    "java"
  ]
  [layers.override_cmd]
    java = [
    "java"."-Declipse.application=org.eclipse.jdt.ls.core.id1"."-Dosgi.bundles.defaultStartLevel=4"."-Declipse.product=org.eclipse.jdt.ls.core.product"."-Dlog.protocol=true"."-Dlog.level=NONE"."-noverify"."-Xmx1G"."-jar"."~ /. SpaceVim. D/javalsp/plugins/org. Eclipse equinox. Launcher_1. 6.100 v20201223-0822. The jar." "."-configuration"."~/.SpaceVim.d/javalsp/config_linux"."-data"."~/.SpaceVim.d/javalsp/.cache/javalsp"
    ]
Copy the code

The value of -jar-configuration-data can be adjusted based on the actual deployment position. Especially for -jar items, since jar versions are constantly being updated and probably not the same!

– Configuration Select a configuration file based on the current system:

Config_win, Windows system config_MAC, MacOS system config_Linux, Linux system config_linux

After the configuration is complete, the next time you open Nvim, some plug-ins will be automatically installed.The plug-ins are obviously Java-specific.After installation, exit Nvim. Create a new directory to write a Java file test!

Note: Never create a Java test in a directory with many files. At first, I didn’t understand. Create a Java file in the user directory ~, so that the plugin will traverse all files in the user directory as the current project.

Can pressSpace (Space) l r mTo run the current project. The running results are as follows:

Code formatting

Careful friends will notice that code formatting (Space B F) does not work properly now. The following error is reported:

Neoformat: formatters java failed to run
Copy the code

How to solve it? See SpaceVim: Java Code Formatting

  1. Download Google – Java – format

SpaceVim. D /lib/google-java-format-1.9-all-deps.jar: /.spacevim. D /lib/google-java-format-1.9-all-deps.jar: /.spacevim. D /lib/google-java-format-1.9-all-deps.jar: /.spacevim. The spacevim. d directory is convenient for me to use on other machines later.

SCP Google - Java - format - 1.9 - all - deps. Jar vimer @ vmware: ~ /. SpaceVim. D/libCopy the code
  1. Configure the vim file~/.SpaceVim.d/init.tomlAdd a line

[[layers]] name = “lang# Java “add the following line below.

 java_formatter_jar = "~ /. SpaceVim. D/lib/Google - Java - format - 1.9 - all - deps. Jar"
Copy the code

Restart Nvim to edit the Java file and now you can format the Java code. Can be achieved by:NeoformatFormat, or shortcut keys:The space b f

Now you can have fun formatting your Java code.

Syntax tree error

By default, press F2 to display the syntax tree, but you’ll find the following error:Tagbar: Exuberant ctags not found! Please download Exuberant Ctags from ctags.sourceforge.net and install it in a directory in your $PATH or set g:tagbar_ctags_bin. Press ENTER or type command to continue Reason: Ctags are missing, so let’s install it. MacOS students can refer to:SpaceVim builds a Java Development Environment in macOS

CentOS7

sudo yum -y install ctags
Copy the code

Re-open Nvim to find that the problem has been successfully resolved!

reference

Using Vim to build Java development environment spacevim.org/cn/use-vim-…

Javacomplete needs Python3 Support to Run! SpaceVim builds Java development environment in macOS