skip to Main Content

I have an absolute path to a file (/path_to_file/file.file) embedded in my code, which is open in the VSCode 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?

2

Answers


  1. With the help of the extensions HTML Related Links and Command Variable

      {
        "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 }
        ]
      }
    
    Login or Signup to reply.
  2. 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);",
          "}$$",
        ]
      }
    },
    

    keybinding to open an absolute path from the file

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search