Skip to main content
Module

x/denops_std/function/common.ts>exists

📚 Standard module for denops.vim
Go to Latest
function exists
import { exists } from "https://deno.land/x/denops_std@v4.1.4/function/common.ts";

The result is a Number, which is |TRUE| if {expr} is defined, zero otherwise.

Note: In a compiled |:def| function the evaluation is done at runtime. Use exists_compiled() to evaluate the expression at compile time.

For checking for a supported feature use |has()|. For checking if a file exists use |filereadable()|.

The {expr} argument is a string, which contains one of these: varname internal variable (see dict.key |internal-variables|). Also works list[i] for |curly-braces-names|, |Dictionary| import.Func entries, |List| items, imported items, etc. Does not work for local variables in a compiled :def function. Also works for a function in |Vim9| script, since it can be used as a function reference. Beware that evaluating an index may cause an error message for an invalid expression. E.g.: :let l = [1, 2, 3] :echo exists("l[5]") 0 :echo exists("l[xx]") E121: Undefined variable: xx 0 &option-name Vim option (only checks if it exists, not if it really works) +option-name Vim option that works. $ENVNAME environment variable (could also be done by comparing with an empty string) *funcname built-in function (see |functions|) or user defined function (see |user-functions|) that is implemented. Also works for a variable that is a Funcref. ?funcname built-in function that could be implemented; to be used to check if "funcname" is valid :cmdname Ex command: built-in command, user command or command modifier |:command|. Returns: 1 for match with start of a command 2 full match with a command 3 matches several user commands To check for a supported command always check the return value to be 2. :2match The |:2match| command. :3match The |:3match| command (but you probably should not use it, it is reserved for internal usage) #event autocommand defined for this event #event#pattern autocommand defined for this event and pattern (the pattern is taken literally and compared to the autocommand patterns character by character) #group autocommand group exists #group#event autocommand defined for this group and event. #group#event#pattern autocommand defined for this group, event and pattern. ##event autocommand for this event is supported.

Examples: exists("&shortname") exists("$HOSTNAME") exists("strftime") exists("s:MyFunc") " only for legacy script exists("MyFunc") exists("bufcount") exists(":Make") exists("#CursorHold") exists("#BufReadPre#.gz") exists("#filetypeindent") exists("#filetypeindent#FileType") exists("#filetypeindent#FileType#") exists("##ColorScheme") There must be no space between the symbol (&/$/ /#) and the name. There must be no extra characters after the name, although in a few cases this is ignored. That may become stricter in the future, thus don't count on it! Working example: exists(":make") NOT working example: exists(":make install")

Note that the argument must be a string, not the name of the variable itself. For example: exists(bufcount) This doesn't check for existence of the "bufcount" variable, but gets the value of "bufcount", and checks if that exists.

Can also be used as a |method|: Varname()->exists()

Parameters

denops: Denops
expr: string

Returns

Promise<boolean>