-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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