Reputation: 1
I have an absolute path to a file (/path_to_file/file.file
) embedded in my code, which is open in the VS Code editor. I want to create a shortcut to quickly open this file by either just clicking on the path or highlighting the path and pressing a shortcut.
// my code blabla
file = "/path_to_file/file.file"
I have something like this in mind, only it does not work:
keybindings.json:
[
{
"key": "ctrl+y",
"command": "editor.action.openFileAtPath",
"when": "editorTextFocus"
}
]
The command editor.action.openFileAtPath
does not exist. Is there an alternative?
I have tried creating an own snippet:
{
"OpenFilePath": {
"prefix": "openfile",
"body": [
"const filePath = \"$TM_SELECTED_TEXT$\";",
"vscode.workspace.openTextDocument(filePath).then(doc => {",
" vscode.window.showTextDocument(doc);",
"});"
],
"description": "Open the selected file path"
},
and using it with keybindings:
{
"key": "ctrl+y",
"command": "editor.action.insertSnippet",
"when": "editorTextFocus",
"args": {
"langId": "",
"name": "OpenFilePath"
}
}
This, however, only replaces the highlighted text with the content of the snippet, but does not execute it. How can I achieve that?
Upvotes: 0
Views: 840
Reputation: 31
In relation to this answer https://stackoverflow.com/a/76916786/10320822:
The following should allow it to work for absolute or relative paths in Linux:
{
"key": "alt+q",
"command": "findInCurrentFile",
"args": {
"description": "Open absolute or relative path that is selected",
"run": [
"$${",
"const selectedText = '${selectedText}';",
"const currentFilePath = vscode.window.activeTextEditor.document.uri.fsPath;",
"const workspaceFolders = vscode.workspace.workspaceFolders;",
"let uri;",
"const absolutePath = path.isAbsolute(selectedText) ? selectedText : path.join(path.dirname(currentFilePath), selectedText);",
"uri = vscode.Uri.file(absolutePath);",
"vscode.commands.executeCommand('vscode.open', uri);",
"}$$"
]
}
}
It does not seem to work with "~" to get to a user space, and it does not seem to work from windows while ssh into linux.
Upvotes: 0
Reputation: 50024
Bit of a workaround: If your code can stomache it, adding the file://
schema part to the path to turn it into a file URL will cause VS Code's builtin document link providers to allow ctrl+click. See the docs for DocumentLinkProvider
's provideDocumentLinks
method, which says:
Note that the editor ships with a default provider that detects
http(s)
andfile
links.
Ex. file:///my/absolute/path/to/file
or file://C:/my/absolute/path/to/file
.
Upvotes: 0
Reputation: 180611
You will need an extension to do this. Another option is the Find and Transform extension (which I wrote). The necessary code is very small. This keybinding will find the path around the cursor - in your case assuming it is surrounded by quotes.
{
"key": "alt+q", // whatever keybinding you like
"command": "findInCurrentFile",
"args": {
"description": "Open absolute path around cursor", // whatever you want here
"find": "(?<=\")([^\"]+)(?=\")", // assumes path is bounded by quotes
"isRegex": true,
"restrictFind": "matchAroundCursor", // put cursor anywhere on the path
"run": [
"$${",
// uses the regex capture group $1 from the find
"const uri = vscode.Uri.file( '$1' );", // treat $1 as a string
"vscode.commands.executeCommand('vscode.open', uri);",
"}$$",
]
}
},
If a find
regex won't work for you to isolate the path, you can also do it by selecting the entire path. Try this keybinding:
{
"key": "alt+q",
"command": "findInCurrentFile",
"args": {
"description": "Open absolute path that is selected", // whatever you want here
"run": [
"$${",
"const uri = vscode.Uri.file('${selectedText}');",
"vscode.commands.executeCommand('vscode.open', uri);",
"}$$",
]
}
}
Upvotes: 1
Reputation: 28623
I made 2 extensions that can be used to solve this:
{
"key": "ctrl+y",
"command": "htmlRelatedLinks.openFile",
"args": {
"file": "${command:selecttxt}",
"method": "vscode.open",
"viewColumn": "2",
"command": {
"selecttxt": {
"command": "extension.commandvariable.transform",
"args": { "text": "${selectedText}" }
}
}
}
}
If the file paths have a particular pattern in the file you can use the setting html-related-links.include
and the extension will create Ctrl+Click
links.
The pattern could be all strings that start with a /
:
"html-related-links.include": {
"python": [
{ "find": "\"(/[^\"]+)\"", "isAbsolutePath ": true }
]
}
Upvotes: 0