Sunday, September 16, 2012

Re: Putting stdout from Python into vim

On 16/09/12 19:56, Simon W. Jones wrote:
> On Sunday, September 16, 2012 5:32:09 PM UTC+1, Simon W. Jones wrote:
>> Hello,
>>
>> I am trying to capture the stdout (a simple digit) from a python script that I am calling from vim and assign it to a variable. I have been trying redir but can't seem to get it to work.
>>
>> Any help would be appreciated!
>>
>> Thanks.
>>
>> Simon.
>
> Many thanks for the many answers, unfortunately I am still struggling.
>
> The python script ends with:
>
> sys.stdout.write(refs[int(chosen[1])][0])
>
> When I run the script with %!nameofscript the correct answer is replaces the content of the current buffer. I still can't seem able to pipe it into a variable. Could it be because the python script gets user input from a dialog box that they have to select by pressing Enter?
>
> Thanks again for all your assistance.
>
> Simon.
>

%!nameofscript runs the script as a filter, i.e., its output replaces
the current buffer. This is intentional: for instance :2,$-1!sort uses
the external "sort" program to sort all lines of the current buffer
except the first and last. (It feeds lines 2 to $-1 to the stdin of
sort, and replaces them with its stdout.)

Use instead
:update | let variablename = system('nameofscript < ' .
shellescape(expand('%'),1))

Or if your script doesn't require to get your editfile as input, just use
:let variablename = system('nameofscript')

Or if your script can get its data from vim.current.range (after
importing vim), then

:redir => variablename
:%pyfile nameofscript
:redir END


Best regards,
Tony.
--
Keep Cool, but Don't Freeze
- Hellman's Mayonnaise

--
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

No comments: