Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Clickable file paths #1972

Open
lenovouser opened this issue Jul 19, 2024 · 39 comments
Open

Clickable file paths #1972

lenovouser opened this issue Jul 19, 2024 · 39 comments
Labels
contributor friendly A well-scoped, approachable issue for someone looking to contributor.

Comments

@lenovouser
Copy link
Member

In iTerm you can cmd+click all the valid file paths to open it inside the configured application for that file type. I use that functionality quite a lot, for example when compiling TypeScript to quickly open the files with issues.

Might be related to #968.

iterm-clickable-files.mp4
ghostty-clickable-files.mp4
@briancain
Copy link
Member

I would +1 this, and also say, it would be nice if this worked for URLs too. It's one of the features that I really miss after switching from iTerm 2. I thought maybe @mitchellh had an issue about this earlier this year, but I'd love to be able to cmd+click urls!

@jcollie
Copy link
Collaborator

jcollie commented Jul 19, 2024

Ghostty recently added support for OSC 8, which allows apps to embed hyperlinks. Ghostty has had for a long time auto-detection of hyperlinks shown on screen. If you hold down the control key and hover your mouse over an embedded URL you can click on them. Try using ls --hyperlink=always as a test. As far as detecting random file paths in the output, I don't think that will ever happen as it would likely be impossible to construct a regex that would reliably detect them. Apps will need to be modified to output OSC 8 escape codes to embed hyperlinks.

@jcollie
Copy link
Collaborator

jcollie commented Jul 19, 2024

Here's the PR that added OSC 8 support: #1928

@mitchellh
Copy link
Contributor

I would +1 this, and also say, it would be nice if this worked for URLs too. It's one of the features that I really miss after switching from iTerm 2. I thought maybe @mitchellh had an issue about this earlier this year, but I'd love to be able to cmd+click urls!

This is already implemented. Hold command on Mac and ctrl on Linux.

@briancain
Copy link
Member

Ah, OK, well must be my tmux interfering with that! You're right though, outside of tmux it works.

Kapture.2024-07-19.at.10.58.21.mp4

@gpanders
Copy link
Member

@briancain tmux (and other full screen TUI apps like Vim) capture your mouse, so click events are intercepted by the application and not received by Ghostty. You can hold the Shift key to escape mouse capture. So to click on a link in tmux (or Vim, etc.), you would use Cmd+Shift+Click.

@briancain
Copy link
Member

@gpanders - fair enough! Just saying, folks coming from iterm to ghostty, tmux cmd+click still works in iterm2 land 😄

Kapture.2024-07-19.at.13.30.17.mp4

At this point it's a little off topic from the original issue so I'm happy to take it to a new one

@mitchellh
Copy link
Contributor

If someone can point me to the regular expressions used by iTerm or any other terminal for these file path captures then I could add this very quickly since our URL system was built to be generic like this.

@mitchellh mitchellh added enhancement contributor friendly A well-scoped, approachable issue for someone looking to contributor. labels Jul 20, 2024
@Syphdias
Copy link
Contributor

Related but not the same: OSC for paths.
image

@jcollie
Copy link
Collaborator

jcollie commented Aug 21, 2024

See #2111. Automatically detecting paths via regex is very difficult to get right, but apps that support OSC 8 make that a lot easier. ls --hyperlink=always works well.

@mitchellh mitchellh changed the title Feature request: clickable file paths Clickable file paths Oct 6, 2024
@nitsujri
Copy link

Just discovered this via hnews post. Incredible stuff everyone! I fired up ghostty and loved it instantly. The speed kicks!

For me, I come from iTerm2 land and this is sadly a hard requirement as it's baked into my workflow. I recognize it's a hard problem not only on detection but allowing which program load if you want to get away from system's settings.

Really hoping to see this soon!

@rrotter
Copy link
Contributor

rrotter commented Dec 29, 2024

If someone can point me to the regular expressions used by iTerm or any other terminal for these file path captures then I could add this very quickly since our URL system was built to be generic like this.

Cleanroom disclaimer: I've read no source code to figure this out, this is 100% from my experience using iTerm, and testing the edge cases.

Roughly speaking: ([^\s]+| )+
It's both more and less complex than you're thinking. There is no fancy regex, instead there is a heuristic:

  • for any line in the buffer iTerm keeps track of what $PWD was
  • when holding ⌘:
    • The line under the mouse is tokenized into:
      • continuous non-whitespace up to any length
      • single ' ' chars (idk what it does w/ \t, \n, etc, only tested ' ')
      • thus foo bar baz would be 6 tokens: ['foo',' ','bar',' ',' ','baz']
    • Token immediately under mouse is tested for existence, i.e. [[ -i $PWD/$TOKEN ]], where $PWD refers to the working dir when the line was printed, not when the token is tested/clicked
    • If $PWD/$TOKEN exists, it's underlined+clickable
    • If not, keep adding adjacent tokens until something is clickable or we run out of tokens
    • A token beginning with / is assumed to be an absolute path, so $PWD doesn't matter
    • / alone is always ignored, so there is no hyperlink to the root dir
    • Some examples of edge cases that are actually correctly detected:
      • Library/Application Support/
      • Library/Application\ Support/
      • 288B 18 Dec 17:23 # assuming a make a file or dir with such a bad name mousing over its name will work

