aboutsummaryrefslogtreecommitdiff
path: root/bash/bashrc.d/vim.bash
blob: 27758fd26715d0eaff240ecdeb6678fd30a2ad9a (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# Bail if no vim(1)
if ! hash vim 2>/dev/null; then
    return
fi

# If Vim exists on the system, use it instead of ex, vi, and view
alias ex='vim -e'
alias vi='vim'
alias view='vim -R'

# 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
        if ! file=$(type -P "${cmd##*/}"); then
            mkdir -p "$HOME"/.local/bin || exit
            file="$HOME"/.local/bin/"${cmd##*/}"
        fi
        files=("${files[@]}" "$file")
    done

    # Run Vim with all the options and full path file arguments
    command vim "${opts[@]}" -- "${files[@]}"
}

# Complete the vis function with command names
complete -A command vis