I want to organize my vimrc better, so I want to put all the custom functions at bottom:different setting between different osif MyDetectOS() ==? windows    set rtp+=~/vimfiles/bundle/Vundle.vim/    let path='~/vimfiles/bundle'else     set rtp+=~/.vim/bundle/Vundle.vimendifif MyDetectOS() ==? linux     set clipboard=unnamedpluselse    set clipboard=unnamedendiffunction! MyDetectOS()    if has(win32) || has(win16) || has(win64)        return windows    elseif has(mac) || has(macunix)        return mac    elseif has(win32unix)        return cygwin    elseif has(unix)        let l:uname = substitute(system(uname), '\n', '', '')        if l:uname ==? linux            return linux        else            return unix        endif    endifendfunctionIf I put the MyDetectOS function at bottom like above. Then I'll get errors like below:E117: Unknown function: MyDetectOSE15: Invalid expression: MyDetectOS() ==? linuxWhy it doesn't work?
How to put custom functions at bottom?
vimscript;vimrc;functions
The way to understand this is that a function decleration is just an assignment, similar to x = 2.So when you do:function! MyDetectOS()    [...]endfunctionYou're really just doing:MyDetectOS = function!    [...]endfunctionSo it doesn't work for the same reason that something like this doesn't work:a = 40 + bb = 2The reason that it does work in some languages, is that those languages use a more advanced parser; they will first look for all functions and assign references to them, and then do a second pass where the code is actually executed with those references in place. Note that neither method can be considered better than the other; the simpler method is more flexible in some ways and suited for functional programming, the more complex allows you to not worry so much about the order of code and is somewhat less surprising for some.One technique that you can use to get around this is put most code in a function, and kick that off at the end:function! Main()    if MyDetectOS() ==? windows        [...]    endifendfunctionfunction! MyDetectOS()    [...]endfunctionMain()This way the entire file is read, before the actual code is being executed.An alternative method putting your functions in a seperate file, and then use source to include that:source ~/.vim/functions.vimif MyDetectOS() ==? windows    [...]endif