• February 6, 2021
  • ·
  • 2 min read

Easily Navigating Back and Forth Between Files in Visual Studio Code

If you use a JetBrains IDE for work, chances are you've used the "View Recent Files" feature (⌘E on macOS) at least once before. I personally use it all the time, and it is deeply ingrained in my muscle memory at this point. I constantly catch myself hitting ⌘E even in Visual Studio Code which does not do the same thing by default, even after installing the IntelliJ keymap.

So I decided to fix this minor annoyance.

In Visual Studio Code, the "Go to file" command (⌘P) displays a list of recently opened files just below the search field. Here's the catch: it is the currently opened file that is initially selected instead of the previously opened file. This means that we can't hop back and forth between files as easily as we could in a JetBrains IDE.

In order to remedy this in Visual Studio Code, we need to set up a keyboard shortcut bound to ⌘E that will open "Go to file" and select the second file in the list of recently opened files:

1. Install the multi-command extension.

"Go to file" and "Navigate to the next list item" are two distinct commands in Visual Studio Code. As of now a keyboard shortcut can only trigger a single command at a time. Triggering multiple commands, although highly requested, isn't possible without the multi-command extension.

2. Define your custom keyboard shortcut.

Paste the following keybinding into your keybindings.json file.

{
  "key": "cmd+e",
  "command": "extension.multiCommand.execute",
  "args": {
    "sequence": [
      "workbench.action.quickOpen",
      "workbench.action.quickOpenNavigateNext"
    ]
  },
  "when": "editorTextFocus"
}

3. Happy coding!