2

One of the very useful things about urxvt is that I could use keyboard shortcuts to navigate through and open hyperlinks that appeared on the terminal screen (by adding some urxvt* entries on .Xresources, IIRC).

Now I'm on xfce4-terminal and am missing this functionality. I took a look at ~/.config/xfce4/terminal/accels.scm but couldn't find anything relevant. For the moment, I'm stuck to having to grab my mouse and Ctrl+click the terminal URLs I want to open in the browser, which is quite disruptive to my workflow.

Question: is it possible to select and open URLs printed on the xfce4-terminal just using the keyboard?

I'd be glad to hear about other terminal emulators that offer this functionality, but I don't intend on going back to urxvt for the moment.

3
  • 1
    You can use Ctrl+Shift+F search by for example regex Esc, then copy by Ctrl+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.
    – ibuprofen
    Commented Jun 23, 2021 at 8:25
  • Thank you for your suggestion, @ibuprofen. This doesn't seem to work for long URLs spanning multiple lines, but it's definitely a step forward. I'll expand on this a bit as an answer as I wait for a more proper solution. Commented Jun 24, 2021 at 7:48
  • Yes. It is not a good solution. I use 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 use screen etc., but that is not a solution either.
    – ibuprofen
    Commented Jun 24, 2021 at 8:30

2 Answers 2

2

Expanding a bit on @ibuprofen's answer, here's a workaround that works as long as the URL doesn't span multiple lines:

  1. Open Ctrl+Shift+F
  2. Enable searching with regular expressions
  3. Enter http\S+ as the search term
  4. Press Enter until the desired URL is highlighted
  5. Press Esc
  6. Press Ctrl+Shift+C to copy the URL to the clipboard
  7. Paste it on your browser with Ctrl+V (you might need to press something like Ctrl+L first to focus the address bar)
2
  • You could use - look at menu -> Terminal -> Fin next / previous. If not bound to any key you can change ./config/xfce4/terminal/accels.scm (I use Ctrl+ and Ctrl- as I do not zoom anyway). Then after you have set search string you can search forward. When searching forward text that wraps is highlighted as well. Then - next time you want to search you do not need Ctrl+Shift+F if it is in same terminal window. Just do previous / next - and it searches using set pattern. Unfortunately it does not look like there is a way to make it save patterns between launches (as of now).
    – ibuprofen
    Commented Jun 24, 2021 at 13:45
  • nor setting regex as toggled. Had a look at this github.com/xfce-mirror/xfce4-terminal/blob/… etc. There are some custom MISC options one can set, and it would be easy enough to write a patch, but then one would need a custom build.
    – ibuprofen
    Commented Jun 24, 2021 at 13:46
1

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.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .