Open file from VSCode file explorer using keyboard?

I have switched focus to the explorer using cmd+shift+E. I have selected a file in the explorer using the arrow keys.

I have found that ctrl+enter opens the file in a new tab.

How do I preview the file?

How do I open the file?


Solution 1:

Built-in Command Overview

When working with VSCode on Windows, you can use the following commands to open/preview files from the explorer:

  • Give the editor focus with workbench.files.action.focusOpenEditorsView. Default keybinding: Ctrl + K E
  • Preview the highlighted file in the explorer with list.select. Default keybinding: Enter
  • Preview the highlighted file in the explorer in a new tab with explorer.openToSide. Default keybinding: Ctrl + Enter
  • Once the file preview is in focus and you want to open the file so it remains in the editor, use workbench.action.keepEditor . Default keybinding: Ctrl + K Enter

As of writing this, it seems it is not possible to keybind an action that directly opens the file from the explorer. Fortunately, we can remedy this with the macros extension as seen below.

Note: As for Mac, my understanding is that pressing Enter in the explorer will rename the file. Use the default shortcut Command + Down, or open the keyboard shortcuts menu and map list.select to something you want to use to open the file preview from the explorer. You could also remap renameFile from Enter to something else, and then use Enter for list.select.


Adding a Keybinding to Open a File Directly (Non-preview)

If you want to add a keyboard shortcut to directly open a file from the explorer list, without it opening as a preview, you can use the macros extension.

  1. Install the macros extension.

  2. Open the settings.json file and add the following. This will create a macro called openFileAndKeep that opens the file preview and then tells it to remain open.

     "macros": {
         "openFileAndKeep": [
             "list.select",
             "workbench.action.keepEditor"
         ]
     },
    
  3. Open the keybindings.json file and add the following.

     {
         "key": "enter",
         "command": "macros.openFileAndKeep",
         "when": "listFocus"
     }
    

Now when you press Enter and your explorer file list has focus, it will open the file in a non-preview state. Of course, you can also configure this to whatever key combination you desire.

Note: There is also an open issue for adding macro functionality to vscode, so we might be able to achieve this without an extension in the future.

Solution 2:

On Mac, if the File Explorer is visible in your VSCode Side Bar and the file you want to view is selected, just press the space bar.

To navigate between files:

  • Give File Explorer focus (Cmd-Shift-E).
  • Use the Up or Down arrow keys to navigate to your desired file.
  • Press the space bar. Your desired file should appear in the Editor.