]> git.rmz.io Git - dotfiles.git/blobdiff - vim/ultisnips/sh.snippets
vim: add choice snippet for sh
[dotfiles.git] / vim / ultisnips / sh.snippets
index ff10449e6ac55075d7d991191def968cdd6d3ba3..45d5f810452b431759931debb6e552d53ef91039 100644 (file)
@@ -12,6 +12,22 @@ snippet ar "${array[@]}" w
 \${${1:array}[${2:@}]}$0
 endsnippet
 
 \${${1:array}[${2:@}]}$0
 endsnippet
 
+snippet choice "Read a choice from stdin" b
+read -r -p "${1:Question?} [Yn] " choice
+case "$choice" in
+       Y|y) echo "yes" ;;
+       '')  echo "yes" ;;
+       N|n) echo "no"  ;;
+       *)   echo "invalid"
+esac
+endsnippet
+
+snippet readline "Read a file line by line" b
+while ${1:IFS='' }read -r ${2:line} || [[ -n "$$2" ]]; do
+       ${0:echo "Text read from file: $$2"}
+done < "\$${3:file}"
+endsnippet
+
 snippet getopt
 version="${1:0.1}"
 
 snippet getopt
 version="${1:0.1}"
 
@@ -26,7 +42,7 @@ function usage ()
 
 # Parse arguments
 declare -a args
 
 # Parse arguments
 declare -a args
-while [[ $# > 0 ]]; do
+while [[ $# -gt 0 ]]; do
        opt="\$1"
 
        case $opt in
        opt="\$1"
 
        case $opt in
@@ -36,7 +52,7 @@ while [[ $# > 0 ]]; do
                        echo -e "\n  Option does not exist : $opt\n"
                        usage; exit 1 ;;
 
                        echo -e "\n  Option does not exist : $opt\n"
                        usage; exit 1 ;;
 
-               *) args+=($opt); shift ;;
+               *) args+=("$opt"); shift ;;
        esac
 done
 endsnippet
        esac
 done
 endsnippet