Wikia

Vim Tips Wiki

Changes: Scripting - Indirectly Referencing Variables

Edit

Back to page

(Remove html character entities)
(Adjust previous/next navigation + minor manual clean)
 
Line 1: Line 1:
 
{{TipImported
 
{{TipImported
 
|id=794
 
|id=794
|previous=793
+
|previous=792
 
|next=795
 
|next=795
|created=September 20, 2004
+
|created=2004
 
|complexity=advanced
 
|complexity=advanced
 
|author=rja
 
|author=rja
Line 14: Line 14:
   
 
For example:
 
For example:
 
 
<pre>
 
<pre>
 
:let richard_name = "Richard"
 
:let richard_name = "Richard"
Line 24: Line 23:
   
 
Another example:
 
Another example:
 
 
<pre>
 
<pre>
 
:let richard_name = "Richard"
 
:let richard_name = "Richard"
Line 38: Line 36:
   
 
You can also call functions indirectly in the same way. For example:
 
You can also call functions indirectly in the same way. For example:
 
 
<pre>
 
<pre>
 
function! GlobalFunc(pattern,func)
 
function! GlobalFunc(pattern,func)

Latest revision as of 02:27, March 30, 2010

Tip 794 Printable Monobook Previous Next

created 2004 · complexity advanced · author rja · version 5.7


It is possible to indirectly reference a variable using {} (curly braces) in your Vim script. This give you the ability to build up variable names on-the-fly and subsequently reference the data that those variables hold.

For example:

:let richard_name = "Richard"
:let name_pointer = "richard_name"
:echo {name_pointer}

will display the string "Richard" on screen

Another example:

:let richard_name = "Richard"
:let alan_name = "Alan"
:let postfix = "_name"
:let name_pointer = "richard" . postfix
:echo {name_pointer}
:let name_pointer = "alan" . postfix
:echo {name_pointer}

will display "Richard" then "Alan".

You can also call functions indirectly in the same way. For example:

function! GlobalFunc(pattern,func)
  let files = glob(a:pattern)
  while files != ''
    let file = substitute(files,'^\(.\{-}\)\n.*','\1',"")
    let files = strpart(files,strlen(file)+1)
    call {a:func}(file)
  endwhile
endfunction

This function calls a:func with every file matching pattern.

ReferencesEdit

CommentsEdit

Around Wikia's network

Random Wiki