用键盘打开终端链接

用键盘打开终端链接

非常有用的事情之一urxvt是我可以使用键盘快捷键导航并打开出现在终端屏幕上的超链接(通过在IIRCurxvt*上添加一些条目)。.Xresources

现在我已经打开xfce4-terminal并且缺少此功能。我看了一下,~/.config/xfce4/terminal/accels.scm但找不到任何相关内容。目前,我不得不抓住鼠标并Ctrl单击我想要在浏览器中打开的终端 URL,这对我的工作流程造成了很大的干扰。

问题:是否可以仅使用键盘选择并打开 xfce4 终端上打印的 URL?

我很高兴听到其他提供此功能的终端仿真器,但我暂时不打算回到 urxvt。

答案1

稍微扩展一下 @ibuprofen 的答案,只要 URL 不跨越多行,这里就有一个解决方法:

  1. 打开Ctrl+ Shift+F
  2. 启用使用正则表达式搜索
  3. 输入http\S+作为搜索词
  4. Enter直到突出显示所需的 URL
  5. Esc
  6. Ctrl+ Shift+C将 URL 复制到剪贴板
  7. Ctrl使用+将其粘贴到浏览器上V(您可能需要先按Ctrl+之类的键L才能聚焦地址栏)

答案2

这应该适用于在 Linux、BSD 或 macOS 上运行 Bash 的任何终端。(视频演示)

注意:它只打开最后一个链接,但绝对可以扩展为交互式选择。

# 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

顺便说一句,Alacritty 有一个内置功能:https://wiki.archlinux.org/title/Alacritty#Regex_hints

也可以在 tmux 中使用以下命令完成tmux-urlview插入。

相关内容