'VS Code - Add a new file under the selected working directory
I'm trying to get a shortcut to add a new file under my current working folder. So I navigate to the explorer using cmd+shift+e and when I get to the folder I want to create a new class I do cmd+n which creates me a new file but is not saved anywhere (I'm trying to get a similar behaviour to what ReSharper does for instance).
Is there any other shortcut to get the file created under the folder you have the focus in the explorer? Essentially I could like to get the text box to fill in the name then the file gets automatically saved so that I don't get the prompt later on.
I'm using for reference the following page: https://code.visualstudio.com/docs/customization/keybindings
Solution 1:[1]
To add files or folders add the following lines to keybindings.json:
Updated answer on 2020/02/10 (Thanks to @AbrahamHernandez, who provided the comment).
[
{ "key": "ctrl+n",
"command": "explorer.newFile",
"when": "!editorFocus" },
{ "key": "ctrl+shift+n",
"command": "explorer.newFolder",
"when": "!editorFocus" }
]
These bindings are active if the editor is not focused.
They are also triggered if the debug window is active. That's not perfect but the context explorerFocus is going to be added in the near future: see GitHub
Solution 2:[2]
You can use this method if you don't want to create a shortcut.
- Navigate to the explorer via cmd+shift+e
- Get to the folder you want to create a new file/folder in.
- Open the file picker via cmd+shift+p
- Then type
New FileorNew Folderand enter.
That is how you create a new file/folder without touching the mouse. :)
Solution 3:[3]
You can try this extension, it allows you to create files and folders even if the source being clicked is a file.
Demo:

Solution 4:[4]
{
"key": "ctrl+n",
"command": "explorer.newFile",
"when": "explorerViewletFocus"
}
Paste it in the Keybindings.json by pressing F1 key and search for Preference: Keyboard(JSON).
Solution 5:[5]
You can do it in two steps:
Cmd + Shift + E: Open the explorer withCtrl + N: Create a new file under the current folder and name it.
I actually like this approach than the magical-one-click because each step is clearer and you don't have to configure anything.
Solution 6:[6]
As for 2021 VSCode on mac the CMD + SHIFT + N opens a new VSCode window. If you want to change the behaviour and create new folders using only keyboard do the above to change keybinding:
Press CMD + SHIFT + P and search for new folder then click on setting icon on the right side of search result.
This brings up Keyboard Shortcut tab here you can change keybinding for new folder, click on pen icon and enter your desired keybinding and press ENTER
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | |
| Solution 2 | |
| Solution 3 | |
| Solution 4 | ouflak |
| Solution 5 | lehoang |
| Solution 6 | Farbod Vand |
