Keyboard navigation in Android Studio / IntelliJ

We spend most of our days in front of our computers. And since you are reading this, you are probably spending most of your time inside your IDE (which is Android Studio / IntelliJ).

To navigate around in your source code you probably use both your keyboard and your mouse. But the constant switch between mouse <-> keyboard can be annoying and time consuming.

You've heard about those mythical engineers that don't have to use the mouse at all. Maybe you've even seen one. But you said that this is not for you. You can't learn all those keyboard shortcuts. Your mouse is enough for you.

Fear not. The truth is that navigating in your source code without a mouse is not that hard and the shortcuts are not that many. Trust me, I've been there not wanting to leave my mouse for some stupid keyboard shortcuts. The truth is though that it can help you to be more productive and express faster your thoughts in code.

It's not that you will never use the mouse, but you will use it less. I am not an expert myself, but I enjoyed the transition to a using-the-mouse-less world.

Vim

You might have heard vim, one of the oldest and most popular command-line text editors. It's described as "a highly configurable text editor built to make creating and changing any kind of text very efficient". And it delivers on its promise.

In Vim, there are 2 modes: command mode and insert mode. In command mode, you don't edit the text but you move around (navigate). In insert mode, you enter text, and is the regular text-input mode that you know and love.

IdeaVim

IdeaVim is an IntelliJ plugin for "porting" some of Vim's features into Android Studio / IntelliJ. Not everything is there, but the most important ones necessary for navigating around your source code fast and efficiently are there.

A bonus of learning these shortcuts is that you can use them in regular vim that can be found installed in every Unix/Linux OS (including macOS).

Go to Preferences -> Plugins -> Marketplace -> Search for "IdeaVim" -> Install.

Modes

To switch between insert and command modes you use ESC and i. I repeat: command mode to move around, insert mode to edit/insert text. The rectangular cursor indicates that you are in command mode. The regular cursor means you are in insert mode.

  • Command mode -> Insert mode: i
  • Insert mode -> Command mode: ESC

Basic navigation

  • Move up: k
  • Move down: j
  • Move left: h
  • Move right: l

More advanced navigation

  • Go to the beginning of the next word: w
  • Go to the end of the next word: e
  • Go to the next block of code / method / paragraph: }
  • Go to the previous block of code / method / paragraph: {
  • Go to the first character of the file: gg
  • Go to the last character of the file: GG
  • Search for text (case-sensitive): / (enter text + hit enter)
  • Find the next occurrence of the last search term: n
  • Find the previous occurrence of the last search term: N

Basic editing

  • Delete current line: dd
  • Add a new line below and switch to insert mode to insert text: o
  • Add a new line above and switch to insert mode to insert text: O

These are just the tip of the iceberg. There are so many shortcuts in vim. You can take a look and see what's more convenient for your work type. But don't be intimidated. Give it a try, resist your initial urge to revert to your comfortable usual keyboard+mouse setup, and you will soon get used to moving around using the keyboard. Happy coding!