Shortcut to open a specific file in VSC?
You can do it with tasks, here is how:
Open your tasks.json
file:
F1 -> type: "Tasks: Configure Tasks" -> select: "Open tasks.json file"
Paste in the following:
{
"version": "2.0.0",
"tasks": [
{
"label": "openMyFile",
"type": "shell",
"command": "path_to_your_VSCode path_to_your_file.txt"
},
]
}
In your keybindings.json
add:
{
"key": "ctrl+scrolllock", # Or some other binding
"command": "workbench.action.tasks.runTask",
"args": "openMyFile" # <- Your task's name in this case openMyFile
},
That should do it.
The solution that relies on launching the code
command in Tasks feels a bit heavyweight and does not work with Code-Server. Here's a more "native" approach:
- Install the VSCode Macros extension.
- Make a file
macros.js
somewhere (e.g. in$workspace/.vscode/macros.js
):const vscode = require('vscode'); module.exports.macroCommands = { OpenTodo: { no: 1, func: () => openFile("/full/path/to/your/todo.md") }, }; async function openFile(filename) { const document = await vscode.workspace.openTextDocument(filename); const editor = await vscode.window.showTextDocument(document); }
- Add to your
settings.json
:"vscodemacros.userMacroCommands": [ { "path": "/full/path/to/your/.vscode/macros.js", "name": "OpenTodo" }, ]
- Open Keyboard Shortcuts (Ctrl+K Ctrl+S by default), find the command "VCSMacros: User Macro1" and map it to a key of your preference. Reload window (at least in code-server that was necessary for the keyboard shortcut to start working).