sponsor Vim development Vim logo Vim Book Ad

getVar.vim : Convenient retrieval of buffer or global script options.

 script karma  Rating 2/2, Downloaded by 1902  Comments, bugs, improvements  Vim wiki

created by
Salman Halim
script type
These are just a few convenience functions that are useful for retrieval of plugin options that can be set globally as well as having buffer-specific over-rides.

Example:  I have a global variable called "g:sortOrder" which is set to "a".  In the test.java buffer, I create a buffer variable called "b:sortOrder" which is set to "d".  Now, GetVar#GetVar( "sortOrder" ) will return "d" when I'm in test.java and "a" everywhere else.  My plugin code doesn't need to actually check for buffer or global variables; I can just call GetVar#GetVar and be done with it.

Why is this useful?  It mimics the behaviour of options.  For example, :set grepprg? will return the value of 'grepprg'; if a local version has been set (:setlocal grepprg=<something>), it will quietly return that.  Otherwise, it will return the global value.  GetVar#GetVar allows the same behaviour for variables.

GetVar#GetVar will take an optional second parameter which will be the default value to return if neither a buffer nor a global variable by the specified name is found (the default default :) is -1).

Example:  GetVar#GetVar( "userID", "NONE" ) will take either a userID or the string "NONE" if there isn't one.  This can be useful where a script should just use an assumed default if the user hasn't specified an override (global or otherwise).

GetVar#VarExists simply returns true if any of the scopes used by GetVar#GetVar exist (in the same order of checking as GetVar#GetVar).
install details
Pop it into the autload directory and use in your plugins -- just make sure to add this as a dependence when you submit your plugin to this site.

rate this script Life Changing Helpful Unfulfilling 
script versions (upload new version)

Click on the package to download.

package script version date Vim version user release notes
GetVar.vim 2.0 2011-04-23 7.0 Salman Halim Made the whole thing into an autoload script, prefixing the names of all the functions with GetVar (the name of the script); so, the new function names are:

GetVar.vim 1.4 2010-03-18 6.0 Salman Halim Added two new functions: GetSafe and Allocate.
GetVar.vim 1.3 2009-03-06 6.0 Salman Halim Changed the plugin slightly to allow for a default value to continue working if the regular return value is a list (or dictionary; basically, not a simple
scalar value). Then change, while bringing the plugin up to scratch with Vim 7.x, doesn't break Vim 6.x compatibility.
getVar.vim 1.2 2006-03-31 7.0 Salman Halim Updated to use Vim 7's t: (tab-specific) variables once buffer-specific variables aren't found.
The new sequence is window -> buffer -> tab -> global.  (Just stick with 1.1 if not using Vim 7.)
getVar.vim 1.1 2003-09-22 6.0 Salman Halim Updated to return window-specific variables, THEN buffer and then global variables.
getVar.vim 1.0 2002-07-26 6.0 Salman Halim Initial upload
ip used for rating:

If you have questions or remarks about this site, visit the vimonline development pages. Please use this site responsibly.
Questions about Vim should go to the maillist. Help Bram help Uganda.