Skip to main content
added 2 characters in body
Source Link
Kusalananda
  • 355.8k
  • 42
  • 735
  • 1.1k
vim () {
    dashdash=false

    for arg do
        use_arg=false 

        case $arg in
            --)
                dashdash=true
                use_arg=true
                ;;
            -*)
                if "$dashdash"; then
                    [ -e "$arg" ] && use_arg=true
                else
                    use_arg=true
                fi
                ;;
            *)
                [ -e "$arg" ] && use_arg=true
        esac

        if "$use_arg"; then
            set -- "$@" "$arg"
        else
            printf 'No such file or directory (skipping): %s\n' "$arg" >&2
        fi

        shift
    done

    unset arg dashdash use_arg

    command vim "$@"
}
vim () {
    dashdash=false

    for arg do
        use_arg=false
        case $arg in
            --)
                dashdash=true
                use_arg=true
                ;;
            -*)
                if "$dashdash"; then
                    [ -e "$arg" ] && use_arg=true
                else
                    use_arg=true
                fi
                ;;
            *)
                [ -e "$arg" ] && use_arg=true
        esac

        if "$use_arg"; then
            set -- "$@" "$arg"
        else
            printf 'No such file or directory (skipping): %s\n' "$arg" >&2
        fi

        shift
    done

    unset arg dashdash use_arg

    command vim "$@"
}
vim () {
    dashdash=false

    for arg do
        use_arg=false 

        case $arg in
            --)
                dashdash=true
                use_arg=true
                ;;
            -*)
                if "$dashdash"; then
                    [ -e "$arg" ] && use_arg=true
                else
                    use_arg=true
                fi
                ;;
            *)
                [ -e "$arg" ] && use_arg=true
        esac

        if "$use_arg"; then
            set -- "$@" "$arg"
        else
            printf 'No such file or directory (skipping): %s\n' "$arg" >&2
        fi

        shift
    done

    unset arg dashdash use_arg

    command vim "$@"
}
deleted 24 characters in body
Source Link
Kusalananda
  • 355.8k
  • 42
  • 735
  • 1.1k
vim () {
    passthrough=falsedashdash=false

    for arg do
        use_arg=false
        case $arg in
            --)
                passthrough=truedashdash=true
                use_arg=true
                ;;
            -*)
                if "$passthrough";"$dashdash"; then
                    [ -e "$arg" ] && use_arg=true
                else
                    use_arg=true
                fi
                ;;
            *)
                [ -e "$arg" ] && use_arg=true
        esac

        if "$use_arg"; then
            set -- "$@" "$arg"
        else
            printf 'No such file or directory (skipping): %s\n' "$arg" >&2
        fi

        shift
    done
 
    printf '%s\n' "$@"
    unset arg passthroughdashdash use_arg

    command vim "$@"
}
vim () {
    passthrough=false

    for arg do
        use_arg=false
        case $arg in
            --)
                passthrough=true
                use_arg=true
                ;;
            -*)
                if "$passthrough"; then
                    [ -e "$arg" ] && use_arg=true
                else
                    use_arg=true
                fi
                ;;
            *)
                [ -e "$arg" ] && use_arg=true
        esac

        if "$use_arg"; then
            set -- "$@" "$arg"
        else
            printf 'No such file or directory (skipping): %s\n' "$arg" >&2
        fi

        shift
    done
 
    printf '%s\n' "$@"
    unset arg passthrough use_arg

    command vim "$@"
}
vim () {
    dashdash=false

    for arg do
        use_arg=false
        case $arg in
            --)
                dashdash=true
                use_arg=true
                ;;
            -*)
                if "$dashdash"; then
                    [ -e "$arg" ] && use_arg=true
                else
                    use_arg=true
                fi
                ;;
            *)
                [ -e "$arg" ] && use_arg=true
        esac

        if "$use_arg"; then
            set -- "$@" "$arg"
        else
            printf 'No such file or directory (skipping): %s\n' "$arg" >&2
        fi

        shift
    done

    unset arg dashdash use_arg

    command vim "$@"
}
added 1248 characters in body
Source Link
Kusalananda
  • 355.8k
  • 42
  • 735
  • 1.1k

To also handle possible options that a user may pass to Vim, one would have to parse the command line arguments and remove the arguments that are filename operands that corresponds to non-existing names:

vim () {
    passthrough=false

    for arg do
        use_arg=false
        case $arg in
            --)
                passthrough=true
                use_arg=true
                ;;
            -*)
                if "$passthrough"; then
                    [ -e "$arg" ] && use_arg=true
                else
                    use_arg=true
                fi
                ;;
            *)
                [ -e "$arg" ] && use_arg=true
        esac

        if "$use_arg"; then
            set -- "$@" "$arg"
        else
            printf 'No such file or directory (skipping): %s\n' "$arg" >&2
        fi

        shift
    done

    printf '%s\n' "$@"
    unset arg passthrough use_arg

    command vim "$@"
}

The only tricky bit here is that an -- argument on the command line signals the end of options. This means that every argument after -- needs to be tested as a filename, whereas while we haven't seen a -- we can skip the -e test on anything that starts with a dash.

This function would open the editor with a blank buffer if no existing filenames were given.


To also handle possible options that a user may pass to Vim, one would have to parse the command line arguments and remove the arguments that are filename operands that corresponds to non-existing names:

vim () {
    passthrough=false

    for arg do
        use_arg=false
        case $arg in
            --)
                passthrough=true
                use_arg=true
                ;;
            -*)
                if "$passthrough"; then
                    [ -e "$arg" ] && use_arg=true
                else
                    use_arg=true
                fi
                ;;
            *)
                [ -e "$arg" ] && use_arg=true
        esac

        if "$use_arg"; then
            set -- "$@" "$arg"
        else
            printf 'No such file or directory (skipping): %s\n' "$arg" >&2
        fi

        shift
    done

    printf '%s\n' "$@"
    unset arg passthrough use_arg

    command vim "$@"
}

The only tricky bit here is that an -- argument on the command line signals the end of options. This means that every argument after -- needs to be tested as a filename, whereas while we haven't seen a -- we can skip the -e test on anything that starts with a dash.

This function would open the editor with a blank buffer if no existing filenames were given.

Source Link
Kusalananda
  • 355.8k
  • 42
  • 735
  • 1.1k
Loading