Dec 30, 2016

[IntelliJ] Switch focus back to editor while keep the embedded terminal open

TL;DR
IntelliJ IDEA 13's default shortcut, ⌥F12, closes embedded terminal when switching focus to editor window. Hitting ⌘2 twice works to switch focus to editor and still have the terminal open.

Software engineers often have demand to open both editor and terminal at the same time so they can code on editor and tail logs, display git-grep's result, or show whatever they want aside their editor. It is pretty handy because you can focus on coding task, but still keep an eye on and monitor server logs. Or you can open a particular log file on the terminal on the right side of your monitor, and apply changes to code on left side of your monitor. It is even handier if both can be displayed within IntelliJ IDEA since the pre-defined or user-defined shortcut keys allows engineers to switch from and to terminal when needed while both editor and terminal are displayed and aligned in more organized manner comparing to opening both Terminal.app and IntelliJ IDEA and toggle. It is Integrated Development Environment, after all.

The first thing one may notice is the pre-defined shortcut to toggle between terminal and editor: ⌥F12. When focus is on editor window, this opens embedded terminal and switch focus to it; when focus is on embedded terminal, switch focus to editor window. This, however, has one problem. Embedded terminal closes when focus is switched to editor. There actually are some modes that define the behaviour of the terminal window, and programmers can choose one or combinations of those modes depending on their preference. But still the terminal window closes with this shortcut. Hence the term, "toggle."

So how can we switch focus back to editor and still have the embedded terminal open? If there is no pre-defined shortcut, what is the best workaround? There were some workarounds introduced on the web including defining macros/shortcuts, and the simplest yet less disruptive way was found on stackoverflow.com. On this question, Andrey introduces the idea of switching the focus to some different window with pre-defined shortcut keys; dev shows comprehensive answer. Andrey's approach seems simple yet effective. Since the ⌥F12 shortcut closes terminal window on "toggle," just use another shortcut to "switch" focus to different window such as "Favorite" with ⌘2. At this point, the state is just the same as you hit ⌘2 from editor and switch focus to Favorite window. Then hitting esc key or another ⌘2 works to switch focus back to editor. The only requirement is to check "Docked mode" and "Pinned mode" for terminal window.

Note that embedded terminal is a bit different from other tool windows since this is terminal and consumes most key inputs including esc keys and other ⌘ related inputs. And as long as there is no pre-defined shortcut to just switch -- not toggle -- focus from and to terminal, the workaround that introduced above is required. There is a request on youtrack that asks for shortcut doing exactly the one discussed in this article, so until that is implemented or is declined the simple workaround without macro assignment could be enough.