Skip to content

Instantly share code, notes, and snippets.

@bwagner
Last active October 12, 2019 10:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bwagner/1eb7336d144e759d0d86f73a438295a9 to your computer and use it in GitHub Desktop.
Save bwagner/1eb7336d144e759d0d86f73a438295a9 to your computer and use it in GitHub Desktop.
REPL below vi, ability to send paragraph from vi to REPL in languages: Python, Perl, JavaScript, Bash
#!/bin/bash
prg=`basename $0`
if [ $# -eq 0 ]; then
echo "Usage: $prg <script>"
exit
else
echo "args $1"
fi
# This script opens a tmux session with a window of two panes, the top of which contains a vi session with the
# script provided on the command line. The bottom pane contains a REPL in the language based on the extension
# of the script name provided.
# To verify functionality, a script language specific string is sent from vi to the REPL,
# the necessary changes to the source file are immediately undone.
#
# PREREQUISITES:
# - IPython (if you want to use Python)
# - Perl (if you want to use Perl)
# - Bash (if you want to use Bash)
# - Node (if you want to use JavaScript)
# - Vi
# - vim-slime (https://github.com/jpalardy/vim-slime)
#
# CONFIGURATION:
# _vimrc needs these lines:
# let g:slime_target = "tmux"
# let g:slime_paste_file = "$HOME/.slime_paste"
# let g:slime_default_config = {"socket_name": "default", "target_pane": "{down-of}"}
# .tmux.conf has these:
# to hide REPL pane:
# bind @ break-pane -t :
# to make REPL pane reappear:
# bind X join-pane -s ! \; select-pane -U
#
# INSPIRATION: https://technotales.wordpress.com/2007/10/03/like-slime-for-vim/
# TODO: bash needs a proper REPL. (https://stackoverflow.com/questions/19728253/how-do-i-get-a-repl-in-bash#comment-90342849)
# TODO: Figure out how to avoid arbitrary sleeps by wait-for
# (https://unix.stackexchange.com/questions/144663/have-tmux-wait-until-i-ssh-in-to-complete-rest-of-tmuxinator-script)
SESSION=${1%.*}
echo session: $SESSION
ext="${1#*.}"
echo ext: $ext
welcome_str="*****************************\n*** $prg working! ***\n*****************************"
default_expr="2+3" # works for python, js, and perl
if [ "$ext" == "py" ]; then
REPL='ipython'
LANGUAGE='Python'
expression="print('$welcome_str')"
elif [ "$ext" == "js" ]; then
REPL='node'
LANGUAGE='JavaScript'
expression="console.log('$welcome_str')"
elif [ "$ext" == "pl" ]; then
REPL="rlwrap -A -pgreen -S\"perl> \" perl -wnE'say eval()//\$@'"
LANGUAGE='Perl'
expression="\"$welcome_str\""
elif [ "$ext" == "sh" ]; then
LANGUAGE='Bash'
REPL='bash'
expression="echo -e \"$welcome_str\""
fi
if [ "$expression" == "" ] ; then
expression=$default_expr
fi
echo "expression: $expression"
tmux -2 new-session -d -s $SESSION -n "Hacking $LANGUAGE $1"
tmux split-window
tmux select-pane -t 2
tmux send-keys "$REPL" C-m
tmux select-pane -t 1
sleep 1
# O Escape: open new line,
# i Escape: get rid of indent,
# yy p k yy p: surround with empty lines to make paragraph
# i$expression: Enter expression
tmux send-keys "vi $1" C-m O Escape i Escape yy p k yy p "i$expression" Escape
sleep 2
tmux send-keys C-c C-c C-m C-m
tmux send-keys 4u
tmux -2 attach-session -t $SESSION
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment