173

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

DrDonkey
  • 45
  • 1
  • 8
Carlos Torrecillas
  • 4,965
  • 7
  • 38
  • 69

9 Answers9

386

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.

devpolo
  • 2,487
  • 3
  • 12
  • 28
ndonohoe
  • 9,320
  • 2
  • 18
  • 25
  • 4
    This will create the new file in the currently focussed folder. Is it possible to create the new file from the project root? – Oliver Joseph Ash Mar 12 '18 at 14:03
  • 3
    Is there any reason why `"key": "a"` wouldn't work? It works _only_ if I do `cmd+n` – Grant Mar 31 '20 at 12:06
  • @OliverJosephAsh This extension will do that for you: https://marketplace.visualstudio.com/items?itemName=ritwickdey.create-file-folder. It lets you type the exact path relative to the project root that you wish to create a file (or multiple files at once). – SUM1 Jun 07 '20 at 23:28
  • Also wanted to add for others: This answer depends only on the folder focussed _in the workspace menu_, not the explorer as a whole. Was trying to figure out why clicking a file in "Open Editors" then triggering this shortcut was creating the file in a different directory. You have to click a file in the directory you want in the workspace menu first. The open editors menu has no effect. – SUM1 Jun 07 '20 at 23:37
  • Third point: that extension also adds "Create File" and "Create Folder" options to the context menu in the workspace menu. – SUM1 Jun 07 '20 at 23:40
  • I want this to open a new file in vscode (as I would if I clicked on the add file icon with the little plus symbol), not in notepad. I just want to avoid clicking on the file add and folder add icons and just be able to do what they do but with keyboard shortcuts. I was able to do that for the new folder, binding a default shortcut to explorer.newFolder, but explorer.newFile opens Windows notepad. I want to name a new file while staying in vscode as happens when you click the icon. Can I do that? – user258081 Oct 11 '21 at 19:11
  • Aditionally you can focus into the Explorer with `ctrl+0` or `cmd+0`. With arrow keys you can go in and out of folders and have control over where the file should go. – khaki May 27 '22 at 09:43
47

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

devpolo
  • 2,487
  • 3
  • 12
  • 28
Gregor Woiwode
  • 1,046
  • 1
  • 9
  • 12
  • This didn't work for me: It gives me `workbench.action` not found – Abraham Mar 29 '19 at 04:25
  • 2
    @AbrahamHernandez try changing the commands to `explorer.newFile` and `explorer.newFolder` – mysl Nov 04 '19 at 20:30
  • @gregor-woiwode Good news! As you mentioned, the `explorerViewletFocus` trigger was added to Visual Studio Code in early 2017, and it is now fully functional. It may be best to update the answer to reflect that, so that even more people can enjoy your solution. I myself found it tremendously helpful. Thank you for the great insight! – Jacopo Tedeschi Jan 04 '22 at 23:52
22

You can use this method if you don't want to create a shortcut.

  1. Navigate to the explorer via cmd+shift+e
  2. Get to the folder you want to create a new file/folder in.
  3. Open the file picker via cmd+shift+p
  4. Then type New File or New Folder and enter.

That is how you create a new file/folder without touching the mouse. :)

Fatih Bulut
  • 2,385
  • 1
  • 15
  • 12
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).

ouflak
  • 2,458
  • 10
  • 44
  • 49
Abhishek
  • 81
  • 6
  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Jan 28 '22 at 14:43
1

You can try this extension, it allows you to create files and folders even if the source being clicked is a file.

Demo:

Demo

Wenfang Du
  • 8,804
  • 9
  • 59
  • 90
1

Use ctrl+shift+p and search for "Preferences: Open Keyboard Shortcuts"

Then, inside the keybindings menu, search for "explorer.newFile"

you can bind it to ctrl+n, and set the "When" property to "explorerViewletFocus", so this command will only happen when the focus is in the explorer

Similarly, you can change "explorer.newFolder" to ctrl+shift+n and the "When" condition to "explorerViewletFocus", like before.

Now you can open files and folders via keyboard inside the selected folder

I preferred this solution to the other ones since I did not have to open that huge json file with the keybindings

Lucca Huguet
  • 11
  • 1
  • 1
0

You can do it in two steps:

  1. Cmd + Shift + E: Open the explorer with
  2. Ctrl + 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.

lehoang
  • 3,577
  • 1
  • 15
  • 16
0

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:

  1. Press CMD + SHIFT + P and search for new folder then click on setting icon on the right side of search result.

  2. 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

0

Try CTRL WIN ALT N While highlighting a file. Enter file name, double enter. Enjoy!