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

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

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

ベストアンサー1

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.

カスタムバインディングファイルに以下を入力します(おそらくcmd+n代わりに入力する必要がありますがctrl+n、私はWindowsを使用しているためテストできません)

[
  { "key": "ctrl+n", "command": "explorer.newFile" }
]

エクスプローラーにフォーカスがあるときにのみこれを適用したい場合は、when 条件を追加できます。

{ "key": "ctrl+n", "command": "explorer.newFile", "when": "explorerViewletFocus" }

この方法では、他のコンポーネントにフォーカスがあるときにCtrl+を押すとn、デフォルトの新規ファイルコマンドが実行されます。

ショートカットUIを使用して編集する

Cmd+ Shift+を入力pkeyてEnterキーを押しますPreferences: Open Keyboard Shortcuts

これにより、キーボード ショートカットの設定 UI が開きます。

explorer.newFile検索に入力して新しいファイル コマンドを見つけ、それをダブルクリックしてショートカット キャプチャ モーダルを表示し、このコマンドに関連付けるキーの組み合わせを押します。

おすすめ記事