workon environment name tab completion
engin-can opened this issue · comments
I was wondering if you could add environment name tab completion for workon
command. This would help me a lot.
I'd love to, but alas, I don't think it's possible on windows (I'd love to be wrong about this though).
The lsvirtualenv
command will list the available virtualenvs for you, if that helps. Other than that you can always create a batch file that helps you remember, e.g.:
@echo off
lsvirtualenv | grep "%~1" >nul && goto:found
:: not found
lsvirtualenv
goto:eof
:found
:: did we find a single match?
lsvirtualenv | grep "%~1" | wc -l | python -c "import sys;sys.exit(not int(sys.stdin.readline())==1)" && goto:singlematch
goto:multimatch
:singlematch
:: put found env name in wkon.venv
set "tmpvenvfile=%TMP%\venv%RANDOM%.txt"
lsvirtualenv | grep "%~1" > "%tmpvenvfile%"
set /p wkon.venv=< "%tmpvenvfile%"
del /q "%tmpvenvfile%"
choice /M "use this (%wkon.venv%) venv"
:: 1 is yes, 2 is no
if "%ERRORLEVEL%"=="2" goto:eof
:: not using call so the next workon exits
workon %wkon.venv%
:multimatch
lsvirtualenv | grep "%~1"
this uses the gnuwin32 package (highly recommended, http://gnuwin32.sourceforge.net/) for the grep
and wc
command. It lists the matches found for the text you entered, and if there's just one match it asks you if you want to use it. Usage example (I named it wkon.bat
):
(dev) w:\srv> wkon py
npytst
pydeps
pydeps2
pydepstemp
pyicu
pylint634
(dev) w:\srv> wkon pyt
use this (npytst) venv [Y,N]?N
(dev) w:\srv> wkon pyt
use this (npytst) venv [Y,N]?Y
(npytst) go|w:\srv>
Hope this helps.
Could also make a function for this
Function Set-VirtualEnv() {
[CmdletBinding()]
param(
)
DynamicParam {
$virtualenvironments = Get-ChildItem -Path "$env:USERPROFILE\Envs" | Select -ExpandProperty Name
$attributes = New-Object System.Management.Automation.ParameterAttribute
$attributes.ParameterSetName = "__AllParameterSets"
$attributes.Mandatory = $true
$attributes.Position = 0
$attributeCollection = New-Object -Type System.Collections.ObjectModel.Collection[System.Attribute]
$attributeCollection.Add($attributes)
$_Values = $virtualenvironments
$ValidateSet = New-Object System.Management.Automation.ValidateSetAttribute($_Values)
$attributeCollection.Add($ValidateSet)
$dynParam1 = New-Object -Type System.Management.Automation.RuntimeDefinedParameter("Name", [string], $attributeCollection)
$paramDictionary = New-Object -Type System.Management.Automation.RuntimeDefinedParameterDictionary
$paramDictionary.Add("Name", $dynParam1)
return $paramDictionary
}
begin{}
process{
$name = $PSBoundParameters.Name
Invoke-Expression "$env:USERPROFILE\Envs\$name\Scripts\activate.ps1"
}
end{}
}