Debian Codegraph @

manual/src/hyperlinks.md

raw symbols SCIP
1 # Hyperlinks
2
3 Delta uses [terminal hyperlinks](https://gist.github.com/egmontkob/eb114294efbcd5adb1944c9f3cb5feda) to turn line numbers, file paths, commit hashes, etc into clickable links, as long as your terminal emulator supports it. Enable the feature with
4
5 ```gitconfig
6 [delta]
7 hyperlinks = true
8 ```
9
10 Commit hashes link to GitHub/GitLab/Bitbucket (use `hyperlinks-commit-link-format` for full control).
11
12 The links on line numbers (in grep output, as well as diffs) are particularly interesting: with a little bit of effort, they can be made to open your editor or IDE at the correct line.
13 Use `hyperlinks-file-link-format` to construct the correct URL for your system.
14 For VSCode and JetBrains IDEs this is easy, since they support their own special URL protocols. Here are examples:
15
16 ```gitconfig
17 [delta]
18 hyperlinks = true
19 hyperlinks-file-link-format = "vscode://file/{path}:{line}"
20 # hyperlinks-file-link-format = "idea://open?file={path}&line={line}"
21 # hyperlinks-file-link-format = "pycharm://open?file={path}&line={line}"
22 ```
23
24 Zed also supports its own URL protocol, and probably others.
25
26 If your editor does not have its own URL protocol, then there are still many possibilities, although they may be more work.
27
28 - The easiest is probably to write a toy HTTP server (e.g. in [Python](https://docs.python.org/3/library/http.server.html)) that opens the links in the way that you need. Then your delta config would look something like
29 ```gitconfig
30 [delta]
31 hyperlinks = true
32 hyperlinks-file-link-format = "http://localhost:8000/open-in-editor?path={path}&line={line}"
33 # Now write an HTTP server that handles those requests by opening your editor at the file and line
34 ```
35
36 Here's some Python code that could be used as a starting point:
37 ```python
38 from http.server import BaseHTTPRequestHandler, HTTPServer
39 from pathlib import Path
40 from subprocess import call
41 from urllib.parse import parse_qs, urlparse
42
43
44 class OpenInEditor(BaseHTTPRequestHandler):
45 def do_GET(self):
46 if self.path.startswith("/open-in-editor"):
47 query = parse_qs(urlparse(self.path).query)
48 [path], [line] = query["path"], query["line"]
49 # TODO: You might need to change this to construct the correct root directory for the
50 # project that the file is in, so that your IDE opens in the project workspace.
51 cwd = Path(path).parent
52 # TODO: Replace with the appropriate command for your editor
53 call(["code", "-g", f"{path}:{line}"], cwd=cwd)
54 self.send_response(200)
55 else:
56 self.send_response(404)
57 self.end_headers()
58
59
60 print("Starting open-in-editor server on port 8000...")
61 HTTPServer(("", 8000), OpenInEditor).serve_forever()
62 ```
63
64 - Another possibility is to register a custom protocol with your OS (like VSCode does) that invokes a script to open the file. [dandavison/open-in-editor](https://github.com/dandavison/open-in-editor) is a project that aimed to do that and may be helpful. However, registering the protocol with your OS can be frustrating, depending on your appetite for such things. If you go this route, your delta configuration would look like
65 ```gitconfig
66 [delta]
67 hyperlinks = true
68 hyperlinks-file-link-format = "my-file-line-protocol://{path}:{line}"
69 # Now configure your OS to handle "my-file-line-protocol" URLs!
70 ```
71 - Finally, you can just use traditional `file://` links (making sure your OS is configured to use the correct editor). But then your editor won't open the file at the correct line, which would be missing out on something very useful.