Vim Tips Wiki
Tip 895 Printable Monobook Previous Next

created 2005 · complexity advanced · author Charles E. Campbell, Jr. · version 6.0

This tip concerns the interplay between commands, maps, and functions. Please consider the following test script... To run the script, you'll need Decho.vim and cecutil.vim

This tip is aimed for those who wish to write scripts in vimL

fun! TestOne(reg,bang,args) range
  call Dfunc("TestOne(reg<".a:reg.">,bang=".a:bang." q-args=".a:args.") firstline=".a:firstline." lastline=".a:lastline)
  call Dret("TestOne")

fun! TestTwo(reg,bang,qty,args)
  call Dfunc("TestTwo(reg<".a:reg.">,bang=".a:bang." qty=".a:qty." q-args=".a:args.")")
  call Dret("TestTwo")

" this is a test range
" this is a test range
" this is a test range
" this is a test range

com! -range -register -bang TestOne <line1>,<line2>call TestOne("<reg>",<bang>0,<q-args>)
com! -count=1 -register -bang TestTwo call TestTwo("<reg>",<bang>0,<count>,<q-args>)
nnoremap \aa :TestOne<CR>
nnoremap \bb :TestOne!<CR>
nnoremap \cc :TestOne a<CR>
nnoremap \dd :TestOne! a<CR>
nnoremap \ee :TestOne! a "abc" "def"<CR>
nnoremap \ff :10,13TestOne<CR>
nnoremap \gg :TestTwo 3<CR>

\aa : Using a map to call TestOne(). When no count (ie. 5\aa) is provided, the only thing interesting is that firstline and lastline are equal to the current line.

5\aa : Like \aa, but this time lastline= firstline+5-1. Thus one can pass a repetition count of sorts to the map. However, one will get an "Invalid range" error within four lines of the end-of-file.

\bb : How to pass the optional "!" to a function. Useful for having an alternate behavior associated with a function.

\cc : Illustrates the use of <reg> -- note that the first argument

\dd : is made available by the <reg>, and is elided from the arguments. The

\ee : \ee map shows how arguments are still available.

\ff : Shows how to pass a fixed range to a function. Here the <line1>,<line2> construct is being illustrated

\gg : Illustrates the use of <count>.