Export miniconda2's environments to miniconda3 - bash

I used miniconda2 but I had to upgrade to miniconda3. However, how can export miniconda2's environments to miniconda3?
Thank you in advance,
UPDATE
I found here the below script:
for env in $(conda env list | cut -d" " -f1); do
if [[ ${env:0:1} == "#" ]] ; then continue; fi;
conda env export -n $env > ${env}.yml
done
It only picks up the new miniconda3 environments and not the old miniconda2 which are located in a different folder.
> ls -1 /work/miniconda2/envs/
3d-dna
abyss
afterqc
busco4
...
(base)> conda activate /work//miniconda2/envs/busco4
(busco4)>
How can I modify the above script to export from miniconda2 folder?

I'm having a hard time finding a conda YML file that's that old. There's no foolproof way that I'm aware of but here is a way that should satisfice the solution.
Note: This is for linux or mac; you can use findstr for Windows. Or PowerShell.
From your environment:
conda env export --from-history | grep -v "prefix" > your_environment_name.yml
You can view the contents with cat environment.yml. This gives you the libraries -- but not the dependencies -- and no version numbers for anything. I did that because sometimes a dependent library will get dropped for some reason or another. You may need to modify this with the new version of python you want.
I think this works better than conda env export | cut -f 1 -d '=' | grep -v ^prefix because, again, sometimes a dependency will get dropped.
From there
conda env create -f your_environment_name.yml -p /home/user/anaconda3/envs/your_environment_name

An alternative approach here is to add the previous Miniconda2 package cache (/work/miniconda2/pkgs) and environments directory (/work/miniconda2/envs) to the pkgs_dirs and envs_dirs Conda configuration settings. That way you can simply continuing having them available without having to archive and recreate. There are some details in this answer.
You may need to add back the Miniconda3 locations after this if you would like them to continue to be defaults.
Also, because you installed multiple copies of Conda, you should probably check your ~/.bashrc (and/or ~/.bash_profile) file to remove any previous sections from conda init left by the other version.

Related

Installing soda cli in existing app golang

Description
Issue while installing soda cli in existing app
I downloaded the cli like the documentation
https://gobuffalo.io/en/docs/db/toolbox
Steps to Reproduce the Problem
$ go get github.com/gobuffalo/pop/...
$ go install github.com/gobuffalo/pop/soda
Expected Behavior
when i write soda -v
it must show soda version
Actual Behavior
soda: command not found
Info
OS: ubuntu 21
The problem is very probably that the path where the soda binary gets installed is not in your PATH system variable.
To know where your go binaries are installed, run:
go env | grep GOPATH
This will print:
GOPATH="/path/to/go"
Then you need to add /path/to/go/bin in your environment, through your .bashrc, .zshrc, .profile or whatever you need to have it in your environment, adding the line:
export PATH="$PATH:/path/to/go/bin"
You can do all of this in one single command:
echo "export PATH=\"\$PATH:${$(go env | grep GOPATH | cut -d '=' -f2):1:-1}/bin\"" >> .bashrc
If soda is installed inside go/bin
Just add an alias for soda
Open your terminal and write this command
alias soda="~/go/bin/soda"
To write permanent alias
sudo nano ~/.bashrc
And in the end of file write the alias
alias soda="~/go/bin/soda"

Git - how do I add the configuration for me to "tab" to get the correct branch or branches if there aren't any matches? [duplicate]

