This commit is contained in:
artemave 2022-01-30 14:22:24 +01:00
parent 6c2e74db29
commit 64d3f9e533
3 changed files with 86 additions and 0 deletions

51
README.md Normal file
View file

@ -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 <kbd>prefix</kbd> + <kbd>I</kbd> 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 -'
```

23
plugin.sh Executable file
View file

@ -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

View file

@ -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"