The easiest way to demo this is to open iTerm and run:

~ % echo "Library/Application\ Support Library/Application Support Documents"
Library/Application\ Support Library/Application Support Documents
# note that when holding ⌘ key, each of the above are clickable, both in the command and the command output
~ % echo foobar
foobar
# not clickable, unless you have a file w/ this name 
~ % mkdir Support ' '
# now note that the token `Support` and the individual space chars above are now clickable

edit: ~, ~/Some/Path, ~nobody are also correctly detected as valid absolute paths. While ~ is expanded, paths printed with vars in them like $HOME are ignored.

edit: also picks up line numbers appended to the end of a path, in the format myfile.txt:123, and when clicked these go to the correct line in the file, at least in TextMate (only editor I've texted). This is a killer feature for opening files from stack traces like:
/opt/homebrew/Cellar/[email protected]/3.2.6/lib/ruby/3.2.0/benchmark.rb:311
I'm assuming there is some macos api for this, because I can't figure out any way to form a file:/// url that does this.

@zx8
Copy link

zx8 commented Dec 29, 2024

edit: also picks up line numbers appended to the end of a path, in the format myfile.txt:123, and when clicked these go to the correct line in the file, at least in TextMate (only editor I've texted). This is a killer feature for opening files from stack traces like:

Agree this is a very useful feature! I rely on it pretty much daily.

I think the logic for identifying line numbers is here:

@daviskirk
Copy link

daviskirk commented Dec 30, 2024

This is how it's configured in guake on linux:
Image

Python traceback: ^\s*File\s\".*\",\sline\s[0-9]+
Python pytest report: ^\s.*\:\:[a-zA-Z0-9\_]+\s
line starts by 'ERROR in Filename:line' pattern (GCC/make). File path should exists.: [a-zA-Z0-9\/\_\-\.\]+\.?[a-zA-Z0-9]+\:[0-9]+
line starts by 'Filename:line' pattern (GCC/make). File path should exists.: ^\s*[a-zA-Z0-9\/\_\-\.\ ]+\.?[a-zA-Z0-9]+\:[0-9]+

@ConradIrwin
Copy link

@mitchellh The Zed implementation of this is here: https://github.com/zed-industries/zed/blob/033726cf8713829ea5d5626e9bf86044c1dacd04/crates/editor/src/hover_links.rs#L803-L891

  • Seek back from the cursor position to the previous unescaped whitespace or quote character, then seek forward to the same. (or in Regex terms /(\\.|[^ '"])+/)
  • Unlike URLs, because there are many more valid file-names than valid files, you need to post-process by checking whether the file exists before showing the hover. This depends on the current working directory.
  • iTerm (but not Zed) has special handling for [ab]/ as shown in git diff so those are still clickable.

@amustaque97
Copy link
Contributor

this is partially implemented by #4713

@martimlobao
Copy link

I believe this is implemented in #4713 and released in 1.1.0 and can be closed now. To click on a file path, hold command and click on the highlighted path.

@jacobsvante
Copy link

I can confirm that absolute file paths are clickable. Relative are not however (well, you can click the part that follows the first relative path component, which doesn't really make any sense).

@Pyppe
Copy link

Pyppe commented Jan 31, 2025

Also, it is not working if the file contains line and column references, such as:

/Users/foobar/myproject/src/app/YOLO.bar:47:70

This is e.g. what many compilers output ($file:$line or $file:$line:$column). Would be nice if these worked also 🤞 (as they work e.g. in iTerm, where I used to use them pretty much daily).

@Eveeifyeve
Copy link

What does the iterm implementation open your default editor via $EDITOR environment virable.

@Pyppe
Copy link

Pyppe commented Jan 31, 2025

  1. My EDITOR environment variable in unset
  2. E.g. for *.tsx files both iTerm and Ghostty open VS Code, and for *.scala it's IntelliJ (IMHO these are defined by the OS)
  3. However, when the filename includes file.scala:$line (e.g. file.scala:4) or file.scala:$line:$col (e.g. file.scala:4:10) "as a suffix" then Ghostty no longer opens anything (on my Macbook at least) — whereas iTerm still opens them.

@lcrespom
Copy link

I see that Cmd+click on a file name opens the file via the Mac "open" command. To me, it makes more sense that, if the $EDITOR variable is set, to open it with the configured text editor.

Is anybody aware of whether there is an open issue about that, or it's in plan to use $EDITOR, etc?

@nurpax
Copy link

nurpax commented Jan 31, 2025

I'd also expect this to use open instead of EDITOR. At least in my case, EDITOR is used for things like opening console mode nano or vim when writing a git commit message when working on the CLI whereas command-clicking the filename would be more like double clicking the file in Finder or Explorer (on Windows if that worked here), in which case I'd expect the file to open in VSCode.

@lcrespom
Copy link

The best approach would be to have a configuration entry to support both, I don't mind open being the default if I can set it to $EDITOR or even configure the edit command itself.

@llimllib
Copy link

llimllib commented Jan 31, 2025

The #1 feature I want from ghostty is the ability to click on a link from rg or ls and open it in console mode vim

Kitty has it and it's an incredibly killer feature that I use constantly

edit: I filed this as #5627 since I think it's substantively different from this issue

@Eveeifyeve
Copy link

Well the problem the current regex doesn't support ./src/example.py:22:4 for example

@jcollie
Copy link
Collaborator

jcollie commented Feb 2, 2025

Well the problem the current regex doesn't support ./src/example.py:22:4 for example

The current regex is getting unwieldy... And it's matching both too much and not enough. For example, anything that matches \.[.a-zA-Z0-9] will be linked as a URL.

@Eveeifyeve
Copy link

Well the problem the current regex doesn't support ./src/example.py:22:4 for example

The current regex is getting unwieldy... And it's matching both too much and not enough. For example, anything that matches \.[.a-zA-Z0-9] will be linked as a URL.

I also feel like there should be a seperate regex for urls and file urls because It just seems one regex is not match the two sorrectly.

@koliyo
Copy link

koliyo commented Feb 3, 2025

Just adding a reference to how it is configured in wezterm, https://wezfurlong.org/wezterm/hyperlinks.html#implicit-hyperlinks

@olivM
Copy link

olivM commented Feb 3, 2025

the regex doesn't quite get the whole link path :
Image

@psychonaut
Copy link

I am not sure if my solution fits this issue, but sometimes, the URL regex/routine doesn't work properly. I have cases when the sentence contains a URL with ':' at the end.

Image

In this case ':' is used as part of the URL when it shouldn't. This is quite typical in terminal emulation but somehow gnome-terminal recognizes this URL properly

@daviskirk
Copy link

I'd also be super ok with a default that works for 95% of cases and allow customizing the regex and the command that get's called when you click a link. I know the goal of ghostty is to be configuration-free out of the box for 9x% of users but an optional configuration option would perhaps be helpful here. That way, we could:

  • Open the editor of choice at the line + column (that is my use case 99% of the time for clicking file links in the terminal, but this may vary and editors also might have different ways of passing the line/column to open at)
  • Customize the regex if you have tooling that is outside the standard syntax (special traceback / compiler output / partial paths and so on)

@jdietrich-tc
Copy link

I would love for it to also support (or allow me to write a regex that does) other protocols, and rely on the system to take action. for instance slack://channel?team=XXXXXXX&id=YYYYYYY which will open the specified channel in slack if clicked in iTerm2. I have also created and registered my own protocol `clipboard://XXX' which will send the url encoded XXX to the clipboard unencoded.

@ldavison
Copy link

ldavison commented Feb 4, 2025

I think regex support would be covered by https://ghostty.org/docs/config/reference#link ? Unfortunately, it says it "can't currently be set".

I don't know if there is another discussion/ticket specifically for this (I couldn't find one). In addition to supporting other protocols, in Alacritty and iTerm2, I have a regex for Jira tickets that opens them in a browser.

@piyiotisk
Copy link

any update on this? Also could you give an example how we can set the link-url or the link properties?

@llimllib
Copy link

llimllib commented Feb 7, 2025

@ldavison I filed #5627 for that issue, I think it's a different one than the top-level post on this discussion

amustaque97 added a commit to amustaque97/ghostty that referenced this issue Feb 16, 2025
amustaque97 added a commit to amustaque97/ghostty that referenced this issue Feb 16, 2025
@isaachinman
Copy link

@mitchellh As per #189 (comment), can you let us know if this feature request is on the "immediate" roadmap?

It's the sole thing keeping me on iTerm at the moment, as it's so integral to my workflows.

@rrotter
Copy link
Contributor

rrotter commented Feb 20, 2025

@mitchellh As per #189 (comment), can you let us know if this feature request is on the "immediate" roadmap?

It's the sole thing keeping me on iTerm at the moment, as it's so integral to my workflows.

Your answer is right in the comment you linked:

Otherwise, if you aren’t doing the work, please respect the other volunteers on this project and be patient. We’ll get it it when we want. If this is a blocker for you, you can and should use another terminal. There are fantastic choices out there!

Sorry, I want this feature too, but this is an all volunteer project. Your options are to patiently wait, write the code yourself, or find/hire someone to do it for your.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
contributor friendly A well-scoped, approachable issue for someone looking to contributor.
Projects
None yet
Development

No branches or pull requests