E.g. on a fresh ubuntu machine, I've just run sudo apt-get git, and there's no completion when typing e.g. git check[tab].
I didn't find anything on http://git-scm.com/docs, but IIRC completion is included in the git package these days and I just need the right entry in my bashrc.
On Linux
On most distributions, git completion script is installed into /etc/bash_completion.d/ (or /usr/share/bash-completion/completions/git) when you install git, no need to go to github. You just need to use it - add this line to your .bashrc:
source /etc/bash_completion.d/git
# or
source /usr/share/bash-completion/completions/git
In some versions of Ubuntu, git autocomplete may be broken by default, reinstalling by running this command should fix it:
sudo apt-get install git-core bash-completion
On Mac
You can install git completion using Homebrew or MacPorts.
Homebrew
if $BASH_VERSION > 4: brew install bash-completion#2 (updated version)
Pay special care which version of bash you have as MacOS default ships with 3.2.57(1)-release.
add to .bash_profile:
[[ -r "/usr/local/etc/profile.d/bash_completion.sh" ]] && . "/usr/local/etc/profile.d/bash_completion.sh"
For older versions of bash: brew install bash-completion
add to .bash_profile:
[ -f /usr/local/etc/bash_completion ] && . /usr/local/etc/bash_completion
MacPorts
sudo port install git +bash_completion
then add this to your .bash_profile:
if [ -f /usr/share/bash-completion/bash_completion ]; then
. /usr/share/bash-completion/bash_completion
fi
more info in this guide: Install Bash git completion
Note that in all cases you need to create a new shell (open a new terminal tab/window) for changes to take effect.
i had same issue, followed below steps:
curl https://raw.githubusercontent.com/git/git/master/contrib/completion/git-completion.bash -o ~/.git-completion.bash
then add the following lines to your .bash_profile (generally under your home folder)
if [ -f ~/.git-completion.bash ]; then
. ~/.git-completion.bash
fi
source : http://code-worrier.com/blog/autocomplete-git/
Most of the instructions you see will tell you to download
https://raw.githubusercontent.com/git/git/master/contrib/completion/git-completion.bash
and source that in your bash startup script like .bashrc.
But there is a problem with that, because it is referencing the master branch, which is the latest version of git-completion.bash. The problem is that sometimes it will break because it is not compatible with the version of git you've installed.
In fact, right now that will break because the master branch's git-completion.bash has new features that requires git v2.18, which none of the package managers and installers have updated to yet. You'll get an error unknown option: --list-cmds=list-mainporcelain,others,nohelpers,alias,list-complete,config
So the safest solution is to reference the version/tag that matches the git you've installed. For example:
https://raw.githubusercontent.com/git/git/v2.17.1/contrib/completion/git-completion.bash
Note that it has a v2.17. in the URL instead of master. And then, of course, make sure to source that in the bash startup script.
Ubuntu 14.10
Install git-core and bash-completion
sudo apt-get install -y git-core bash-completion
For current session usage
source /usr/share/bash-completion/completions/git
To have it always on for all sessions
echo "source /usr/share/bash-completion/completions/git" >> ~/.bashrc
Just do this in your ~/.bashrc:
source /usr/share/bash-completion/completions/git
Other answers are telling you to install bash-completion, you don't need to do that, but if you do, then there's no need to source the completion directly. You do one or the other, not both.
A more generic solution is querying the system location as recommended by the bash-completion project:
source "$(pkg-config --variable=completionsdir bash-completion)"/git
See https://github.com/git/git/blob/master/contrib/completion/git-completion.bash
You just need to source the completion script
on my ubuntu there is a file installed here:
source /etc/bash_completion.d/git-prompt
you can follow the links into the /usr/lib/git-core folder. You can find there an instruction, how to set up PS1 or use __git_ps1
macOS via Xcode Developer Tools
Of all the answers currently posted for macOS, this is only mentioned in a very brief comment by jmt...
If you already have the Xcode developer tools installed, then you shouldn't need to download anything new.
Instead, you just need to locate the already-existing git-completion.bash file and source it in your .bashrc. Check the following directories:
/Applications/Xcode.app/Contents/Developer/usr/share/git-core
/Library/Developer/CommandLineTools/usr/share/git-core
Failing that, git itself might be able to help you out. When I run git config as follows, git reports a setting which comes from a gitconfig file located in the same directory as my git-completion.bash:
$ git config --show-origin --list
...
file:/Applications/Xcode.app/Contents/Developer/usr/share/git-core/gitconfig credential.helper=osxkeychain
...
or you can always brute-force search your machine and grab some coffee:
$ find / -type f -name git-completion.bash 2>/dev/null
Thus, I have the following insertion for my ~/.bashrc:
# Git shell completion and prompt string on macOS
_git_dir="/Applications/Xcode.app/Contents/Developer/usr/share/git-core"
if [ -f "${_git_dir}/git-completion.bash" ]; then
source "${_git_dir}/git-completion.bash"
fi
if [ -f "${_git_dir}/git-prompt.sh" ]; then
source "${_git_dir}/git-prompt.sh"
fi
unset _git_dir
Note that this sources the git prompt-string script as well, since it resides in the same directory.
(Tested in macOS Catalina)
May be helpful for someone:--
After downloading the .git-completion.bash from the following link,
curl https://raw.githubusercontent.com/git/git/master/contrib/completion/git-completion.bash -o ~/.git-completion.bash
and trying to use __git_ps1 function, I was getting error as--
-bash: __git_ps1: command not found
Apparently we need to download scripts separately from master to make this command work, as __git_ps1 is defined in git-prompt.sh . So similar to downloading .git-completion.bash , get the git-prompt.sh:
curl -L https://raw.github.com/git/git/master/contrib/completion/git-prompt.sh > ~/.bash_git
and then add the following in your .bash_profile
source ~/.bash_git
if [ -f ~/.git-completion.bash ]; then
. ~/.git-completion.bash
export PS1='\W$(__git_ps1 "[%s]")>'
fi
source ~/.bash.git will execute the downloaded file and
export PS1='\W$(__git_ps1 "[%s]") command will append the checkout out branch name after the current working directory(if its a git repository).
So it will look like:-
dir_Name[branch_name] where dir_Name is the working directory name and branch_name will be the name of the branch you are currently working on.
Please note -- __git_ps1 is case sensitive.
Arch Linux
Source /usr/share/git/completion/git-completion.bash in one of the bash startup files.
For example:
# ~/.bashrc
source /usr/share/git/completion/git-completion.bash
You may be able to find the script in other locations like /usr/share/bash-completion/completions/git but these scripts did not work for me.
Mac M1
For those that are using Mac M1 environment, I was able to install via homebrew:
brew install bash-completion
Then added to my ~/.bash_profile or ~/.bashrc (whatever you use):
[[ -r "/opt/homebrew/Cellar/bash-completion/1.3_3/etc/profile.d/bash_completion.sh" ]] && . "/opt/homebrew/Cellar/bash-completion/1.3_3/etc/profile.d/bash_completion.sh"
You may need to update the version number (1.3_3). You'll just need to look it up in that directory. I would love to know if there's a better way.
Windows
How it works for me finally on Windows 10 command line (cmd):
Install Clink
Copy git-autocomplete.lua file into C:\Users\<username>\AppData\local\clink directory
Restart Windows
Ubuntu
There is a beautiful answer here. Worked for me on Ubuntu 16.04
Windows
Git Bash is the tool to allow auto-completion. Not sure if this is a part of standard distribution so you can find this link also useful.
By the way, Git Bash allows to use Linux shell commands to work on windows, which is a great thing for people, who have experience in GNU/Linux environment.
On Ubuntu 22.04 just add this line at the end of .bashrc or .zshrc
source /etc/bash_completion.d/git-prompt
On Github in the Git project, They provide a bash file to autocomplete git commands.
You should download it to home directory and you should force bash to run it. It is simply two steps and perfectly explained(step by step) in the following blog post.
code-worrier blog: autocomplete-git/
I have tested it on mac, it should work on other systems too. You can apply same approach to other operating systems.
Just put below in the .bashrc and relaunch the terminal. Navigate to Git repo to see the path in the prompt.
PS1='\[\033[0;32m\]\[\033[0m\033[0;32m\]\u\[\033[0;36m\] # \[\033[0;36m\]\h \w\[\033[0;32m\]$(__git_ps1)\n\[\033[0;32m\]└─\[\033[0m\033[0;32m\] \$\[\033[0m\033[0;32m\] ▶\[\033[0m\] '

How do I prevent Conda from activating the base environment by default?

I recently installed anaconda2 on my Mac. By default Conda is configured to activate the base environment when I open a fresh terminal session.
I want access to the Conda commands (i.e. I want the path to Conda added to my $PATH which Conda does when initialised so that's fine).
However I don't ordinarily program in python, and I don't want Conda to activate the base environment by default.
When first executing conda init from the prompt, Conda adds the following to my .bash_profile:
# >>> conda initialize >>>
# !! Contents within this block are managed by 'conda init' !!
__conda_setup="$('/Users/geoff/anaconda2/bin/conda' 'shell.bash' 'hook' 2> /dev/null)"
if [ $? -eq 0 ]; then
eval "$__conda_setup"
else
if [ -f "/Users/geoff/anaconda2/etc/profile.d/conda.sh" ]; then
. "/Users/geoff/anaconda2/etc/profile.d/conda.sh"
else
export PATH="/Users/geoff/anaconda2/bin:$PATH"
fi
# fi
unset __conda_setup
# <<< conda initialize <<<
If I comment out the whole block, then I can't activate any Conda environments.
I tried to comment out the whole block except for
export PATH="/Users/geoff/anaconda2/bin:$PATH"
But then when I started a new session and tried to activate an environment, I got this error message:
CommandNotFoundError: Your shell has not been properly configured to use 'conda activate'.
This question (and others like it) are helpful, but doesn't ultimately answer my question and is more suited for linux users.
To be clear, I'm not asking to remove the (base) from my $PS1 I'm asking for Conda not to activate base when I open a terminal session.
I have conda 4.6 with a similar block of code that was added by conda. In my case, there's a conda configuration setting to disable the automatic base activation:
conda config --set auto_activate_base false
The first time you run it, it'll create a .condarc in your home directory with that setting to override the default.
This wouldn't de-clutter your .bash_profile but it's a cleaner solution without manual editing that section that conda manages.
There're 3 ways to achieve this after conda 4.6. (The last method has the highest priority.)
Use sub-command conda config to change the setting.
conda config --set auto_activate_base false
In fact, the former conda config sub-command is changing configuration file .condarc. We can modify .condarc directly. Add following content into .condarc under your home directory,
# auto_activate_base (bool)
# Automatically activate the base environment during shell
# initialization. for `conda init`
auto_activate_base: false
Set environment variable CONDA_AUTO_ACTIVATE_BASE in the shell's init file. (.bashrc for bash, .zshrc for zsh)
export CONDA_AUTO_ACTIVATE_BASE=false
To convert from the condarc file-based configuration parameter name to the environment variable parameter name, make the name all uppercase and prepend CONDA_. For example, conda’s always_yes configuration parameter can be specified using a CONDA_ALWAYS_YES environment variable.
The environment settings take precedence over corresponding settings in .condarc file.
References
The Conda Configuration Engine for Power Users
Using the .condarc conda configuration file
conda config --describe
Conda 4.6 Release
The answer depends a little bit on the version of conda that you have installed. For versions of conda >= 4.4, it should be enough to deactivate the conda environment after the initialization, so add
conda deactivate
right underneath
# <<< conda initialize <<<
To disable auto activation of conda base environment in terminal:
conda config --set auto_activate_base false
To activate conda base environment:
conda activate
So in the end I found that if I commented out the Conda initialisation block like so:
# >>> conda initialize >>>
# !! Contents within this block are managed by 'conda init' !!
# __conda_setup="$('/Users/geoff/anaconda2/bin/conda' 'shell.bash' 'hook' 2> /dev/null)"
# if [ $? -eq 0 ]; then
# eval "$__conda_setup"
# else
if [ -f "/Users/geoff/anaconda2/etc/profile.d/conda.sh" ]; then
. "/Users/geoff/anaconda2/etc/profile.d/conda.sh"
else
export PATH="/Users/geoff/anaconda2/bin:$PATH"
fi
# fi
# unset __conda_setup
# <<< conda initialize <<<
It works exactly how I want. That is, Conda is available to activate an environment if I want, but doesn't activate by default.
If you manage your .bashrc manually and like to keep it simple, all you really need is:
. "$HOME/anaconda2/etc/profile.d/conda.sh"
See Recommended change to enable conda in your shell.
This will make the conda command available without activating the base environment (nor reading your conda config).
Note that this is (of course) not compatible with managing the conda installation with conda init, but other than that, nothing bad is coming from it. You may even experience a significant speedup compared to the conda init generated code, because this solution avoids calling conda to parse your config files on whether to enable the base environment, etc.
It's best to also keep the if/fi lines to avoid error messages if using the same bashrc on several systems where conda may not be installed:
if [ -f "$HOME/anaconda2/etc/profile.d/conda.sh" ]; then
. "$HOME/anaconda2/etc/profile.d/conda.sh"
fi
Finally, if you share your bashrc between several systems where conda may be installed in different paths, you could do as follows:
for CONDA_PREFIX in \
"$HOME/anaconda2" \
"$HOME/miniconda3" \
"/opt/miniconda3" \
do
if [ -f "$CONDA_PREFIX/etc/profile.d/conda.sh" ]; then
. "$CONDA_PREFIX/etc/profile.d/conda.sh"
break
fi
done
Of course, this is now similar in length compared to the conda init generated code, but will still execute much faster, and will likely work better than conda init for users who synchronize their .bashrc between different systems.
for conda 4.12.0 (under WOS) the following worked (where all the previous answers -these included- didn't do the trick):
in your activate.bat file (mine was at ~/miniconda3/Scripts/activate.bat), change the line:
#REM This may work if there are spaces in anything in %*
#CALL "%~dp0..\condabin\conda.bat" activate %*
into
#REM This may work if there are spaces in anything in %*
#CALL "%~dp0..\condabin\conda.bat" deactivate
this line chage/modification doesn't work in the section (of the activate.bat file):
#if "%_args1_first%"=="+" if NOT "%_args1_last%"=="+" (
#CALL "%~dp0..\condabin\conda.bat" activate
#GOTO :End
)
maybe because it depends on how your miniconda3 (Anaconda Prompt) executable is set up: %windir%\System32\cmd.exe "/K" some-path-to\miniconda3\Scripts\activate.bat some-path-to\miniconda3 (in my case).
caveat: updating conda overwrites this (activate.bat) file; so one has to modify the above line as many times as needed/updated. not much of a deal-breaker if you ask me.
This might be a bug of the recent anaconda. What works for me:
step1: vim /anaconda/bin/activate, it shows:
#!/bin/sh
_CONDA_ROOT="/anaconda"
# Copyright (C) 2012 Anaconda, Inc
# SPDX-License-Identifier: BSD-3-Clause
\. "$_CONDA_ROOT/etc/profile.d/conda.sh" || return $?
conda activate "$#"
step2: comment out the last line: # conda activate "$#"
One thing that hasn't been pointed out, is that there is little to no difference between not having an active environment and and activating the base environment, if you just want to run applications from Conda's (Python's) scripts directory (as #DryLabRebel wants).
You can install and uninstall via conda and conda shows the base environment as active - which essentially it is:
> echo $Env:CONDA_DEFAULT_ENV
> conda env list
# conda environments:
#
base * F:\scoop\apps\miniconda3\current
> conda activate
> echo $Env:CONDA_DEFAULT_ENV
base
> conda env list
# conda environments:
#
base * F:\scoop\apps\miniconda3\current

virtualenv name not show in zsh prompt

Recently, I give a try on oh my zsh, everything looks good till I try virtualevn and virtualenvwrapper. When I activate a virtualenv (e.g test), on normal bash, I will see the virtualenv name like:
(test)abc#abc:
But when I switched to zsh, I cannot see virtualenv name. Even though, I alr add virtualenv and virtualenvwrapper in plugins of oh my zsh. I also checked the activate file of my virtualenv, it contains:
f [ -z "${VIRTUAL_ENV_DISABLE_PROMPT-}" ] ; then
_OLD_VIRTUAL_PS1="$PS1"
if [ "x" != x ] ; then
PS1="$PS1"
else
PS1="(`basename \"$VIRTUAL_ENV\"`) $PS1"
fi
export PS1
fi
Is it because the comparision ["x" != x] return true?
Updated:
I tried to echo $PS1 in activate file, and got this:
(test) %{$fg[magenta]%}%n%{$reset_color%}%{$fg[cyan]%}#%{$reset_color%}%{$fg[yellow]%}%m%{$reset_color%}%{$fg[red]%}:%{$reset_color%}%{$fg[cyan]%}%0~%{$reset_color%}%{$fg[red]%}|%{$reset_color%}%{$fg[cyan]%}⇒%{$reset_color%}
It seems the $PS1 is correct, but when I echo $PS1 in the terminal, the (test) is gone. It seems the $PS1 is override by something else!
Do this in ~/.zshrc:
plugins=(virtualenv)
POWERLEVEL9K_RIGHT_PROMPT_ELEMENTS=(status virtualenv)
Caveats:
1 -- add that plugin in addition to other plugins you have.
2 -- I'm using the POWERLEVEL9K theme. Maybe you theme
The best solution is to add the following to the end of your ~/.zshrc file:
export VIRTUAL_ENV_DISABLE_PROMPT=
This will override the value in virtualenv.plugin.zsh - no need to change that file.
My setting to display Python virtualenv name for the default (a.k.a. robbyrussell) theme is the following.
In the .zshrc file
virtualenv added in plugins
Add custom function:
function virtualenv_info {
[ $VIRTUAL_ENV ] && echo '('`basename $VIRTUAL_ENV`') '
}
Navigate to your theme
My theme is the default theme for zsh:
$ vim ~/.oh-my-zsh/themes/robbyrussell.zsh-theme
Add this command right after existing PROMPT commands:
PROMPT+='%{$fg[green]%}$(virtualenv_info)%{$reset_color%}%'
Finally
$ source ~/.zshrc
PS: You can add your name or a few space before or after the PROMPT+.
Hope that helps!
Found the problem, it's due to the theme. The theme I used in the above case is pygmalion, it won't allow u to change $PS1.
After changed to robbyrussell theme, I can change $PS1 in terminal, but still cannot see the virtualenv name. After a while debugging, I found that by default the virtualenv plugin of oh my zsh disable the prompt:
# disables prompt mangling in virtual_env/bin/activate
export VIRTUAL_ENV_DISABLE_PROMPT=1
So just comment out the line in virtualenv plugin, problem solved.
As per this guide here
First add virtualenv dependency under plugin in file .zshrc
If this doesn't work for you, then it means that the theme(one of oh-my-zsh theme) you have selected doesn't include virtualenv name in bash prompt so try second step.
Go to file ~/.oh-my-zsh/themes/YOUR_THEME_NAME.zsh-theme and add this in base prompt
%{$fg[green]%}$(virtualenv_prompt_info)%{$reset_color%}%
NOTE: virtualenv_prompt_info is the name of function which is declared in ~/.oh-my-zsh/plugins/virtualenv/virtualenv.plugin.zsh. If your plugin file have different function name then change it accordingly.
Or you can declare your own function in ~/.zshrc file as shown in this guide
If you are using conda to start your virtual environment the envorionment variable will be different. To figure out the name of the environment that holds your virtaulenv name type printenv and look through the output. For me it is CONDA_PROMPT_MODIFIER
after you know the name of the variable open .zshrc and add this function
function virtualenv_info {
[ $CONDA_PROMPT_MODIFIER ] && echo `basename $CONDA_PROMPT_MODIFIER`
}
and below that add this line
PROMPT="%{$fg[green]%}$(virtualenv_info)%{$reset_color%}%${PROMPT}"
close the editor and type source .zshrc
In the case you installed Anaconda using Homebrew:
brew tap homebrew/cask-versions
brew cask install anaconda
And you are using POWERLEVEL9K theme
git clone https://github.com/bhilburn/powerlevel9k.git ~/.oh-my-zsh/custom/themes/powerlevel9k
All you need to do is add this line to the end of .zshrc :
POWERLEVEL9K_RIGHT_PROMPT_ELEMENTS=(status root_indicator background_jobs history time anaconda)
There's no need for virtualenv plugin.
Edited:
In case you already had conda installed for bash and you get:
zsh: command not found: conda
Run this:
~/anaconda3/bin/conda init zsh
I made it work following this link: https://askubuntu.com/a/387098
I reproduce the answer below.
How the prompt is changed is defined in the script bin/activate inside the virtual environment directory. This file is created by virtualenv from a template. Unfortunatelly, the only way of prompt modification provided by the template is prepending (env name) or whatever is set with --prompt.
To modify the prompt in the way you want, I'd suggest circumventing the setting of the prompt in bin/activate and modify the definition of PROMPT in your theme file.
First add the following to your.zsh-theme (or .zshrc)
export VIRTUAL_ENV_DISABLE_PROMPT=yes
function virtenv_indicator {
if [[ -z $VIRTUAL_ENV ]] then
psvar[1]=''
else
psvar[1]=${VIRTUAL_ENV##*/}
fi
}
add-zsh-hook precmd virtenv_indicator
and add %(1V.(%1v).) in front of the second line of the definition of PROMPT. It should then look like this:
PROMPT='
%(1V.(%1v).)%{$fg_bold[grey]%}[%{$reset_color%}%{$fg_bold[${host_color}]%}%n#%m%{$reset_color%}%{$fg_bold[grey]%}]%{$reset_color%} %{$fg_bold[blue]%}%10c%{$reset_color%} $(git_prompt_info) $(git_remote_status)
%{$fg_bold[cyan]%}❯%{$reset_color%} '
If you want some color you could add %(1V.%{$fs_bold[yellow]%}(%1v)%{$reset_color%}.) for example.
Explanation:
virtenv_indicator will be called each time before the prompt is created. It checks if $VIRTUAL_ENV is set and not empty. If so, it sets the first element of the $psvar array to $VIRTUAL_ENV with everything before and including the last / removed (like basename $VIRTUAL_ENV but less expensive)
In the definition of PROMPT %(1V.(%1v).) checks if the first element of $psvar is set and not empty (%(1V.true-text.false-text)) and adds the content of the this element plus some parentheses ((%1v))
export VIRTUAL_ENV_DISABLE_PROMPT=yes disables any prompt setting by bin/activate scripts.
if you use zsh and pyenv, put this into ~/.zshrc
eval "$(pyenv init -)"
eval "$(pyenv virtualenv-init -)"
#export PS1='($(pyenv version-name)) '$PS1
export PYENV_VIRTUALENV_DISABLE_PROMPT=1
export BASE_PROMPT=$PS1
function updatePrompt {
if [[ "$(pyenv version-name)" != "system" ]]; then
# the next line should be double quote; single quote would not work for me
export PS1="($(pyenv version-name)) "$BASE_PROMPT
else
export PS1=$BASE_PROMPT
fi
}
export PROMPT_COMMAND='updatePrompt'
precmd() { eval '$PROMPT_COMMAND' } # this line is necessary for zsh
I am also using Oh My Zsh with the pygmalion theme. I had to edit the pygmalion script to add the virtual environment name before the prompt name.
open ~/.oh-my-zsh/themes/pygmalion.zsh-theme, modify the prompt_pygmalion_precmd function as following:
prompt_pygmalion_precmd(){
setopt localoptions extendedglob
local gitinfo=$(git_prompt_info)
local gitinfo_nocolor=${gitinfo//\%\{[^\}]##\}}
local exp_nocolor="$(print -P \"$base_prompt_nocolor$gitinfo_nocolor$post_prompt_nocolor\")"
local prompt_length=${#exp_nocolor}
local python_venv="($(echo $CONDA_DEFAULT_ENV)) "
PROMPT="${python_venv}${base_prompt}${gitinfo}${post_prompt}"
}
The following steps should solve the problem:
open ~/.p10k.zsh.
If you use only the left prompt, make the following changes:
typeset -g POWERLEVEL9K_LEFT_PROMPT_ELEMENTS=(
# =========================[ Line #1 ]=========================
os_icon # os identifier
dir # current directory
vcs # git status
# =========================[ Line #2 ]=========================
newline # \n
prompt_char # prompt symbol
virtualenv venv .venv env # show the venv on the second line
)
Add the following line, preferably after you adjust POWERLEVEL9K_VIRTUALENV_SHOW_WITH_PYENV:
typeset -g POWERLEVEL9K_VIRTUALENV_GENERIC_NAMES=()
Save the .p10k.zsh.
Restart the terminal.
Now, when you activate the virtual environment (on macOS source my_venv/bin/activate), then the name of the virtual environment (in my case, my_venv) and the version of Python installed on it (3.9.13) will appear after a beautiful Python symbol. Have a look at the attached screenshot.
I am using oh-my-zsh pygmalion them, and this works for me:
add virtualenv plugin in ~/.zshrc
open ~/.oh-my-zsh/themes/pygmalion.zsh-theme, modify the prompt_pygmalion_precmd function to this:
prompt_pygmalion_precmd(){
setopt localoptions extendedglob
local gitinfo=$(git_prompt_info)
local gitinfo_nocolor=${gitinfo//\%\{[^\}]##\}}
local exp_nocolor="$(print -P \"$base_prompt_nocolor$gitinfo_nocolor$post_prompt_nocolor\")"
local prompt_length=${#exp_nocolor}
local python_venv=$(virtualenv_prompt_info)
PROMPT="${python_venv}${base_prompt}${gitinfo}${post_prompt}"
}
Basically just add $(virtualenv_prompt_info) to your PROMPT to wherever you prefer, here I added it to the very beginning of my PROMPT.
You do not need to create new function, as per documentation - this function is created for you.
https://github.com/ohmyzsh/ohmyzsh/tree/master/plugins/virtualenv
But You still need to edit theme file, as mentioned above, just enter correct function name - virtualenv_prompt_info:
PROMPT+='%{$fg[green]%}$(virtualenv_prompt_info)%{$reset_color%}%'
After playing with the surround answers, I found the following to be the best for my use case. This checks the $VIRTUAL_ENV_PROMPT and $VIRTUAL_ENV variables every time you change directories and sets the prompt with the correct venv info.
DEFAULT_PROMPT=$PROMPT
function cd() {
builtin cd "$#"
if [[ -n "$VIRTUAL_ENV_PROMPT" ]] ; then
PROMPT="$VIRTUAL_ENV_PROMPT$DEFAULT_PROMPT"
elif [[ -n "$VIRTUAL_ENV" ]] ; then
PROMPT="(`basename $VIRTUAL_ENV`) $DEFAULT_PROMPT"
else
PROMPT=$DEFAULT_PROMPT
fi
}
export PS1='($(pyenv version-name)) '$PS1
source & link to issue #135 in pyenv-virtualenv repo:
https://github.com/pyenv/pyenv-virtualenv/issues/135#issuecomment-582180662
Back to 2023 : here something that worked for me with the theme .
Search the line for "plugins" and add virtualenv (if you are using this one)
plugins=(git python brew macos colored-man-pages virtualenv vscode)
Now look for the ZSH-Theme and use
ZSH_THEME="pygmalion-virtualenv"
Reload your terminal or kill your Visual Studio code window (reloading the terminal into VS cod didn't display the change for me...)

System-wide RVM to replace a home directory loaded on different architectures

I saw some questions like this already, but it seems that system-wide RVM has been deprecated, so none of the answers apply.
I have an NFS-mounted home directory which is accessible to me when I login to any number of different machines in our lab. As such, a locally-compiled Ruby will break when I try to use it on these different machines -- as they have different architectures.
I installed RVM as superuser, but I can't figure out how to direct my regular user account to use the superuser-selected Ruby. Instead it always tries to use the one in ~/bin.
What is the appropriate way to select the global Ruby?
I struggled with this same issue for quite a while. I ended up doing the following:
Install rvm with the following command line (note: the --path option is not mentioned in the usage, so I don't know how supported it is, but it worked for me), with the appropriate architecture in the path:
rvm-installer --path ~/tools/x86_64/rvm --version latest
Install for whatever architectures you want to be able to support. (note: make sure you don't have a .rvmrc file overriding the path, especially during the second install.)
Then add the following to your .bashrc/.bash_profile:
OS=$(uname -s)
if [[ $OS = Linux ]] ; then
> ARCH=$(uname -m | sed 's/i.86/i686/')
elif [[ $OS = FreeBSD ]] ; then
> ARCH=$(uname -m | sed 's/i.86/i686/')
elif [[ $OS = Darwin ]] ; then
> ARCH=mac
else
> ARCH=unknown
fi
export ARCH
There will be a line added by the installer in your .bash_profile that sets up the rvm function, change it to look like this:
[[ -s "$HOME/tools/$ARCH/rvm/scripts/rvm" ]] && . "$HOME/tools/$ARCH/rvm/scripts/rvm"
You should also edit your .rvmrc file to have the following:
export rvm_path="$HOME/tools/$ARCH/rvm"
I haven't had time to fully test out this setup, but at least it appears to be invoking the correct version of RVM on the different platforms.
Good luck!

Resources