From 64d3f9e53340e115ad8bab1136e7ba6216d1afe2 Mon Sep 17 00:00:00 2001 From: artemave Date: Sun, 30 Jan 2022 14:22:24 +0100 Subject: [PATCH] init --- README.md | 51 +++++++++++++++++++++++++++ plugin.sh | 23 ++++++++++++ tmux_capture_last_command_output.tmux | 12 +++++++ 3 files changed, 86 insertions(+) create mode 100644 README.md create mode 100755 plugin.sh create mode 100755 tmux_capture_last_command_output.tmux diff --git a/README.md b/README.md new file mode 100644 index 0000000..3a8ad2d --- /dev/null +++ b/README.md @@ -0,0 +1,51 @@ +# Tmux Capture Last Command Output + +Capture the output of the last terminal command and open it an editor in a separate tmux window. + +## Installation + +### Using [TPM](https://github.com/tmux-plugins/tpm): + + set -g @plugin 'artemave/tmux_capture_last_command_output' + +Hit prefix + I to fetch and source the plugin. + +### Manual + +Clone the repo: + + git clone https://github.com/artemave/tmux_capture_last_command_output.git ~/.tmux/plugins/tmux_capture_last_command_output + +Source it in your `.tmux.conf`: + + run-shell ~/.tmux/plugins/tmux_capture_last_command_output/tmux_capture_last_command_output.tmux + +Reload TMUX conf by running: + + tmux source-file ~/.tmux.conf + +## Configuration + +#### @command-capture-key + +Required. Set Prefix + key to trigger the plugin. For example, `prefix+t`: + +``` +set -g @command-capture-key t +``` + +#### @command-capture-prompt-pattern + +Required. A regexp to identify command separator. Usually a prompt. E.g., if set to '] % ', the plugin will capture the latest output up until the first line that contains '] % ': + +``` +set -g @command-capture-prompt-pattern '] % ' +``` + +#### @command-capture-editor-cmd + +Optional. An editor to use for the captured output. Defaults to `$EDITOR -`, which works with vim/nvim. Example: + +``` +set -g @command-capture-editor-cmd 'nvim -' +``` diff --git a/plugin.sh b/plugin.sh new file mode 100755 index 0000000..28a4a99 --- /dev/null +++ b/plugin.sh @@ -0,0 +1,23 @@ +#!/usr/bin/env bash + +x=$(tmux capture-pane -p -J -t !) +readarray -t pane_contents <<<"$x" + +# reverse loop through pane contents lines +for (( idx=${#pane_contents[@]}-2 ; idx>=0 ; idx-- )) ; do + line=${pane_contents[idx]} + + # strip trailing whitespace from line + line=$(sed 's/[[:space:]]*$//' <<<"$line") + + if [[ $line =~ $PROMPT_PATTERN ]]; then + break + fi + + # prepend line to result array + result="$line"$'\n'"$result" +done + +EDITOR_CMD=${EDITOR_CMD:-"$EDITOR -"} + +echo "$result" | $EDITOR_CMD diff --git a/tmux_capture_last_command_output.tmux b/tmux_capture_last_command_output.tmux new file mode 100755 index 0000000..42704e1 --- /dev/null +++ b/tmux_capture_last_command_output.tmux @@ -0,0 +1,12 @@ +#!/usr/bin/env bash + +CURRENT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )" + +default_capture_key="t" +CAPTURE_KEY=$(tmux show-option -gqv @command-capture-key) +CAPTURE_KEY=${CAPTURE_KEY:-$default_capture_key} + +PROMPT_PATTERN=$(tmux show-option -gqv @command-capture-prompt-pattern) +EDITOR_CMD=$(tmux show-option -gqv @command-capture-editor-cmd) + +tmux bind $CAPTURE_KEY new-window -n last-command-output -e PROMPT_PATTERN="$PROMPT_PATTERN" -e EDITOR_CMD="$EDITOR_CMD" "$CURRENT_DIR/plugin.sh"