Home Explore Blog CI



neovim

25th chunk of `runtime/doc/vimeval.txt`
0be795d790804a604e180b680df55cbc0bcbcc35c53ac5810000000100000fa3
 variables
			  s:	script-local variables
			  l:	local function variables
			  v:	Vim variables.

:let			List the values of all variables.  The type of the
			variable is indicated before the value:
			    <nothing>	String
				#	Number
				*	Funcref


:unl[et][!] {name} ...				*:unlet* *:unl* *E108* *E795*
			Remove the internal variable {name}.  Several variable
			names can be given, they are all removed.  The name
			may also be a |List| or |Dictionary| item.
			With [!] no error message is given for non-existing
			variables.
			One or more items from a |List| can be removed: >
				:unlet list[3]	  " remove fourth item
				:unlet list[3:]   " remove fourth item to last
<			One item from a |Dictionary| can be removed at a time: >
				:unlet dict['two']
				:unlet dict.two
<			This is especially useful to clean up used global
			variables and script-local variables (these are not
			deleted when the script ends).  Function-local
			variables are automatically deleted when the function
			ends.

:unl[et] ${env-name} ...			*:unlet-environment* *:unlet-$*
			Remove environment variable {env-name}.
			Can mix {name} and ${env-name} in one :unlet command.
			No error message is given for a non-existing
			variable, also without !.
			If the system does not support deleting an environment
			variable, it is made empty.

						*:cons* *:const*
:cons[t] {var-name} = {expr1}
:cons[t] [{name1}, {name2}, ...] = {expr1}
:cons[t] [{name}, ..., ; {lastname}] = {expr1}
:cons[t] {var-name} =<< [trim] [eval] {marker}
text...
text...
{marker}
			Similar to |:let|, but additionally lock the variable
			after setting the value.  This is the same as locking
			the variable with |:lockvar| just after |:let|, thus: >
				:const x = 1
<			is equivalent to: >
				:let x = 1
				:lockvar! x
<			This is useful if you want to make sure the variable
			is not modified.  If the value is a List or Dictionary
			literal then the items also cannot be changed: >
				const ll = [1, 2, 3]
				let ll[1] = 5  " Error!
<			Nested references are not locked: >
				let lvar = ['a']
				const lconst = [0, lvar]
				let lconst[0] = 2  " Error!
				let lconst[1][0] = 'b'  " OK
<							*E995*
			It is an error to specify an existing variable with
			|:const|. >
				:let x = 1
				:const x = 1  " Error!
<							*E996*
			Note that environment variables, option values and
			register values cannot be used here, since they cannot
			be locked.

:cons[t]
:cons[t] {var-name}
			If no argument is given or only {var-name} is given,
			the behavior is the same as |:let|.

:lockv[ar][!] [depth] {name} ...			*:lockvar* *:lockv*
			Lock the internal variable {name}.  Locking means that
			it can no longer be changed (until it is unlocked).
			A locked variable can be deleted: >
				:lockvar v
				:let v = 'asdf'	  " fails!
				:unlet v	  " works
<							*E741* *E940* *E1122*
			If you try to change a locked variable you get an
			error message: "E741: Value is locked: {name}".
			If you try to lock or unlock a built-in variable you
			will get an error message "E940: Cannot lock or unlock
			variable {name}".

			[depth] is relevant when locking a |List| or
			|Dictionary|.  It specifies how deep the locking goes:
				0	Lock the variable {name} but not its
					value.
				1	Lock the |List| or |Dictionary| itself,
					cannot add or remove items, but can
					still change their values.
				2	Also lock the values, cannot change
					the items.  If an item is a |List| or
					|Dictionary|, cannot add or remove
					items, but can still change the
					values.
				3	Like 2 but for the |List| /
					|Dictionary| in the |List| /
					|Dictionary|, one level deeper.
			The default [depth] is 2, thus when {name} is a |List|
			or |Dictionary| the values cannot be changed.

			Example with [depth] 0: >
				let mylist = [1, 2, 3]
				lockvar 0 mylist
				let mylist[0] = 77	" OK
				call add(mylist, 4)	" OK
				let mylist = [7, 8, 9]  " Error!
<								*E743*
			For unlimited depth use [!] and

Title: Vim Script: :unlet, :const, and :lockvar
Summary
This section describes the ':unlet' command for removing variables and environment variables. It also introduces the ':const' command, which is similar to ':let' but locks the variable after assignment, preventing modification. The ':lockvar' command allows locking variables at different depths, controlling whether the variable itself and/or its contents (for Lists and Dictionaries) can be changed.