How to use virtualenv in Vim ?

1,508 views
Skip to first unread message

chris

unread,
Aug 24, 2012, 5:27:48 AM8/24/12
to vim...@googlegroups.com
I added plugin virtualenv (https://github.com/jmcantrell/vim-virtualenv) into my
runtimepath.

I'm new to virtualenv. The Vim plugin virtualenv has a function for statusline
show the virutalenv name. I added it into my statusline option like this:
set statusline+=\ \%{virtualenv#statusline()}
And I also add this virtualenv segment into Powerline statusline.
call Pl#Theme#InsertSegment('virtualenv', 'after', 'hgrev')

I execute some commands:
$ virtualenv test
$ source bin/activate
$ python bin/activate_this.py
$ vim test.py
Then I do not see the virtualenv name in statusline, both original statusline
and Powerline statusline.

Does anybody knows what I should do ? Thanks very much.

--
[ stardiviner ] ^^&^^ {I hate all of you ! Leave me alone}
IRC(freenode): stardiviner \\ Twitter: @numbchild \\
GnuPG Key fingerprint
>>> 9BAA 92BC CDDD B9EF 3B36 CB99 B8C4 B8E5 47C3 2433
signature.asc

Ben Fritz

unread,
Aug 24, 2012, 10:15:20 AM8/24/12
to vim...@googlegroups.com
On Friday, August 24, 2012 4:27:48 AM UTC-5, Pain Nagato wrote:
> I added plugin virtualenv (https://github.com/jmcantrell/vim-virtualenv) into my
>
> runtimepath.
>
>
>
> I'm new to virtualenv. The Vim plugin virtualenv has a function for statusline
>
> show the virutalenv name. I added it into my statusline option like this:
>
> set statusline+=\ \%{virtualenv#statusline()}
>
> And I also add this virtualenv segment into Powerline statusline.
>
> call Pl#Theme#InsertSegment('virtualenv', 'after', 'hgrev')
>
>
>
> I execute some commands:
>
> $ virtualenv test
>
> $ source bin/activate
>
> $ python bin/activate_this.py
>
> $ vim test.py
>
> Then I do not see the virtualenv name in statusline, both original statusline
>
> and Powerline statusline.
>
>
>
> Does anybody knows what I should do ? Thanks very much.
>
>
>

I have no idea what PowerLine does or how it works. Probably it messes with your statusline option. Try disabling PowerLine temporarily to get the statusline working before you start trying to get it working in PowerLine.

I do note that you don't seem to specify the function to call when you pass it into the PowerLine interface, that seems fishy to me. Maybe that's your only issue.

When you have your Vim in a situation where you'd expect something to show in your statusline, try echoing the result of the function manually, with :echo virtualenv#statusline(), to make sure it shows what you'd expect.

Benjamin R. Haskell

unread,
Aug 24, 2012, 11:57:46 AM8/24/12
to vim...@googlegroups.com
On Fri, 24 Aug 2012, chris wrote:

> I added plugin virtualenv
> (https://github.com/jmcantrell/vim-virtualenv) into my runtimepath.
>
> I'm new to virtualenv. The Vim plugin virtualenv has a function for
> statusline show the virutalenv name. I added it into my statusline
> option like this:
> set statusline+=\ \%{virtualenv#statusline()}

You shouldn't set that. Powerline makes the 'statusline' option
irrelevant.


> And I also add this virtualenv segment into Powerline statusline.
> call Pl#Theme#InsertSegment('virtualenv', 'after', 'hgrev')

Powerline already has built-in support for virtualenv. But, if you're
using a non-standard theme or something, your call should be:

call Pl#Theme#InsertSegment('%{virtualenv#statusline()}', 'after', 'hgrev')


> I execute some commands:
> $ virtualenv test
> $ source bin/activate
> $ python bin/activate_this.py
> $ vim test.py
> Then I do not see the virtualenv name in statusline, both original
> statusline and Powerline statusline.

Virtualenv requires a Vim with Python support. Run: :version and look
for +python in the output. If it's not there, virtualenv won't work.
Based on the Powerline condition for including it¹, try (all one line):

:echo has('python') && (exists('g:virtualenv_loaded') && g:virtualenv_loaded == 1)

--
Best,
Ben H

¹: https://github.com/Lokaltog/vim-powerline/blob/develop/autoload/Powerline/Segments/virtualenv.vim

Benjamin R. Haskell

unread,
Aug 24, 2012, 12:10:59 PM8/24/12
to vim...@googlegroups.com
On Fri, 24 Aug 2012, Benjamin R. Haskell wrote:

> On Fri, 24 Aug 2012, chris wrote:
>
>> I added plugin virtualenv (https://github.com/jmcantrell/vim-virtualenv)
>> into my runtimepath.
>>
>> I'm new to virtualenv. The Vim plugin virtualenv has a function for
>> statusline show the virutalenv name. I added it into my statusline option
>> like this:
>> set statusline+=\ \%{virtualenv#statusline()}
>
> You shouldn't set that. Powerline makes the 'statusline' option irrelevant.
>
>
>> And I also add this virtualenv segment into Powerline statusline.
>> call Pl#Theme#InsertSegment('virtualenv', 'after', 'hgrev')
>
> Powerline already has built-in support for virtualenv. But, if you're using
> a non-standard theme or something, your call should be:
>
> call Pl#Theme#InsertSegment('%{virtualenv#statusline()}', 'after', 'hgrev')
>

Also forgot to mention:

Try running :PowerlineClearCache

Or add the following to your .vimrc:

let g:Powerline_cache_enabled = 0


More info for deciding which to use or whether disabling the cache makes
sense at:

:help Powerline_cache_file
:help Powerline_cache_enabled

--
Best,
Ben H

chris

unread,
Aug 25, 2012, 2:14:31 AM8/25/12
to vim...@googlegroups.com
Excerpts from [ Ben Fritz ] //// On [2012-08-24 07:15:20 -0700]:

> I have no idea what PowerLine does or how it works. Probably it messes with your statusline option. Try disabling PowerLine temporarily to get the statusline working before you start trying to get it working in PowerLine.

I do disable Powerline when I use vim option statusline=. That's why I said both
of statusline option and Powerline did not show the virtualenv name on
statusline.

> I do note that you don't seem to specify the function to call when you pass it into the PowerLine interface, that seems fishy to me. Maybe that's your only issue.

Powerline has that support for virtualenv.

> When you have your Vim in a situation where you'd expect something to show in your statusline, try echoing the result of the function manually, with :echo virtualenv#statusline(), to make sure it shows what you'd expect.

I run command `:echo virtualenv#statusline()`, it shows nothing.

> --
> 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
signature.asc

chris

unread,
Aug 25, 2012, 2:20:22 AM8/25/12
to vim...@googlegroups.com
Excerpts from [ Benjamin R. Haskell ] //// On [2012-08-24 11:57:46 -0400]:

> On Fri, 24 Aug 2012, chris wrote:

> >I added plugin virtualenv
> >(https://github.com/jmcantrell/vim-virtualenv) into my runtimepath.

> >I'm new to virtualenv. The Vim plugin virtualenv has a function for
> >statusline show the virutalenv name. I added it into my statusline
> >option like this:
> > set statusline+=\ \%{virtualenv#statusline()}

> You shouldn't set that. Powerline makes the 'statusline' option
> irrelevant.
Because I found Powerline cann't display that in statusline, so I try to test
whether virtualenv#statusline() will show in statusline without Powerline.


> >And I also add this virtualenv segment into Powerline statusline.
> > call Pl#Theme#InsertSegment('virtualenv', 'after', 'hgrev')

> Powerline already has built-in support for virtualenv. But, if
> you're using a non-standard theme or something, your call should be:

> call Pl#Theme#InsertSegment('%{virtualenv#statusline()}', 'after', 'hgrev')


> >I execute some commands:
> > $ virtualenv test
> > $ source bin/activate
> > $ python bin/activate_this.py
> > $ vim test.py
> >Then I do not see the virtualenv name in statusline, both original
> >statusline and Powerline statusline.

> Virtualenv requires a Vim with Python support. Run: :version and
> look for +python in the output. If it's not there, virtualenv won't
> work. Based on the Powerline condition for including it¹, try (all
> one line):

> :echo has('python') && (exists('g:virtualenv_loaded') && g:virtualenv_loaded == 1)
This command shows 1. all of those conditions are satisfied.
> --
> 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

signature.asc

chris

unread,
Aug 25, 2012, 2:24:06 AM8/25/12
to vim...@googlegroups.com
Excerpts from [ Benjamin R. Haskell ] //// On [2012-08-24 12:10:59 -0400]:


> Also forgot to mention:

> Try running :PowerlineClearCache

Yes, I execute this too.
signature.asc

chris

unread,
Aug 25, 2012, 2:29:44 AM8/25/12
to vim...@googlegroups.com
Excerpts from [ Benjamin R. Haskell ] //// On [2012-08-24 12:10:59 -0400]:

solved problem:
reason:
I set g:virtualenv_dir = '~/.virtualenvs/'
But I execute command `$ virtualenv test` in `~/Templates/test/`
That's why Vim plugin virtualenv can't find virtualenv.
signature.asc
Reply all
Reply to author
Forward
0 new messages