Skip to content

Instantly share code, notes, and snippets.

@fcard
Created February 27, 2017 01:04
Show Gist options
  • Save fcard/f1e5fbc7695b77743e2fce7e5905308f to your computer and use it in GitHub Desktop.
Save fcard/f1e5fbc7695b77743e2fce7e5905308f to your computer and use it in GitHub Desktop.
Optional arguments hack in Vimscript
function OptLets(name,default)
let a:let1 = "let a:__OPT_ARGNUM__ = get(a:, '__OPT_ARGNUM__', 0)+1"
let a:let2 = "let a:".a:name." = get(a:, a:__OPT_ARGNUM__, ".a:default.")"
return a:let1."\n".a:let2
endfunction
command -nargs=+ Optional execute OptLets(<f-args>)
function Fx(a,b,...)
Optional c 10
Optional d 20
echo a:a
echo a:b
echo a:c
echo a:d
endfunction
function! TheKnownFunction(arg1,...)
Optional arg2 0
Optional arg3 0
echo "and here goes the first argument !"
if a:arg2
echo "you supplied the second argument !!"
if a:arg3
echo "you went beyond all expectations !!!"
return "5/5"
else
return "4/5"
endif
else
echo "that's it?? :("
return "2/5"
endif
endfunction
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment