ikenox.info

Naoto Ikeno

Naoto Ikeno

Backend Engineer, Software Architecture & Design, Perl, Golang, GCP

Getting Started with IdeaVim

January 25, 2019

Japanese version is here

IdeaVim

IdeaVim

plugins.jetbrains.com
Github: JetBrains/ideavim

IdeaVim is a Vim emulation plugin for IntelliJ or other Jetbrains IDEs. This plugin are officially developed by Jetbrains as you can see the repository owner name.

Demo

Keymap is personally customized.

Why use IdeaVim

The advantages in each of IntelliJ and Vim are as follows, I think.

  • IntelliJ: Very powerful code completeion, code navigation, refactoring, and so on with easy and simple settings.
  • Vim: Very various and efficient operations as a text editor. A concept of the mode(normal/insert/visual).

So, IntelliJ with Vim-like operation can take both of these advantages. IdeaVim supports many Vim features.

In this blog post, I’ll introduce what features IdeaVim supports, how to setup IdeaVim, and how great IdeaVim is.

Supported Vim features

The followings are commonly used Vim features supported by IdeaVim.

FeatureSupported
ModeNORMAL, INSERT and VISUAL mode
Motionsyank( y ), delete( d ), change( c ), undo( u ), redo( Ctrl-r ),
text object operation( ciw , ci' , … ) and so on
Searchtextsearch and highlight by / , incremental search by :set incsearch
Replace:s , :%s , :'<,'>s and so on
Commands:w , :q , :tabnew , :split , a part of :set command and so on
KeymapWe can customize keymaps by same syntax with .vimrc . We can use map , set and other some commands. The details of this feature will be introduced later of this blog post.
Macroavailable
Registeravailable
Others:set surround enables vim-surround emulation

You can see more details at README.md of GitHub repositoy.

Install IdeaVim

In IntelliJ or other Jetbrains IDEs, you can install IdeaVim from [Preferences] > [Plugins]. After install, you have to restart the IDE to enable IdeaVim.

Install IdeaVim

EAP build

You can install an EAP (Early Access Program) build of IdeaVim by adding the following URL to [Settings] > [Plugins] > [Browse Repositories] > [Manage Repositories].

https://plugins.jetbrains.com/plugins/eap/ideavim

Currently, a new version of IdeaVim is officially released a few times for a year, so sometimes bug fixes or new features are not released soon. So I normally use the EAP build.

How to setup IdeaVim

.ideavimrc

IdeaVim loads a file ~/.ideavimrc when the IDE started. We can write the settings to .ideavimrc, with the same syntax as .vimrc. A list of supported set commands is here.
In addition, a set surround command enables a vim-surround emulation.

set incsearch
set ignorecase
set smartcase

nnoremap L $
nnoremap H ^
noremap ; :

Share the keymaps between Vim and IdeaVim

.vimrc and .ideavimrc can be written as the same syntax. This is a large advantage, because you don’t need to learn new specific syntax, and you can share the settings of the common basic keymaps (e.g. nnoremap ; :) between both of them.

To share the settings, I prepared a shared keymaps file .vimrc.keymap. It is loaded in .vimrc and .ideavimrc by a source command. Thus you can centralize the common basic keymaps to the one file.

.ideavimrc
" Load common basic keymaps
source .vimrc.keymap

" IdeaVim specific settings are here
.vimrc.keymap
" Common basic keymaps
nnoremap L $
nnoremap H ^
noremap ; :

The followngs are my .ideavimrc and .vimrc.keymap.

Vim-like keymap to IntelliJ functions

In my .ideavimrc, a statement map XXX :action YYY often appears. :action is a IdeaVim-specific command. By using :action command, you can call IntelliJ functions.

The following is an example of keymap.

nnoremap gd :action GotoDeclaration

GotoDeclaration is one of an “action”. An action corresponds to one of the IntelliJ functions. It navigates to the declaration of the symbol on the text cursor. So, nnoremap gd :action GotoDeclaration means, “When you type gd in the normal mode, then you’ll be navigated to the declaration of the symbol”.
Thus, by using an :action command, you can define a vim-like keymap to any IntelliJ functions, even very powerful code modification, code navigation, refactoring, and the more functions.

You can call any IntelliJ functions, from high-level functions (e.g. refactoring) to low-level functions (e.g. move text cursor). You can also call the functions of the IntelliJ plugin you’ve installed.

The :action command dramatically boosts the usability of IdeaVim-ed IntelliJ.
The followings are my recommended actions.

Examples of Action

Action概要
SearchEverywhereNavigate to any symbol
FindInPathFind text in the whole project
FileStructurePopupNavigate to any symbol in current file
GotoDeclarationNavigate to the declaration of a symbol
GotoSuperMethodNavigate to the super method of a symbol
GotoImplementationNavigate to the implementation of an interface
JumpToLastChangeNavigate to the place changed at last
FindUsagesList the usages of a symbol
RenameElementRename a symbol
ReformatCodeFormat code
CommentByLineCommentComment out
ShowIntentionActionsQuick fix
GotoActionCall anything

An example

nnoremap ,e :action SearchEverywhere<CR>
nnoremap ,g :action FindInPath<CR>
nnoremap ,s :action FileStructurePopup<CR>

nnoremap gd :action GotoDeclaration<CR>
nnoremap gs :action GotoSuperMethod<CR>
nnoremap gi :action GotoImplementation<CR>
nnoremap gb :action JumpToLastChange<CR>

nnoremap U :action FindUsages<CR>
nnoremap R :action RenameElement<CR>

nnoremap == :action ReformatCode<CR>
vnoremap == :action ReformatCode<CR>

nnoremap cc :action CommentByLineComment<CR>
vnoremap cc :action CommentByLineComment<CR>

nnoremap <C-CR> :action ShowIntentionActions<CR>

nnoremap ,a :action GotoAction<CR>
vnoremap ,a :action GotoAction<CR>

Search an action name

If you want to know an action name of some IntelliJ functions, you can use an :actionlist command. By using the command you can search an action by words.

But, currently there is no way to know the action name of the IntelliJ function which you want to call from IdeaVim.
Calling :actionlist will display assigned shortcut key for the each action. It may be a hint of the corresponded IntelliJ function.

Conclusion

In this blog post, I introduced IdeaVim. IdeaVim hasn’t emulated Vim completely yet, but it’s still a great plugin.
Let’s enjoy IdeaVim!

Naoto Ikeno

Naoto Ikeno

Backend Engineer, Software Architecture & Design, Perl, Golang, GCP


0 Comments


0 / 1000