VS Code - Add a new file under the selected working directory
Visual Studio-CodeVisual Studio-Code Problem Overview
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
Visual Studio-Code Solutions
Solution 1 - Visual Studio-Code
The cmd+n
command is by default bound to workbench.action.files.newUntitledFile
but what you want is the command explorer.newFile
which by default is not bound to a shortcut.
Edit shortcuts file
Hit Cmd+Shift+p type key
and hit enter on Preferences: Open Keyboard Shortcuts (JSON)
This will open keybindings.json file which stores custom keybindings specified by the current VS Code user.
Enter the following in the custom bindings file (presumably you need to enter cmd+n
instead of ctrl+n
but I'm on windows so can't test
[ { "key": "ctrl+n", "command": "explorer.newFile" }]
If you want to only have this apply when the explorer is focused you can add a when condition:
{ "key": "ctrl+n", "command": "explorer.newFile", "when": "explorerViewletFocus" }
This way when any other component is focused pressing Ctrl+n will execute the default new file command
Edit using shortcuts UI
Hit Cmd+Shift+p type key
and hit enter on Preferences: Open Keyboard Shortcuts
This will open up the keyboard shortcut preferences UI.
Type explorer.newFile
in the search to find the new file command, double click it to bring up the shortcut capture modal and press the key combination you want to associate with this command.
Solution 2 - Visual Studio-Code
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 3 - Visual Studio-Code
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 File
orNew Folder
and enter.
That is how you create a new file/folder without touching the mouse. :)
Solution 4 - Visual Studio-Code
You can try this extension, it allows you to create files and folders even if the source being clicked is a file.
Demo:
Solution 5 - Visual Studio-Code
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 - Visual Studio-Code
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
Solution 7 - Visual Studio-Code
{
"key": "ctrl+n",
"command": "explorer.newFile",
"when": "explorerViewletFocus"
}
Paste it in the Keybindings.json by pressing F1 key and search for Preference: Keyboard(JSON).