Quantcast

Dynamically name commands in vimscript

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Dynamically name commands in vimscript

jclark42796
I'm trying to dynamically create functions and commands that point to those functions.  e.g. I have a directory:
modules/abc
modules/xyz

I want commands Abc and Xyz that point to functions Abc() and Xyz().  It appears I'm able to generate the function names dynamically, but I'm unable to create the command names dynamically.  With the following, I get an error: E182 Invalid command name

let s:modules = split(glob("modules/*"))

for s:module in s:modules
  let s:module_name = split(s:module, "/")[-1]
  let s:prefix = toupper(s:module_name[0]) . s:module_name[1:]
 
  function! {s:prefix}()
    echo "inside " . s:prefix
  endfunction
  command! {s:prefix} call {s:prefix}()
endfor

I've tried with no luck:
command! s:prefix call {s:prefix}()
command! prefix call {s:prefix}()

What am I missing? Does anyone know how to do this in vimscript if it's possible?

Thanks,
Jim

--
--
You received this message from the "vim_use" maillist.
Do not top-post! Type your reply below the text you are replying to.
For more information, visit http://www.vim.org/maillist.php

---
You received this message because you are subscribed to the Google Groups "vim_use" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Dynamically name commands in vimscript

hermitte
Hi

> let s:modules = split(glob("modules/*"))
>
> for s:module in s:modules
>   let s:module_name = split(s:module, "/")[-1]
>   let s:prefix = toupper(s:module_name[0]) . s:module_name[1:]
>  
>   function! {s:prefix}()
>     echo "inside " . s:prefix
>   endfunction
>   command! {s:prefix} call {s:prefix}()
> endfor
>
> I've tried with no luck:
> command! s:prefix call {s:prefix}()
> command! prefix call {s:prefix}()


You're looking for `:exe`

-> exe 'command! '.s:prefix.' call '.s:prefix.'()'

Regards,
--
Luc Hermitte

--
--
You received this message from the "vim_use" maillist.
Do not top-post! Type your reply below the text you are replying to.
For more information, visit http://www.vim.org/maillist.php

---
You received this message because you are subscribed to the Google Groups "vim_use" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
For more options, visit https://groups.google.com/d/optout.
Loading...