Skip to content

Instantly share code, notes, and snippets.

@mikeslattery
Last active January 26, 2024 14:02
Show Gist options
  • Save mikeslattery/04a9548d6b0dbe49292f9005ada418e3 to your computer and use it in GitHub Desktop.
Save mikeslattery/04a9548d6b0dbe49292f9005ada418e3 to your computer and use it in GitHub Desktop.
How to loosely integrate IdeaVim and gVim

This will allow you to switch between a Jetbrains IDE and gVim at the same file/line/column by hitting <leader>i. Hit <leader><leader>i to start gVim (once).

I've also done this with terminal Vim, but it's much more complex and varies depending on your environment (OS, DE, multiplexers, terminal).

Steps:

Install gVim and the IdeaVim plugin

In IDE: Settings > Build,Execution,Deployment > check "Allow unsigned requests"

Settings > Tools > External Tools > Alt+Insert

Field Value
Name gvim-start
Program gvim
Arguments --servername $ProjectName$
Working Directory $ProjectFileDir$

Settings > Tools > External Tools > Alt+Insert

Field Value
Name gvim
Program gvim
Arguments --servername $ProjectName$ --remote "+call cursor($LineNumber$, $ColumnNumber$)|call foreground()<CR>" "$FilePathRelativeToProjectRoot$"
Working Directory $ProjectFileDir$

In ~/.vimrc:

nnoremap <leader>i :execute "silent !curl -fs 'http://localhost:63342/api/file/".expand("%")."?line=".line(".")."&column=".col(".")."'"\|redraw!<cr>

In ~/.ideavimrc:

nnoremap <leader>i  :action Tool_External Tools_gvim<cr>
nnoremap <leader><leader>i  :action Tool_External Tools_gvim_start<cr>
@mikeslattery
Copy link
Author

mikeslattery commented Oct 16, 2021

I think I'll make this into a plugin. You'll have to add this to ~/.ideavimrc

nnoremap <leader>i :execute('!gvim --servername GVIM --remote +call cursor('.line('.').', '.col('.').'\|call foreground() %')

But that might be too limiting. I may have to write a viml function, call like:

nnoremap <leader>i execute('!gvim -s -e +idea2vim('.line('.').','.col('.').',"%")')

In the plugin the function idea2vim will do the following

  • Find the root of the project, by looking for .idea
  • Use the root directory name as servername
  • Launch gvim --servername if not already running
  • Remotely call the server gvim instance (call cursor ... foreground)

Also in the plugin will be another function vim2idea:

  • Save changes to current file.
  • Http call to switch to IDE. redraw.
  • If http port not open, explain how to open it.

To make things even easier and to support updates, I might just say to add this to .ideavimrc:

source ~/.vim/plugged/gvim-idea/ideavimrc.vim

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment