Putting stdout from Python into vim

64 views
Skip to first unread message

Simon W. Jones

unread,
Sep 16, 2012, 12:32:09 PM9/16/12
to vim...@googlegroups.com
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.

Marc Weber

unread,
Sep 16, 2012, 12:39:25 PM9/16/12
to vim_use

let var = system('python script.py')

If you're using the :py command you have to lookup how to redirect
stdout within python if using redir doesn't work.

Marc Weber

Tony Mechelynck

unread,
Sep 16, 2012, 1:07:11 PM9/16/12
to vim...@googlegroups.com, Simon W. Jones
Unless your Python script is using the Vim-Python interface (using
"import vim" etc.) you might run it independently:

:let py_retval = system('python mynicescript.py')

see :help system()


Or you might try to capture the stdout by means of the :redir statement
(see ":help :redir") but I don't know if it works:

(untested)
:redir => py_retval
:pyfile mynicescript.py
:redir END
:echo 'Result = �' . py_retval . '�'

Also, this might add ends-of-lines before and/or after your "single
digit" answer.


Best regards,
Tony.
--
We are all in the gutter, but some of us are looking at the stars.
-- Oscar Wilde

Timothy Madden

unread,
Sep 16, 2012, 1:07:28 PM9/16/12
to vim...@googlegroups.com
Redir worked just fine for me

:redir @a
:py import sys
:py print 2
:redir END
:echo @a

The code will display "2" preceded and followed by few new-lines

Timothy Madden

Simon W. Jones

unread,
Sep 16, 2012, 1:56:34 PM9/16/12
to vim...@googlegroups.com

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.

Tony Mechelynck

unread,
Sep 16, 2012, 2:29:43 PM9/16/12
to vim...@googlegroups.com, Simon W. Jones
%!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

Simon W. Jones

unread,
Sep 16, 2012, 2:57:11 PM9/16/12
to vim...@googlegroups.com
On Sunday, September 16, 2012 5:32:09 PM UTC+1, Simon W. Jones wrote:

Brilliant. Thanks very much Tony. I've made sure your help is recognised in the vimscript!

Marc Weber

unread,
Sep 16, 2012, 3:08:11 PM9/16/12
to vim_use
> Many thanks for the many answers, unfortunately I am still struggling.
> sys.stdout.write(refs[int(chosen[1])][0])
What about rereading what I wrote earlier?

Marc Weber

Simon W. Jones

unread,
Sep 16, 2012, 3:30:53 PM9/16/12
to vim...@googlegroups.com
I read and tried it Marc but it didn't work for me. Thanks for responding.

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


--
---
Simon W. Jones

Marc Weber

unread,
Sep 16, 2012, 4:20:09 PM9/16/12
to vim_use
Excerpts from Simon W. Jones's message of Sun Sep 16 21:30:53 +0200 2012:
> I read and tried it Marc but it didn't work for me. Thanks for responding.
Hard to believe. Do you want to capture stderr?
let var = system('python script.py 2>&1') is the way to go then.

What else can go wrong this way?

Marc Weber

Simon W. Jones

unread,
Sep 16, 2012, 4:37:24 PM9/16/12
to vim...@googlegroups.com
Marc,

The solution that worked was the one provided by Tony, as I mentioned in my reply to him. It was:

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

I did try your suggestion (as I've already written).

-------------
Simon W. Jones.

Marc Weber

unread,
Sep 16, 2012, 4:40:42 PM9/16/12
to vim_use
Excerpts from Simon W. Jones's message of Sun Sep 16 22:37:24 +0200 2012:
> :update | let variablename = system('nameofscript < ' . shellescape(expand('%'),1))
system('nameofscript', join(getline(1,'$'),"\n"))

would be an alternative if you don't care about kind of line ending,
then you don't have to write the buffer.

I should have read all messages, I agree.

Marc Weber

Timothy Madden

unread,
Sep 16, 2012, 6:32:56 PM9/16/12
to vim...@googlegroups.com
On 09/16/2012 11:37 PM, Simon W. Jones wrote:
> Marc,
>
> The solution that worked was the one provided by Tony, as I mentioned in my reply to him. It was:
>
> :update | let variablename = system('nameofscript < ' . shellescape(expand('%'),1))
>
> I did try your suggestion (as I've already written).
>
> -------------
> Simon W. Jones.
>
> On 16 Sep 2012, at 21:20, Marc Weber <marco-...@gmx.de> wrote:
>
>> Excerpts from Simon W. Jones's message of Sun Sep 16 21:30:53 +0200 2012:
>>> I read and tried it Marc but it didn't work for me. Thanks for responding.
>> Hard to believe. Do you want to capture stderr?
>> let var = system('python script.py 2>&1') is the way to go then.
>>
>> What else can go wrong this way?

It looks to me what you actually needed was how to get the right input
for the python script.

Timothy Madden

Tony Mechelynck

unread,
Sep 16, 2012, 8:54:09 PM9/16/12
to vim...@googlegroups.com, Timothy Madden
Well, at first you didn't mention what kind of input your script needed;
I had to guess it from your mention of :%!nameofscript in a later post.


Best regards,
Tony.
--
With sufficient thrust, pigs fly just fine.
-- RFC 1925

Reply all
Reply to author
Forward
0 new messages