# ⚙️ Get into IdeaVim configuration, from basic to advanced

The [IdeaVim](https://github.com/JetBrains/ideavim#ideavim) is a Vim
emulation plugin for IntelliJ Platform-based IDEs. It has a lot of
[supported motions and
operations](https://github.com/JetBrains/ideavim/blob/master/src/main/java/com/maddyhome/idea/vim/package-info.java)
and can help you to quickly edit code and navigate through it.

Install IdeaVim by using the IDE's plugin manager (File -\> Settings -\>
Plugins -\> Search for IdeaVim -\> Install IdeaVim).

# Basic configuration

You can configure IdeaVim with the `~/.ideavimrc` File. Which
is quiet mighty and very useful. It is similar to a `.vimrc`
file and supports some of the default vimrc settings and commands.

## [Set Commands](https://github.com/JetBrains/ideavim/wiki/set-commands#list-of-supported-set-commands)

For basic settings. Here some useful examples:

### Disable select mode for refactoring

    Usually IntelliJ will go in the `select mode`, when
    refactoring a variable or method name. This is only useful, if the
    whole name should be changed. So the `normal mode`, is
    better for me:


    ``` vimrc
    set idearefactormode=keep
    ```

### Enable IdeaJoin

    For the great IntelliJ join functions. Keymap: `J`
    (Shift+j).

    ``` vimrc
    set ideajoin
    ```

## [Plugins](https://github.com/JetBrains/ideavim/wiki/IdeaVim-Plugins#ideavim-plugins)

Some Plugins are already build in IdeaVim, and must not be installed
manually. You enable it via the `Plug` command .

### Activate surround Plugin

    which emulates the the [Vim
    Surround](https://github.com/tpope/vim-surround) plugin. Normal
    mode: `ys`, visual mode: `S`

    ``` vimrc
    Plug 'tpope/vim-surround'
    ```

### Activate commentary Plugin

    which emulates the the [Vim
    Commentary](https://github.com/tpope/vim-commentary) plugin. Keymap:
    `gc`

    ``` vimrc
    Plug 'tpope/vim-commentary'
    ```

## Keymaps

They work the same like in Vim. `map` is the keyword.
`noremap` for non non-recursive mappings. And
`nnoremap` for normal mode, `vnoremap` for visual
mode, etc.

Small examples:

### Yank to the last character of the Line

    `Y` (Shift + y)

    ``` vimrc
    nnoremap Y yg_
    ```

### Jump to the class definition

    ``` vim
    "(gh = go to header)
    nmap gh gg<Action>(MethodDown)mxz<CR>5<C-y>`x

    " gg     = go to beginning
    " mx     = sets a mark, so we can jump back
    " z<CR>  = Scrolls to te Top
    " 5<C-y> = scrolls the window 5 lines
    " `x     = jumps back to our mark
    ```

## IntelliJ Actions

The interesting part. You can execute every action, you might find in
IntelliJ. For example, `refactor a variable`,
`fetch changes`, `run tests`, etc. With this, you
might configure IntelliJ to behave like
[Spacemacs](https://www.spacemacs.org/) or [Doom
Emacs](https://github.com/hlissner/doom-emacs#doom-emacs). (They use
`Space` as leader key and enable you to execute regular
commands with it. For example `SPC-g-F` for git fetch)

With the command `<Action>` (nearly) every possible action can be
triggered.

### Examples

    Show Commit Tool Window

    ``` vim
    nmap <Leader>gg <Action>(ActivateCommitToolWindow)
    ```

    Set bookmark and show bookmarks

    ``` vim
    nnoremap <leader>bm <action>(ToggleBookmark)
    nnoremap <leader><enter> <action>(ShowBookmarks)
    ```

    Run Main Method, Test or Class

    ``` vim
    nnoremap <Leader>rr <action>(RunClass)
    ```

### Find Actions

    To find an overview about all actions or search for them type:

    ``` src
    :actionlist
    ```

    Find via name/description

    ``` src
    :actionlist commit
    ```

    Find via Keymap

    ``` src
    :actionlist <C-K>
    ```

    "Track action Ids" is very helpful. When performing an action via
    mouse click and/or keyboard, a popup shows the last used action id.
    You find this "IdeaVim: track action Ids" in the ActionsMenu
    (double click shift)

# Advanced IdeaVim configuration

In this chapter I will explain which Plugins are helpful and how to
extend the `.ideavimrc` config with Intellimacs and my own
config. This will enable some nice key chords to execute most of your
IntelliJ actions with it. For example, when you press space (SPC),
"g", "b", you can change your Git Branch. Most of the commands start
with your "Leader" Key, which will be the space bar. Then it will
continue with a group, like "g = Git" and then your final action "b =
switch (b)ranch". Of course there are shorter and longer ones, too.
Here some more examples:


| Key binding | Description             |
|-------------|-------------------------|
| `SPC g l`   | (g)it (l)og             |
| `SPC f o`   | (F)ile (o)pen           |
| `SPC r r`   | (r)un method            |
| `SPC r s`   | (r)un (s)elected method |
| `SPC ,`     | show last used files    |

Some commands doesn't start with the leader key, like

Some commands doesn\'t start with the leader key, like

| Key binding | Description              |
|-------------|--------------------------|
| `g t t`     | (g)o to (t)ests          |
| `g d`       | (g)o to (d)efinition     |
| `g i`       | (g)o to (i)mplementation |
| `g s`       | (g)o to (s)uper class    |

Some are longer and start with "SPC m" or ",". Also named your
"local leader" key.

| Key binding | Description                         |
|-------------|-------------------------------------|
| `, d d`     | start debug mode                    |
| `, d s`     | select method for debug mode        |
| `, r i n`   | refactor and inline variable/method |
| `, r e v`   | refactor and extract as variable    |
| `, r e p`   | refactor and extract as parameter   |
| `, r e c`   | refactor and extract as constant    |

In my opintion, they are much easier to remember, then usual hotkeys.
But of course, you can mix them as you want.

## [Plugins](https://github.com/JetBrains/ideavim/wiki/IdeaVim-Plugins#ideavim-plugins)

There are a lot of additional IdeaVim Plugins, here my favorites

### [Commentary](https://github.com/JetBrains/ideavim/wiki/IdeaVim-Plugins#commentary)

    Commend out your code. Commands: `gcc`, `gc` +
    motion Enable with:

    ``` vim
    Plug 'tpope/vim-commentary'
    ```

### [Surround](https://github.com/JetBrains/ideavim/wiki/IdeaVim-Plugins#surround)

    Surround your text with brackets etc.. Commands: `ys` +
    motion + sign, (insert mode) `S` + sign Enable with:

    ``` vim
    Plug 'tpope/vim-surround'
    ```

## [Intellimacs](https://github.com/MarcoIeni/intellimacs#intellimacs)

This project mimics Spacemacs key bindings. This is a good starting
point for your configuration. It has a ton of useful keymaps. Like:

| Key binding | Description                        |
|-------------|------------------------------------|
| `SPC g c`   | Git clone                          |
| `SPC g f l` | Show current file log              |
| `SPC g b`   | Git branches (checkout)            |
| `SPC g G`   | Version control tool window        |
| `SPC g g`   | Select a version control operation |
| `SPC g p`   | Push                               |
| `SPC g S`   | Show shelf                         |

and much more..

Clone it via:

``` bash
git clone https://github.com/MarcoIeni/intellimacs ~/.intellimacs
```

And add those lines to your `.ideavimrc` file

``` bash
source ~/.intellimacs/spacemacs.vim
source ~/.intellimacs/extra.vim
source ~/.intellimacs/major.vim
source ~/.intellimacs/hybrid.vim
```

Check the [Github
page](https://github.com/MarcoIeni/intellimacs#intellimacs), for a more
detailed description

## Early Access Program

You might enable EAP (Early Access Program), to get new features more
quickly. Go to "Vim Actions" and select EAP, to enable it.

## My IdeaVim configuration

My whole configuration with (some) comments you can find here:
[IntelliJ's IdeaVim
Configuration](https://github.com/ch-te/ideavim/blob/master/ideavim.org#intellijs-ideavim-configuration).
The plain config:
[.ideavimrc](https://github.com/ch-te/ideavim/blob/master/.ideavimrc).

# Further readings and link collections

This is a list of the links above and some more further readings from
other blog posts:

-   [IdeaVim](https://github.com/JetBrains/ideavim#ideavim) Github Page
-   [List of Supported Set
    Commands](https://github.com/JetBrains/ideavim/wiki/set-commands)
-   [List of Supported motions and
    operations](https://github.com/JetBrains/ideavim/blob/master/src/main/java/com/maddyhome/idea/vim/package-info.java)
-   [IdeaVim
    Changelog](https://github.com/JetBrains/ideavim/blob/master/CHANGES.md#the-changelog)
-   [Intellimacs](https://github.com/MarcoIeni/intellimacs) Further
    getting started guides:
-   [Getting started with
    Ideavim](https://ikenox.info/blog/getting-started-ideavim/)
-   [Customizing
    IdeaVim](https://dewaka.com/blog/2019/07/27/customising-ideavim/)


