Back to blog

Clickable Links in Terminal Output

2 min read

I was recently looking at a CLI I wrote that prints file paths directly to the terminal. As those paths got longer and more numerous, I realized this creates cluttered output and forces users to copy-paste manually.

Turns out there’s a simple solution: you can make terminal output clickable like HTML links using a special ANSI escape sequence called OSC 8.

Why This Matters

There’s a few drawbacks to printing raw URLs or file paths like this:

View logs at: https://example.com/logs/123456
View file at: file:///Users/jimmy/logs/app.log

They do the job, but:

Luckily, there’s a better way.

Here’s a minimal helper function:

export const link = (text: string, target: string) => {
return `\u001B]8;;${target}\u001B\\${text}\u001B]8;;\u001B\\`;
};
console.log(link("View Logs", "https://example.com/logs/123"));
console.log(link("Open File", "file:///Users/jimmy/logs/app.log"));

If your terminal supports OSC 8:

Screenshot of clickable links in terminal

If not supported, it just shows plain text.

How it works

The escape sequence breaks down as:

Compatibility

Most modern terminals support this:

Tip:

Resources

Final Thoughts

This is a tiny addition that dramatically improves developer experience in your CLI:

For a single line of code, it’s worth adding.

Let's Discuss

Questions or feedback? Send me an email.

Last updated on

Back to blog