Skip to content

Instantly share code, notes, and snippets.

@ccooke
Last active August 29, 2015 14:13
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ccooke/5f5ca948c679fce15c4d to your computer and use it in GitHub Desktop.
Save ccooke/5f5ca948c679fce15c4d to your computer and use it in GitHub Desktop.
A vim wrapper to automatically open files in vsplit in the correct order, passing through arguments correctly
#!/bin/bash
PARAM_OPTS=( )
function get_param_opts {
(( ${#PARAM_OPTS[@]} > 0 )) && return
PARAM_OPTS=( $( vim -h | sed -n -e '/^ *-[-a-z]* </s/ <.*$//p' ) )
}
vim_args=""
files=( )
accept_args=1
while [[ $1 ]]; do
if (( $accept_args )); then
case $1 in
--)
vim_args="$vim_args $1"
accept_args=0
;;
-*)
vim_args="$vim_args $1"
get_param_opts
if [[ ' '${PARAM_OPTS[@]}' ' == *' '$1' '* ]]; then
# this command takes an argument
vim_args="$vim_args $(printf "%q" "$2")"
shift 1
fi
;;
*)
accept_args=0
files[${#files[@]}]="$1"
;;
esac
else
files[${#files[@]}]="$1"
fi
shift 1
done
for (( i = ${#files[@]} - 2; i >= 0; i-- )); do
vim_args="${vim_args} -c ':vsp $(printf "%q" "${files[i]}")'"
done
if (( ${#files[@]} )); then
vim_args="${vim_args} $(printf "%q" ${files[${#files[@]}-1]})"
fi
sh -c "vim $vim_args"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment