This should work for any terminal running Bash on Linux, BSD, or macOS. (video demo)
Note: It only opens the last link, but it could definitely be extended to select interactively.
# Add this somewhere in your .bashrc
# Record terminal to this file.
__terminal_recording_file="${HOME}/.cache/terminal_recording"
function __start_recording_terminal () {
# if the file exists, don't record, as this means that the recording is
# already happening in the current shell.
if [[ -f "$__terminal_recording_file" ]]; then
return
fi
# save the PID of the shell that started the recording
echo $$ >| "$__terminal_recording_file.pid"
# Start recording, with "flushing" option to have quick access to the last
# output, and quiet option to disable "started recording" message.
# Also, check if script can be run with '-f' (linux) or '-F' (macOS)
script -q -f /dev/null >/dev/null 2>&1
local script_can_be_run_with_f="$?"
if [[ "$script_can_be_run_with_f" -ne 0 ]]; then
script -q -F "$__terminal_recording_file"
else
script -q -f "$__terminal_recording_file"
fi
}
start_recording_terminal # comment this line to disable recording
function __open_last_visible_link_in_terminal () {
# look for the last thing that looks like a link in the last 50 lines of the
# terminal recording (assumed to be the currently visible part of the
# terminal)
local link="$( tail -n 50 "$__terminal_recording_file" | tac | grep -Eo -m 1 --color=never 'https?://[[:print:]]+')"
if [[ -z "$link" ]]; then
echo "No link found"
return 1
fi
echo "Opening link: $link"
open "$link"
}
# Bind Ctrl-K to "__open_last_visible_link_in_terminal" in all modes
# Also, it runs Ctrl-u first, so that the command line is cleared and it
# doesn't interfere.
bind -m emacs '"\C-k":"\C-u __open_last_visible_link_in_terminal\n"'
bind -m vi-insert '"\C-k":"\C-u __open_last_visible_link_in_terminal\n"'
bind -m vi '"\C-k":"\C-u __open_last_visible_link_in_terminal\n"'
# cleanup function on Bash exit
function __cleanup () {
# if a recording is happening and the parent of this shell is the recording
# command, kill the shell that started the recording and remove the files
ps -p $PPID | grep -q 'script -q'
is_parent_script="$?"
if [[ -f "$__terminal_recording_file" && "$is_parent_script" -eq 0 ]]; then
rm "$__terminal_recording_file"
# kill the shell that started the recording
local pid="$(cat "$__terminal_recording_file.pid")"
rm "$__terminal_recording_file.pid"
kill -9 "$pid"
fi
}
trap __cleanup EXIT
Btw Alacritty has that as a built-in feature: https://wiki.archlinux.org/title/Alacritty#Regex_hints
It can also be done in tmux with the tmux-urlview plugin.
Ctrl+Shift+F
search by for example regex Esc, then copy byCtrl+Shift+C
... not ideal and I know I have seen something about this elsewhere, but can not find it. Thought it was here gitlab.xfce.org/apps/xfce4-terminal - but does not look like it.xfcef-terminal
as well, (though a custom build). I have still not managed to locate where I read about the hyperlink collection. The botched highlighting is a (bug) I believe in vte (xfce uses vte). Actually if one search forward entire match is highlighted, but not when searching backwards (or on initial search). One can ofc usescreen
etc., but that is not a solution either.