Wikia

Vim Tips Wiki

Changes: Invoke a function with a count prefix

Edit

Back to page

(Comments)
(Change <tt> to <code>, perhaps also minor tweak.)
 
Line 12: Line 12:
 
|category2=
 
|category2=
 
}}
 
}}
Suppose you have a function requiring a count argument, and you would like to be able to invoke it with a mapping, and also allow the count to be prefixed to the mapping, like you can with for example <tt>{count}CTRL-^</tt> (to edit the <tt>[count]</tt>th file in the buffer list).
+
Suppose you have a function requiring a count argument, and you would like to be able to invoke it with a mapping, and also allow the count to be prefixed to the mapping, like you can with for example <code>{count}CTRL-^</code> (to edit the <code>[count]</code>th file in the buffer list).
   
Here is an example of code that can be used to call a <tt>Foo()</tt> function with such a prefix count argument. All it shows is the structure required for such a thing to work (well, it's the best the author of the tip was able to come up with). Given the following, typing <tt>42,a</tt> will echo <tt>FOO: 42</tt>:
+
Here is an example of code that can be used to call a <code>Foo()</code> function with such a prefix count argument. All it shows is the structure required for such a thing to work (well, it's the best the author of the tip was able to come up with). Given the following, typing <code>42,a</code> will echo <code>FOO: 42</code>:
   
 
<pre>
 
<pre>
Line 29: Line 29:
   
 
==Comments==
 
==Comments==
+
It's simpler if you use ":call", then you don't need to use "command". I have something like the following in my vimrc.
It's simpler if you use ":call", then you don't need to use "command". I have something like the following in my vimrc.
+
<pre>
 
 
nmap ,a :<C-U>call Foo(v:count)
 
nmap ,a :<C-U>call Foo(v:count)
 
[...]
 
[...]
Line 36: Line 36:
 
execute "set columns +=" . a:amount
 
execute "set columns +=" . a:amount
 
endfunction
 
endfunction
+
</pre>
--Robert
 

Latest revision as of 05:47, July 13, 2012

Tip 787 Printable Monobook Previous Next

created 2004 · complexity intermediate · author Luc St-Louis · version 5.7


Suppose you have a function requiring a count argument, and you would like to be able to invoke it with a mapping, and also allow the count to be prefixed to the mapping, like you can with for example {count}CTRL-^ (to edit the [count]th file in the buffer list).

Here is an example of code that can be used to call a Foo() function with such a prefix count argument. All it shows is the structure required for such a thing to work (well, it's the best the author of the tip was able to come up with). Given the following, typing 42,a will echo FOO: 42:

function! Foo(count)
  echo 'FOO: ' . a:count
endfunction

command! -nargs=1 FooCmd call Foo(<args>)
map ,a :<C-U>FooCmd(v:count)<CR>

ReferencesEdit

CommentsEdit

It's simpler if you use ":call", then you don't need to use "command". I have something like the following in my vimrc.

   nmap ,a :<C-U>call Foo(v:count)
[...]
   function Foo(amount)
      execute "set columns +=" . a:amount
   endfunction

Around Wikia's network

Random Wiki