Skip to content

Instantly share code, notes, and snippets.

@harjoc
Last active February 28, 2021 02:07
Show Gist options
  • Save harjoc/98d56def6011f5de7ea1f1900a912e5f to your computer and use it in GitHub Desktop.
Save harjoc/98d56def6011f5de7ea1f1900a912e5f to your computer and use it in GitHub Desktop.
Pasting output from previous bash command as arguments
# Paste this gist into .bashrc and add this line to .vimrc:
# xnoremap <CR> y:call writefile(getreg('', 1, 1), $HOME."/.yank") <Bar> quit <CR>
# Now you can select text using 'v' and hit 'Enter' to add it to the bash command.
bind -x '"\C-g":"__yank_from_output"'
__yank_from_output()
{
rm -f ~/.yank
vim ~/.output +$
[ -r ~/.yank ] || return
local yank=$(<~/.yank)
local len=${#yank}
READLINE_LINE="${READLINE_LINE:0:READLINE_POINT}${yank}${READLINE_LINE:READLINE_POINT}"
READLINE_POINT=$((READLINE_POINT+len))
}
__tee() { "$@" 2>&1 | tee ~/.output; }
__save()
{
case "$1" in
git)
case "$2" in
status | branch | tag | push)
__tee "$@" ;;
*)
"$@" ;;
esac ;;
apt | apt-cache)
case "$2" in
search | show)
__tee "$@" ;;
*)
"$@" ;;
esac ;;
*)
__tee "$@" ;;
esac
}
for a in git apt apt-cache ifconfig ip route netstat ps dpkg arp curl wget youtube-dl; do
alias $a="__save $a"
done
@harjoc
Copy link
Author

harjoc commented Feb 28, 2021

asciicast

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment