Android Studio : Using Vim

Ahmed Rizwan
6 min readJun 26, 2016

Ok so… Here’s an intro to Vim on Android Studio (and IntelliJ)… Because I’ve been using it exclusively for about 2–3 months now, and I think it’s awesome!

Preface → I am no expert in Vim… I’m definitely a beginner, but I absolutely love it (so far)… So yes, I wanted to share and demonstrate some of the bindings that I use… And maybe learn some new ones as I compose this article…

So… Let’s begin!

Installing the Plugin : IdeaVim

The first thing we need is the IdeaVim plugin. Installing it is pretty straight forward… You go to the Plugins, search for it, install it… And yeah… Restart the IDE…

Another cool plugin I use is AceJump… Install that one too! Comes in very handy sometimes!

Vim Basics

The main purpose of Vim (in a nutshell) is for you to stop using Mouse! Which initially is pretty tough, but once you get used to it, your speed/productivity improves.

So… How does Vim work exactly?

By default Vim is in Command mode (also sometimes referred to as the Normal mode), which means you can’t type/edit the text. You can only execute commands. You can switch to the Editing aka the Insert mode by pressing ‘i’ and bamm!

To go back to Command mode, press ‘Esc’. You can also remap the default keys.

A good place to start learning vim is this website tutorial.

So… Go on to the website, and learn Vim Basics if you don’t already know… It’s ok… I’ll wait…

I now assume that you know the absolute Basics… So it’s time to go over some of the bindings!

Moving Around

Here are some basic bindings for moving around.

Left → h

Right → l

Up → k

Down → j

I used KeyCastr so you can see the key strokes as I vim

Jump forward to the Start of the word → w

Jump forward to the End of the word → e

Jump backward to the Start of the word → b

Jump to the Start of Line → 0 (zero)

Jump to the first character of the Line → ^

Jump to the End of the Line → $

Append to the Start of the Line → I

Append to the End of the Line → A

Jump to next Block of code → }

Jump to previous Block of code → {

Next instance of the current word → *

Previous instance of the current word → #

First line of the file → gg

Last line of the file → G

Top of the screen → H

Middle of the screen → M

Bottom of the screen → L

Half page down → Ctrl+d

Half page up → Ctrl+u

Page up → Ctrl+b

Page down → Ctrl+f

There are certainly more commands for moving around, you can find them here

Editing

Replace single character → r

Change entire line → cc

Change word (onwards from cursor) → cw

Went a little crazy there…

Change entire word → ciw

Change till end of line → c$

Repeat last action → .

You get the idea…

Change case → ~

Change till next character e.g. ‘i’ → cti

Change till next character e.g. ‘i’ (inclusive) → cfi

Copy/Cut/Paste

Paste → p

Paste before the cursor → P

Copy entire line → yy

Copy a word → yiw

Cut entire line → dd

Cut a word → diw

Vim stores the copy/cut history in its Register that you can access via :reg command… You select the register with prefix “ and simply paste

Search and Replace

Search for pattern → /pattern

Search backwards for pattern → ?pattern

Repeat search in the same direction → n

Repeat search in the opposite direction → N

Replace all old patterns with new → %s/oldPattern/newPattern/g

Replace ‘mPaint’ with ‘mNewPaint’

Replace old patterns with new (ask) → %s/oldPattern/newPattern/gc

Selecting Text

Visual Mode → v

(While in selection mode, all ‘Moving Around’ commands work)

Macros

A very powerful feature of vim, which focuses on Repeatability!

You simply Start recording a macro, do edits, and end recording. The macro gets saved and you get to repeat the macro anytime you want.

Macros are stored to registers, from a to z…

You can access a register with prefix @

Recording a macro in d register :

Note: I mapped Esc to Ctrl+c on my Keyboard because it’s easier to reach

When I press ‘qd’ — status bar changes to ‘VIM — recording’

Playing the macro through ‘@d’

Cool! *_*

You can view the macros by viewing registers via :reg

The d register represents the macro

Now we have just scratched the surface for now… But I hope it’s been a good starting point for you (and I) to further explore Vim and other cool productivity plugins that IntelliJ/AndroidStudio has to offer…

Oh snap!

Almost forgot about the AceJump plugin!

Well here it is → You simply press Ctrl+; and then enter a character, AceJump highlights all the instances of that character with ShortCut keys. And by pressing the ShortCut key → You cursor moves to that position! :’)

AceJump in action :

First I search for ‘P’ and then ‘c’

Ok… So this is pretty much it for now… Hope the article was somewhat useful! If I come across more cool things to add this article, I will… In the meantime… Happy Coding!

Some cool resources:

Vim CheatSheets : one and two

An awesome intro to Vim

Macros

--

--