Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

VS Code - Add a new file under the selected working directory

People also ask

How do I add a directory to VS Code?

The File > Add Folder to Workspace command brings up an Open Folder dialog to select the new folder. Once a root folder is added, the Explorer will show the new folder as a root in the File Explorer. You can right-click on any of the root folders and use the context menu to add or remove folders.

How do I create a new file in Visual Studio Code terminal?

Ctrl + ` to open the terminal in Visual Studio Code. echo > "File. txt" to create a new .

How do I change the selected directory in VS Code?

One option is Ctrl + Shift + E to select the Explorer, use the arrow keys to navigate to the target location, and then click the New File or New Folder button.


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.


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


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. :)


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.