Loading ...
Sorry, an error occurred while loading the content.

python scripting

Expand Messages
  • Alex
    Hello there, I m working on python scripting from vim, and I m a little bit stuck here. I can t find a way to call a python function with vim variables on it.
    Message 1 of 2 , Jul 1 8:01 PM
    • 0 Attachment
      Hello there,
      I'm working on python scripting from vim, and I'm a little bit stuck here.

      I can't find a way to call a python function with vim variables on it.
      here's some example

      :let g:foo="Hello World"
      :echo g:foo
      and I get "Hello World"

      now I try
      :let g:foo="Hello World"
      :py print "g:foo"
      and I get "g:foo"
      I've also tried :py print g:foo (without quotes) and no luck

      if you noticed, I want to use a global variable from vim in python
      scripting, but I can't make it work.
      that was a simple example, what I really pretend is call python functions
      with vim variables as arguments to it.
      but well, the point is the same. how do I get g:foo to expand before passing
      it to python?

      I've also tried that example with
      :let @f="Hello World"
      but same thing happens

      Another similar question:

      I try to assign the result of a python function to a vim variable and
      there's no way to get that working either..
      like assiging the return of :py round(3.5) to g:foo or @f or whatever i can
      use from vim then

      I know I can get this one done on the python side by using vim.command() but
      it's not what I want either.


      Please, any suggestions would be great

      Thanks

      alex
    • Dan Sharp
      ... You need to use execute to get your line to work correctly. The pain is having to add quotes around it. For example, ... will do what you want. You
      Message 2 of 2 , Jul 2 7:18 AM
      • 0 Attachment
        At 12:01 AM 7/2/2001 -0300, Alex wrote:
        >if you noticed, I want to use a global variable from vim in python
        >scripting, but I can't make it work.
        >that was a simple example, what I really pretend is call python functions
        >with vim variables as arguments to it.
        >but well, the point is the same. how do I get g:foo to expand before passing
        >it to python?

        You need to use 'execute' to get your line to work correctly. The pain is
        having to add quotes around it. For example,

        :execute "py print \"" . g:foo . "\""

        will do what you want. You can also do

        :let g:foo = "\"Hello World\""

        to include the quotes in the variable, then do

        :execute "py print " . g:foo

        to print the same line.


        >Another similar question:
        >
        >I try to assign the result of a python function to a vim variable and
        >there's no way to get that working either..
        >like assiging the return of :py round(3.5) to g:foo or @f or whatever i can
        >use from vim then
        >
        >I know I can get this one done on the python side by using vim.command() but
        >it's not what I want either.

        I would have to look into this one. Perhaps someone else (Benji?) can
        help. I imagine it is the same as adding the result of any vim command
        into a variable.

        Dan Sharp
      Your message has been successfully submitted and would be delivered to recipients shortly.