Lev
Lev

Reputation: 15654

Switch cursor from editor to file explorer and vice versa in VS code

Is there a shortcut to switch from editor to file explorer (and vice versa) on vscode ? I have yet to find one. Thanks you for your help.

Upvotes: 28

Views: 18863

Answers (6)

Novi
Novi

Reputation: 11

I set up the keyboard shortcut Ctrl + \ to quickly switch between the Editor and Explorer.

  1. ctrl + shift + P to open command palette

  2. Search : open keyboard shortcut(JSON)

  3. Add the following setting and save file.

    [
       {
          "key": "ctrl+\\",
          "command": "workbench.explorer.fileView.focus",
          "when": "!filesExplorerFocus"
       },
       {
           "key": "ctrl+\\",
           "command": "workbench.action.focusActiveEditorGroup",
           "when": "!editorTextFocus"
       }
    ]
    

Upvotes: 1

Sparrow1029
Sparrow1029

Reputation: 642

If anyone is scouring the web for a solution which allows <C-h> (Ctrl + h) and <C-l> (Ctrl + l) to be used with vim keybindings to easily move from:

  • 1st editor group (farthest left pane) to the file explorer
  • File explorer back to 1st editor group

This is similar to the functionality I remember when using NerdTree in regular vim.

Here's my hack:

// keybindings.json (open with Ctrl/Command + Shift + P) and search for JSON
// Keyboard shortcuts
[
    // ...snip
    // I already remap <C-w> h,j,k,l to <C-h>, <C-j>, etc
    {
        "key": "ctrl+h",
        "command": "workbench.action.focusLeftGroup",
        "when": "editorTextFocus && vim.active && vim.mode != 'Insert'"
    },
    {
        // Special case: when in first editor group (pane), go to sidebar
        // if visible.
        "key": "ctrl+h",
        "command": "workbench.explorer.fileView.focus",
        "when": "editorTextFocus && vim.active && vim.mode != 'Insert' && activeEditorGroupIndex == 1 && explorerViewletVisible"
    },
    {
        "key": "ctrl+l",
        "command": "workbench.action.focusRightGroup",
        "when": "editorTextFocus && vim.active && vim.mode != 'Insert'"
    },
    {
        // Special case: when in file explorer, go right (back to 1st open
        // editor group)
        "key": "ctrl+l",
        "command": "workbench.action.focusFirstEditorGroup",
        "when": "filesExplorerFocus"
    },
    // ...snip
]

Note: I had to remove some ctrl + l keybindings that were floating around in VS Code's keyboard shortcuts before this would work. See item 2 in the list below for help debugging keyboard shortcuts.

Some helpful references I found during my search for answers:

  1. VS Code's undocumented 'when' clause contexts
  2. VS Code's documented 'when' clause contexts
  3. How to debug VS Code keybindings (SO answer)
  4. Vim Keybinding cheat sheet

Upvotes: 5

drkvogel
drkvogel

Reputation: 2571

Focus the file explorer

ctrl+shift+e focuses the file explorer on Windows, and I think cmd+shift+e focuses the file explorer on on MacOS; but in Ubuntu (at least), ctrl+shift+e in an editor is the emoji shortcut, which is why I added this chord:

{
  "key": "ctrl+alt+r",
  "command": "revealFileInOS",
  "when": "!editorFocus"
}

The command revealFileInOS focuses the file explorer at the current file.

To add or change a keyboard shortcut, press ctrl+k, then ctrl+s (on Linux or Windows).

Focus an editor

To focus an editor pane, use ctrl+N on Linux or Windows, cmd+N on a Mac), where N is the pane number starting at 1.

Most commonly, you'll have a single pane, so it would be ctrl+1 on Linux or Windows, cmd+1 on a Mac.

Upvotes: 4

karlbsm
karlbsm

Reputation: 397

For macOS, use shift+cmd+E. Note that if the hotkey is also occupied by another program on your mac, you may need to disable it in the preferences of the program before it tasks effect in VSCode.

Upvotes: 9

Jason R Stevens CFA
Jason R Stevens CFA

Reputation: 3091

Unfortunately Ctrl + Shift + e doesn't do the trick for me, as I utilize various vim setups within vscode. This makes working around Ctrl keys often complex.

I can confirm that @MohamedNaleem's answer works when I turn off various vim integrations. If you don't use vim, I suggest sticking with the built-in.

My solution Focus & Visibility

I use the chord Ctrl+Space e to switch between the explorer and open editors. It feels nice and ergonomic to me -- all I do is hit the Ctrl and Space keys together and the e key quickly afterwards.

Notice the intentionally missing+ between Space and e when notating the chord.

Δ What to change in Keyboard Shortcuts Δ

The commands you're looking for are

  1. Explorer: Focus on Folders View, and
  2. View: Toggle Side Bar Visibility.

To change them simply bring up your command pallet (type Ctrl + Shift + P), type keyboard, select Preferences: Open Keyboard Shortcuts, and input the two shortcut commands above to search.

Double click to select and type your new Ctrl + Space e chord. Enter or Return to confirm. Voila!

Upvotes: 3

Mohamed Naleem
Mohamed Naleem

Reputation: 466

Please try the View: Show Explorer shortcut key, ctrl+shift+e.

Upvotes: 45

Related Questions