Skip to content

Instantly share code, notes, and snippets.

@stephenmm
Created December 8, 2018 01:40
Show Gist options
  • Save stephenmm/1f33d0b60b52653693da36411e8e0c22 to your computer and use it in GitHub Desktop.
Save stephenmm/1f33d0b60b52653693da36411e8e0c22 to your computer and use it in GitHub Desktop.
More extensive way to open files from your clippboard. Supports a file per line open in there own tab.
function gvim_arg() {
local clip servername && local $*; # declare possible named variables
local file_chars clip_file_check extra_stuff line_num find_str cmds cmd cmd1 cmd2
export cmds=()
export file_chars='[-a-zA-Z0-9\$\.\/_]'
#(>&2 echo "Clip: $clip" )
# Get rid of non-file name charecters
clip_file_check=$( echo "$clip" | perl -ne '/($ENV{"file_chars"}+)/ && print("$1");' )
extra_stuff=$( echo "$clip" | perl -ne '/$ENV{"file_chars"}+(.*)/ && print("$1");' )
if [[ $extra_stuff =~ ^(:[0-9]+) ]]; then
cmds+=( "--remote-send \"${BASH_REMATCH[1]}<CR>\" " )
else
#Remove leading colon if exists
[[ $extra_stuff =~ ^(:)?(.+) ]] && extra_stuff="${BASH_REMATCH[2]}"
extra_stuff=$( echo $extra_stuff | sed "s/\s\+/ /g" )
extra_stuff=$( echo $extra_stuff | sed "s/^\s\+//g" )
extra_stuff=$( echo $extra_stuff | sed "s/[^a-zA-Z0-9_\-]/./g" )
[[ "$extra_stuff" != '' ]] && cmds+="--remote-send \"/$extra_stuff<CR>\" --remote-send \":silent normal n<CR>\" "
fi
# expand any variables
clip_file_check=$( eval echo -e "$clip_file_check" )
cmd="gvim --servername $servername --remote-tab \"$clip_file_check\" "
if [[ -e $clip_file_check ]]; then
(>&2 echo -e "[GVIM_ARG]:$cmd")
eval "$cmd" &
sleep 1
for cmd1 in $cmds; do
cmd2="gvim --servername $servername $cmd1"
echo "[GVIM_ARG]: $cmd2"
eval "$cmd2" &
done
else
(>&2 echo "[gvim_arg] no file found in clipboard $clip_file_check $clip")
temp_file=$( mktemp )
echo -e "$clip" >> $temp_file
gvim --servername $servername --remote-tab $temp_file &
fi
}
function gvim_clip() {
local clip servername old_field_sep
clip="${1-$( get_clip )}" # Use argument for testing only otherwise call get_clip
servername=GVIM_CLIP_$(date +%s)
gvim --servername $servername
(>&2 echo -e "[GVIM_CLIP]:gvim --servername $servername")
old_field_sep="$IFS"
IFS=$'\n'
for item in $( clip ); do
#echo "Item: $item"
gvim_arg "clip=$item" "servername=$servername"
done
IFS="$old_field_sep"
}
alias gg=gvim_clip
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment