Score:11

Creating a hyperlink from command line output on a terminal

cn flag

The command hostnamectl, when executed with no argument, will dump a bunch of information to the terminal. One of that is the Operating System line as shown in the sample output below:

$ hostnamectl  
 Static hostname: xxxxxx
       Icon name: computer-laptop
         Chassis: laptop
      Machine ID: e8..43
         Boot ID: 7b..2a
Operating System: Ubuntu 21.10
          Kernel: Linux 5.13.0-28-generic
    Architecture: x86-64
 Hardware Vendor: XXXXXX
  Hardware Model: XXXXXX

However, the output "Ubuntu 21.10" is underlined like a hyperlink and when you Ctrl+click, it will open a web browser with the following link address: https://ubuntu.com/.

How is this accomplished? What are the terminal commands (escape sequences?) that I need to output from a text based program or script to create such a hyperlink on the terminal?

Score:13
cn flag

This information was not present in Ask Ubuntu, but I found the answer in another site: https://gist.github.com/egmontkob/eb114294efbcd5adb1944c9f3cb5feda

The escape sequence to create the above link is:

printf '\e]8;;https://ubuntu.com/\e\\Ubuntu 21.10\e]8;;\e\\\n'

The general format is:

The hyperlink open sequence starts with

  • the \e]8 escape sequence (ESC, ], and 8),
  • the params,
  • the target URI, and
  • the closing \e\ escape sequence (ESC and \)

where params is optional: \e]8;params;URI\e\\

Then comes the content that will be painted as hyperlink.

The hyperlink is closed with the same escape sequence, omitting params and the URI, but keeping the separators: \e]8;;\e\\


As a bonus to this answer, I suggest you use the following alias in your ~/.bash_aliases file:

alias ll="LC_COLLATE=C ls --hyperlink=auto --group-directories-first --color=auto -alF"

Here, the --hyperlink argument will direct the ls command to automatically create hyperlinks for file names on terminal output, so that when they are Ctrl+clicked they will open in Nautilus or the associated application.

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.