diff options
author | Tom Ryder <tom@sanctum.geek.nz> | 2014-04-14 01:55:02 +1200 |
---|---|---|
committer | Tom Ryder <tom@sanctum.geek.nz> | 2014-04-14 01:55:02 +1200 |
commit | 72b7e3d535f189e51917a6a0cfda6ccc31df421c (patch) | |
tree | 8b989a28a72cbb348deafbab4f494d81a086263d /bash/bashrc.d/vis.bash | |
parent | Require at least one argument to vis function (diff) | |
download | dotfiles-72b7e3d535f189e51917a6a0cfda6ccc31df421c.tar.gz dotfiles-72b7e3d535f189e51917a6a0cfda6ccc31df421c.zip |
Move vis function into its own file
Diffstat (limited to 'bash/bashrc.d/vis.bash')
-rw-r--r-- | bash/bashrc.d/vis.bash | 39 |
1 files changed, 39 insertions, 0 deletions
diff --git a/bash/bashrc.d/vis.bash b/bash/bashrc.d/vis.bash new file mode 100644 index 00000000..bf1129a1 --- /dev/null +++ b/bash/bashrc.d/vis.bash @@ -0,0 +1,39 @@ +# Create or edit a script in $PATH, creating in $HOME/.local/bin +vis() { + local arg cmd file endopts + local -a opts cmds files + + # Distinguish options from file arguments + endopts=0 + for arg in "${@:?}" ; do + if [[ $arg == -- ]] ; then + endopts=1 + elif [[ $arg == -* ]] && ! ((endopts)) ; then + opts=("${opts[@]}" "$arg") + else + cmds=("${cmds[@]}" "$arg") + fi + done + + # Check all the commands, if they don't exist, we'll create them + for cmd in "${cmds[@]}" ; do + file=$(type -p "${cmd##*/}") + if [[ ! $file ]] ; then + if ! mkdir -p "$HOME"/.local/bin ; then + exit + fi + file="$HOME"/.local/bin/"${cmd##*/}" + fi + files=("${files[@]}" "$file") + done + + # Run editor with all the options and full path file arguments + command vim "${opts[@]}" -- "${files[@]}" + + # Attempt to make the new files executable by us + chmod -f -- u+x "${files[@]}" +} + +# Complete the vis function with command names +complete -A command vis + |