This post contains details of how I set up my shell and environment. I use Windows, Mac, and Linux on a daily basis, so I have different setups for different purposes, but I try to make them similar when I can. You can see the software I use and how I customize it in the linked posts; this post will focus on setup.
Table of Contents
- Shell
- Installing Homebrew
- Keyboard Customizations
- Installing Anaconda
- Packages
- Shell Configuration
- My .zshrc
- Alias Notes
- Finding How Things Got in Environment
- Useful Git Commands
- Appendix
Shell
On Macs, I use zsh as my main shell. It’s the default shell now but older Macs will need to install it. My setup is based around zsh
.
Shell Configuration
I use Oh My Zsh to configure zsh and highly recommend it.
Development Environment
I have a particular way I set up my development environment. I store all of my aliases and environment variables other than my passwords in a ~/.profile
file. This way I can share it with a team and we can all have the same hotkeys. In ~/.profile
, I source a separate file called something like .my_credentials
, which is where all my credentials are exported from.
I source ~/.profile
from whatever shell I’m using. If I’m using Oh My Zsh, I create a file that just says source ~/.profile
and save it at ~/.oh-my-zsh/custom/profile.zsh
. I usually leave the .zshrc
file alone, but you can customize the Oh My Zsh theme if you want.
The full chain looks like this:
~/.zshrc
-> ~/.oh-my-zsh/custom/profile.zsh
-> ~/.profile
-> ~/.my_credentials
- Also
.profile
will source.bash_profile
if it exists
I put all this in a dotfiles repo so that it is under version control. I use symlinks from there to do the above.
- Note:
~/.zshrc
is actually a symlink to the .zshrc in my dotfiles repo.
Other Additions
Sometimes other applications will place information in your profile files. Some examples:
- brew puts something in
zprofile
- conda adds to
.zshrc
or sometimes.bash_profile
depending on how you install it.
Installing Homebrew
Homebrew is the best package manager for Mac. It installs in /usr/local
for macOS Intel and /opt/homebrew
for Apple Silicon. You can run the right location either way with this:
# Set Homebrew path and run eval
HOMEBREW_PREFIX=$(brew --prefix)
if [[ -d "${HOMEBREW_PREFIX}" ]]; then
eval "$("${HOMEBREW_PREFIX}/bin/brew" shellenv)"
fi
Keyboard Customizations
Here are some things I like to change on Macs and here’s how I do it:
- The cursor doesn’t jump from word to word by default (at least, with oh-my-zsh)
- To fix this, you need to add the following to your .profile:
bindkey "\e\e[D" backward-word bindkey "\e\e[C" forward-word
- To fix this, you need to add the following to your .profile:
In general, I want the control button to do much more:
- ctrl + w to close a web page
- ctrl + arrow key to jump to next word, both in terminal and in text editor in browser.
I do this to make it easier to navigate in terminals: https://stackoverflow.com/questions/6205157/how-to-set-keyboard-shortcuts-to-jump-to-beginning-end-of-line/22312856#22312856
- also add ctrl+z for undo (so there are two ways to undo)
Jumping around text
You’ll find that whether you can jump around text depends on a specific application. For example, if you’ve done the above, you can use control + shift
to highlight entire words in Gmail, but it doesn’t work in all applications.
Installing Anaconda
- In general, I recommend installing Anaconda for all users. If you do, it will be stored in:
/opt/anaconda3/bin/conda /opt/anaconda3/condabin/conda
Packages
There are a few packages I use to improve my terminal experience.
Pygments
- Pygments, a Python syntax highlighter. It’s like
cat
with colors. I alias it toc
(as seen below).
Autojump
ZSH Syntax Highlighting
To activate the syntax highlighting, add the following at the end of your .zshrc:
source /opt/homebrew/share/zsh-syntax-highlighting/zsh-syntax-highlighting.zsh
If you receive “highlighters directory not found” error message,
you may need to add the following to your .zshenv:
export ZSH_HIGHLIGHT_HIGHLIGHTERS_DIR=/opt/homebrew/share/zsh-syntax-highlighting/highlighters
Shell Configuration
I use the shell configuration from my dotfiles repo.
My .zshrc
Conda will install the initialization script for conda inside .zshrc
(for Macs). It will depend on whether you installed Anaconda or Miniconda, and on whether you installed in for a single user or for all users. If it’s installed for all users it will be somewhere like /opt/anaconda3/etc/profile.d/conda.sh
. If it’s just installed for one user it will be somewhere like /Users/$USER/opt/anaconda3/etc/profile.d/conda.sh
. The whole initialization looks like one of the following (depending on whether you use Anaconda or Miniconda):
# >>> conda initialize >>>
# !! Contents within this block are managed by 'conda init' !!
__conda_setup="$('/bin/conda' 'shell.bash' 'hook' 2> /dev/null)"
if [ $? -eq 0 ]; then
eval "$__conda_setup"
else
if [ -f "/home/jsimonelli/miniconda3/etc/profile.d/conda.sh" ]; then
. "/home/jsimonelli/miniconda3/etc/profile.d/conda.sh"
else
export PATH="/home/jsimonelli/miniconda3/bin:$PATH"
fi
fi
unset __conda_setup
# <<< conda initialize <<<
# >>> conda initialize >>>
# !! Contents within this block are managed by 'conda init' !!
__conda_setup="$('/home/julius/anaconda3/bin/conda' 'shell.bash' 'hook' 2> /dev/null)"
if [ $? -eq 0 ]; then
eval "$__conda_setup"
else
if [ -f "/home/julius/anaconda3/etc/profile.d/conda.sh" ]; then
. "/home/julius/anaconda3/etc/profile.d/conda.sh"
else
export PATH="/home/julius/anaconda3/bin:$PATH"
fi
fi
unset __conda_setup
# <<< conda initialize <<<
It’s fine to keep in there, but if you use tmux
, you might run into a problem. tmux
doesn’t always source .zshrc
. Sometimes it only sources .profile
, so conda won’t load in a tmux window. Even worse, it may pull Python from /usr/bin/python
, which will be old Python 2 (use which python
to see which Python is being used). So you might want to cut and paste the initialization over to .profile.
I have found that if I don’t include conda activate $DEFAULT_CONDA_ENVIRONMENT
in my .zshrc
, it doesn’t activate my default profile, even though I have this in my .profile
. So I leave it in .zshrc
.
Other stuff is added to .zshrc
automatically as well. Things like [ -f ~/.fzf.zsh ] && source ~/.fzf.zsh
automatically get added here. If you don’t need it for tmux
, you can leave it here. Otherwise I would recommend moving it all over to .profile
.
Alias Notes
If you make a shortcut to your code base like so:
export BASE='$HOME/git'
then if you want to use it in an alias you’ll have to use double quotes.
Instead of alias cdh=cd $BASE'
you’ll have to use alias cdh="cd $BASE"
However, if you were just doing it with $HOME
, it seems single quotes work.
Finding How Things Got in Environment
If you want to find how something got in your conda environment, you could grep it like this:
grep 'mysterious_message' ~/.bashrc ~/.bash_profile ~/.profile
Useful Git Commands
These are good to set to an alias
git log --pretty=format:'%C(yellow)%h %Cred%ad %Cblue%an%Cgreen%d %Creset%s' --date=short
Testing:
# set a fancy prompt (non-color, unless we know we "want" color)
case "$TERM" in
xterm-color|*-256color) color_prompt=yes;;
esac
Appendix
Full .profile
My default .profile is available here: https://github.com/jss367/dotfiles/blob/main/shell/profile[https://github.com/jss367/dotfiles/blob/main/shell/profile]