DEV Community

mafflerbach
mafflerbach

Posted on

13 1

Vim as Java IDE

Yes I know. Vim is not an IDE, it is a text editor. And the original use case of vim is not to be a full blown IDE. But the plugin possibilities and plugin Landscape is so universal, that people invest the time and wrote language server integrations. nvim, a fork of vim, includes the language server support out of the box.

But what are the expectation of an IDE.
What comes in my mind is: Autocompletion, managing imports, go to definition/implementation/references formatting and debugging as well. Syntax highlighting as well of course.

I think, the most known Plugin for auto completions and language server integration is coc-vim. But it has the drawback, that it implements an own plugin manager and ecosystem, where you have to install the language-servers which you want to use. The coc plugin which I have installed are:

"coc-java": ">=1.4.12",
"coc-java-debug": ">=0.1.4",
Enter fullscreen mode Exit fullscreen mode

To install:

:CocInstall coc-java
:CocInstall coc-java-debug
Enter fullscreen mode Exit fullscreen mode

It covers auto-completion, import management and the "go to" stuff to navigate trough your code structure.

The debug part is covered by vimspector and with the coc-java-debug extension. I don't want to go in detail how to use both extensions because I don't use the standard mappings, but if you are following the readme then Vim will be near to an IDE.

In Addition: to run my tests or start the debug process, I have some key bindings which make it easier.


" start the mvn build in a tmux pane with debug flag an the current buffer as filter parameter 
" [vimux](https://github.com/benmills/vimux) is a plugin for tmux user to interact better between tmux and vim
" I just use it to kick of my maven build in debugmode with the actual Testclass
noremap <leader>ds :VimuxRunCommand "mvn -Dmaven.surefire.debug  -Dtest=".expand("%:t:r")." test --offline"<CR>

" runs mvn test with the current buffer name a filter parameter.  
noremap <leader>rs :call RunMvnThisTest(expand("%:t:r"))<CR>

" runs all tests, but catch the output and open a new buffer with the result of the mvn build
noremap <leader>rS :call RunMvnTest()<CR>

Enter fullscreen mode Exit fullscreen mode

You can see the implementation of the vim function here.
When I am starting the Debug mode, I am using

" after starting in debug mode, the build will stuck and waiting for the attaching debugger. 
map <leader>at :CocCommand java.debug.vimspector.start<CR>
Enter fullscreen mode Exit fullscreen mode

to attach to the debugger and switch to the vimpector tab.

But you have to remember that you need in your project root a .vimspector.json with following content:

{
  "adapters": {
    "java-debug-server": {
      "name": "vscode-java",
      "port": "${AdapterPort}"
    }
  },
  "configurations": {
    "Java Attach": {
      "adapter": "java-debug-server",
      "configuration": {
        "request": "attach",
        "host": "127.0.0.1",
        "port": "5005"
      },
      "breakpoints": {
        "exception": {
          "caught": "N",
          "uncaught": "N"
        }
      }
    }
  }
}
Enter fullscreen mode Exit fullscreen mode

Sentry image

Hands-on debugging session: instrument, monitor, and fix

Join Lazar for a hands-on session where you’ll build it, break it, debug it, and fix it. You’ll set up Sentry, track errors, use Session Replay and Tracing, and leverage some good ol’ AI to find and fix issues fast.

RSVP here →

Top comments (0)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay