How to modify conda prompt string content - bash

How can I edit the conda prompt's behavior without touching my normal prompt? I want to retain conda's prepend-to-PS1 behavior, but change the string that gets prepended.
The question how to modify conda 'source activate' ps1 behavior is very similar. However, the solution there is to either inject the conda prefix into the middle of PS1 and/or edit PROMPT_COMMAND. This breaks the encapsulation I want, and is very hacky when the prepend-to-PS1 behavior is still desirable.
My normal prompt string looks like this:
previous output
previous output
user#short-domain fullpath
$
This is the behavior I want when no conda environment is active. With a conda environment active, this becomes:
previous output
previous output
(<env-name-here>)
user#short-domain fullpath
$
I don't like how this eliminates the blank line between the previous command's output and the new prompt. Unlike the question I mentioned above, I specifically want (<env-name-here>) on its own line:
previous output
previous output
(<env-name-here>)
user#short-domain fullpath
$
Note how this means the conda prompt modification needs to include its own newline character. I could hack the other question's answers into working, but again, I don't want to touch any variables related to my normal prompt.

There is a proper and very simple way to do this using conda's native
configuration options.
Edit the .condarc file to contain the line:
env_prompt: \n({default_env})
or run the command:
$ conda config --system --set env_prompt "\n({default_env})"
Either of these will achieve the desired effect for new terminals.
Note that the --system option may not be desirable for many use cases.
See the explanation below for more details.
From the conda documentation
This feature can be elusive if you don't know where to look. The most
natural way to find it is to start with the configuration section
of the conda user guide.
The "using the .condarc conda configuration file" overview tells
us:
The conda configuration file, .condarc, is an optional runtime
configuration file that allows advanced users to configure various
aspects of conda, such as which channels it searches for packages,
proxy settings, and environment directories. For all of the conda
configuration options, see the configuration page.
The configuration page describes the setting we want, along with
its default value:
# env_prompt (str)
# Template for prompt modification based on the active environment.
# Currently supported template variables are '{prefix}', '{name}', and
# '{default_env}'. '{prefix}' is the absolute path to the active
# environment. '{name}' is the basename of the active environment
# prefix. '{default_env}' holds the value of '{name}' if the active
# environment is a conda named environment ('-n' flag), or otherwise
# holds the value of '{prefix}'. Templating uses python's str.format()
# method.
#
env_prompt: '({default_env}) '
The conda config command
The conda config command is quite useful on its own. Doing
$ conda config --describe
shows the same information as the configuration page.
Since my .condarc file is in a non-default location, I use the --system option
for conda config to prevent conda from creating a new .condarc file in my
home directory. From conda config --help:
Config File Location Selection:
Without one of these flags, the user config file at '$HOME/.condarc' is used.
--system Write to the system .condarc file at
'<my-anaconda-install-path>/.condarc'.
--env Write to the active conda environment .condarc file
(<current-env-path>). If no
environment is active, write to the user config file
($HOME/.condarc).
--file FILE Write to the given file.

Related

How do I conda init if I don't have permission to edit my .bashrc file

I have installed miniconda3. /full/path/to/bin/conda init -d shows only my .bashrc will be modified. Is there a way to write the contents of conda init to a separate file called init_conda.sh, which can be sourced when required?
One can grab the actual code Conda would insert by adding a verbosity flag to the command:
/full/path/to/bin/conda init -d -v
And you can put that code (after removing the "+" that starting the lines, because it's a patch file output) in a file of your choice. Note that if you have a writable ~/.bash_profile, that could be an alternative to .bashrc, and that would still be loaded by default in interactive bash sessions.
Another alternative is to use Conda's hardcoded initialization file. It should be found at:
${CONDA_PREFIX}/etc/profile.d/conda.sh
So, you might consider having an alias to source that, or create a softlink to it in a more memorable location.

Where to pass EMSDK_QUIET=1

I've installed emsdk, following the steps described in the following document: https://emscripten.org/docs/getting_started/downloads.html#sdk-download-and-install
Now, when I launch a Terminal under macOS, I have these lines inserted at the beginning:
Setting up EMSDK environment (suppress these messages with EMSDK_QUIET=1)
Adding directories to PATH:
PATH += [private]/emscripten/emsdk
PATH += [private]/emscripten/emsdk/upstream/emscripten
PATH += [private]/emscripten/emsdk/node/14.18.2_64bit/bin
Setting environment variables:
PATH = [private]/emscripten/emsdk:[private]/emscripten/emsdk/upstream/emscripten:[private]/emscripten/emsdk/node/14.18.2_64bit/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/Applications/Little Snitch.app/Contents/Components:/usr/local/share/dotnet:/opt/X11/bin:~/.dotnet/tools:/Library/Apple/usr/bin:/Library/Frameworks/Mono.framework/Versions/Current/Commands
EMSDK = [private]/emscripten/emsdk
EM_CONFIG = [private]/emscripten/emsdk/.emscripten
EMSDK_NODE = [private]/emscripten/emsdk/node/14.18.2_64bit/bin/node
EMSDK_PYTHON = [private]/emscripten/emsdk/python/3.9.2_64bit/bin/python3
SSL_CERT_FILE = [private]/emscripten/emsdk/python/3.9.2_64bit/lib/python3.9/site-packages/certifi/cacert.pem
I can't find from where this is launched. emsdk doesn't appear in .bash_profile, nor in .profile or .bashrc.
Where do I have to set EMSDK_QUIET=1 to avoid these lines?
A lot of times when you install a program that needs to alter the environment like compiler toolchains the install script will modify the file it expects to be sourced by your shell based on the default shell for the current user or sometimes by scanning $HOME. On MacOS you might see it add a line to (or create if it can't find):
$HOME/.bashrc or $HOME/.bash_profile for bash
$HOME/.zshrc or $HOME/.zprofile for zsh
$HOME/.config/fish/config.fish for fish
Note that recent versions of MacOS have changed the default shell from bash to zsh. Due to licensing issues they have to ship an archaic version of bash (3.2 vs 5.x current) so it was probably a good move but it means you may need to check the zsh files instead of the usual bash ones.
When you need to set an envar for a toolchain like EMSDK_QUIET=1 just look for the line where the environment is sourced, and export that envar above it.

How do I change the name conda displays in parantheses?

I have a conda environment that I created with a path (conda create -p /full/path/to/env). When I activate the environment it displays the full path to the environment in parentheses at the beginning of the command prompt line. like
(full/path/to/env) [username#server]
I want to change it so it just shows env instead of full/path/to/env. How do I do that?
I tried adding the directory leading up to my conda environment (i.e. full/path/to) to my env_dirs configuration using conda config but that didn't work.

How to set specific environment variables when activating conda environment?

Does anyone know how to automatically set environment variables when activating an env in conda?
I have tried editing */bin/activate, but that adds the new environment variables for every new env that is created. I want to set env variables that are specific to each env.
Use the files $CONDA_PREFIX/etc/conda/activate.d and $CONDA_PREFIX/etc/conda/deactivate.d, where $CONDA_PREFIX is the path to the environment.
See the section on managing environments in the official documentation for reference.
Environment Variables as Configuration Settings
Conda v4.8 introduced a new command-line interface in the conda-env tool for managing environment variables on a per-environment basis. The command is conda env config vars and here is the help description as of v4.8.3 for the command overall:
$ conda env config vars -h
usage: conda-env config vars [-h] {list,set,unset} ...
Interact with environment variables associated with Conda environments
Options:
positional arguments:
{list,set,unset}
list List environment variables for a conda environment
set Set environment variables for a conda environment
unset Unset environment variables for a conda environment
optional arguments:
-h, --help Show this help message and exit.
examples:
conda env config vars list -n my_env
conda env config vars set MY_VAR=something OTHER_THING=ohhhhya
conda env config vars unset MY_VAR
Perhaps a bit verbose, but it avoids having to manually manage files in etc/conda/(de|)activate.d.
YAML Specification
Added in Conda v4.9, there is now support for automatic defining of environment-specific variables as part of an environment YAML definition. For example,
name: foo
channels:
- defaults
dependencies:
- python
variables:
MY_VAR: something
OTHER_VAR: ohhhhya
which would set up the environment variables MY_VAR and OTHER_VAR to be set and unset on environment activation and deactivation, respectively.
The accepted answer (conda/activate.d and conda/deactivate.d) works well enough, but it is inconvenient if you want the environment variables to be version controlled without putting the entire environment into version control too. Generally you'd want to store only the environment.yml file in version control.
(I understand that this does not apply to all projects - sometimes the entire reason for using environment variables is to prevent that particular configuration getting stored in version control.)
My preference (on Windows, but the same principle would apply on Linux) is to create a (version-controlled) activate.cmd file in the root of the project directory that sets the environemnt variable(s) and then calls conda's own activate.bat script.
Example (a per-project pylint configuration):
set PYLINTRC=%cd%\pylintrc
#activate.bat %cd%\env
Note that on Windows at least you have to set the environment variables before calling activate.bat because the call to activate.bat never returns to the calling batch file. You also have to name your own script something other than activate.bat to avoid recursion, which is why I chose the cmd extension (which is treated by Windows as a batch file in this context).
So for virtualenv on Ubuntu I did the below where my virtual environement names is my_env and my environmental variables I want to persist were VAR_A and VAR_B:
virtualenv my_env
vim my_env/bin/activate
This opens the file and you can append your env variables to the end of the file like the below:
# This is me env variables to persist
export VAR_A=/home/developer/my_workspace/var_a
export VAR_B=/home/developer/my_workspace/var_b
Then exit the file.
Activate your virtualenv with
source my_env/bin/activate
Then your env variables should be good. Can verify like the below:
printenv | grep VAR_
VAR_B=/home/developer/my_workspace/var_b
VAR_A=/home/developer/my_workspace/var_a

How to set path for vim config files?

I know they say to store your vim config files under source control.
Is there a way to tell vim where the config files are? like .vimrc etc.?
i.e. I will store them in a different folder where my other git repos are.
BTW, what are good coloring plugins for ruby?
You can use the VIM environment variable to change the location of your ~/.vim/ directory, if you want to keep all your git repos in one place. See the :help $VIM section for more details, but here's the bit that looks most useful:
The environment variable "$VIM" is used to locate various user files for Vim,
such as the user startup script ".vimrc". This depends on the system, see
|startup|.
If you add export VIM=/home/blankman/git/vim/ to your ~/.bashrc, you ought to be set.
Excerpt from the vim man page:
-u {vimrc} Use the commands in the file {vimrc} for initializations.
All the other initializations are skipped.
To have it work whenever you run vim, try putting in your shell's startup script (.bashrc etc):
alias vim='vim -u /path/to/vimrc'
Link: http://www.h3rald.com/articles/herald-vim-color-scheme/
Has a few schemes with ruby highlighting examples. Vim has decent native ruby support it seems, so you may just want a new colour scheme.
A light-coloured Ruby highlighting scheme here.

Resources