What's in your .bash_profile?

bash
#1

Inspired by @JoeZMar’s post, what shortcuts or other goodies do you have in your .bash_profile?

2 Likes
#2

I am a sucker for shortcuts, so have just added these:

alias mixec="mix ecto.create"
alias mixem="mix ecto.migrate"
alias mixps="mix phx.server"
alias iexmixps="iex -S mix phx.server"
alias iexsmix="iex -S mix"
alias iexs="iex -S mix"
alias mixpr="mix phx.routes"

I also have various others, such as:

PS1="[\W]\$ "
alias ll="ls -lahG"

alias code="cd /Users/Aston/projects/code"
alias home="cd /Users/Aston"
alias oss="cd /Users/Aston/projects/oss"
alias desktop="cd /Users/Aston/desktop"
alias some_project_name="cd /Users/Aston/projects/code/some_project_name"

alias showhidden="defaults write com.apple.finder AppleShowAllFiles -boolean true ; killall Finder"
alias hidehidden="defaults write com.apple.finder AppleShowAllFiles -boolean false ; killall Finder"

vscode () { VSCODE_CWD="$PWD" open -n -b "com.microsoft.VSCode" --args $* ;}

Plus various others to shh to a server, upload/download files to/from a server, push certain scripts or rsync sites etc

4 Likes
#3

Here is one I recently stumbled upon in the documentation for IEx:

export ERL_AFLAGS='-kernel shell_history enabled'

This enables IEx shell history between sessions.

8 Likes
#4

Well, the most basic improvement is to get rid of bash and use zsh instead :wink:

My alias list is pretty much the default, only replacing ls by k.

Also I do have emacs, emats and emax as aliases to emacsclient to choose graphical/terminal mode automatically in the first or manually with the latter two. I wrote once about my emacs deamon setup in a blog post of mine and made my emacs config public.

I can’t publish the remainder of my dotfiles though, as the repository contains some secrets, I need to distribute in another way before publishing it again.

3 Likes
Bash vs ZSH
#5

This is amazing! I’ve taken two minutes to perform the steps from your blog post, and already made up for them now, because of the near-instant loading of (Spac)emacs now. :smile:

2 Likes
#6

Be aware though, that it now does only check for updates on reboot, manual restart using systemctl or when using M-x whatever-spacemacs-invokes-to-check-for-update. I’m not even sure if you can interact with spacemacs in the way necessary to confirm or deny an update.

Please take care of this in your surrounding configuration.

2 Likes
#7

Here’s some of what I have:

#Elixir Phoenix
alias mps="mix phx.server"
alias ism="iex -S mix"
alias mt="mix test"
alias mts="mix test --stale"
alias mec="mix ecto.create"
alias mem="mix ecto.migrate"

#Git
alias gri="git rebase -i origin/master"
alias grif="git commit -am 'f' && git rebase -i origin/master"
alias gpf="git push origin $(git branch) --force-with-lease"
alias gs="git status"

#Vim
alias rmntl="find . -name '*Netrw*' -exec rm -f {} \;"

# display current git branch
parse_git_branch() {
     git branch 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/ (\1)/'
}

# Customize prompt to show only working directory and git branch.
export PS1="[\W]\[\033[34m\]\$(parse_git_branch)\[\033[00m\] $ "
3 Likes
#8

Most of the time I use plugins from Sublime Text or Neovim to interact with Git, but here are a few aliases and functions. :sweat_smile:

# Aliases
alias nv="nvim"

alias g="git"
alias ga="git add"
alias gaa="git add --all"
alias gb="git branch"
alias gc="git commit -m"
alias gca="git commit -a -m"
alias gcl="git clean -fd"
alias gcm="git checkout master"
alias gco="git checkout"
alias gcp="git cherry-pick"
alias gd="git diff"
alias gfo="git fetch origin"
alias ggpull="git pull origin $(git_current_branch)"
alias ggpush="git push origin $(git_current_branch)"
alias ghh="git help"
alias gl="git log"
alias gm="git merge"
alias gmom="git merge origin/master"
alias gp="git push"
alias gr="git reset"
alias grb="git rebase"
alias grh="git reset --hard"
alias gst="git status"
alias nah="git reset --hard && git clean -df"

alias mr="mix run"
alias mt="mix test"
alias ism="iex -S mix"
alias mdc="mix deps.clean"
alias mdg="mix deps.get"
alias mec="mix ecto.create"
alias mer="mix ecto.reset"
alias mes="mix ecto.setup"
alias mpr="mix phx.routes"
alias mps="mix phx.server"

# Functions
function zshconfig {
    nvim ~/.zshrc
    source ~/.zshrc
    echo "ZSH config was reloaded."
}

function gclone {
    git clone "https://github.com/${2:-$(whoami)}/$1.git"
}

function ginit {
    git init
    git add --all
    git commit -m ${1:-"Init repo"}
}
3 Likes
#9

Oh lord, this one is a mess. I’ve went and added a bunch of comments so it’s not entirely horrible to read through.

#
# ~/.bash_profile
#

# Save working directory after cd-ing so I can hop back into
# the same working directory on login (see `~/.bash_login` in a bit).
function cd {
    builtin cd "$@"
    pwd > ~/.bash_lastcwd
}

__prompt_command() {
    if [[ $? -eq 0 ]]; then
        # On success of the last command, paint the `$` green.
        local colour="32"
    else
        # On error, paint it red.
        local colour="31"
    fi

    # If `.python-version` exists in the current working directory, show the Python version in the
    # prompt. This uses pyenv: https://github.com/pyenv/pyenv
    if [[ -e .python-version ]]; then
        local additional=' via \[\e[33m\]'"$(<.python-version)"' \[\e[0m\]'
    fi
    PS1='\n\w'"$additional"'\n\[\e['"$colour"'m\]$ \[\e[0m\]'
}


export PROMPT_COMMAND=__prompt_command

# Rust tools
export PATH="$HOME/.cargo/bin:$PATH"
# Custom binaries
export PATH="$PATH:~/.bin"
# Up-to-date coreutils to override Mac's builtin coreutils
export PATH="/usr/local/opt/coreutils/libexec/gnubin:$PATH"
# Up-to-date manpages for coreutils from above
export MANPATH="/usr/local/opt/coreutils/libexec/gnuman:$MANPATH"
# Probably some dumb experiments
export SSH_ASKPASS="/usr/local/bin/ssh-askpass"
# load z: https://github.com/rupa/z
source /usr/local/etc/profile.d/z.sh
# load hub: https://github.com/github/hub
source ~/.bash_complete/hub.sh
# setup my borg repository for backups
export BORG_REPO='borgbackup@192.168.0.241:/media/borgbackup/mac/main'
# along with non-interactive passphrase retrieval
export BORG_PASSCOMMAND='pass rpi_borg'
# no idea what these two do again
export HISTSIZE=
export HISTFILESIZE=1000000

# I am one lazy guy
alias ..='cd ..'
alias ...='cd ../..'
alias ....='cd ../../..'
# I think this fixed some `gpg` issues at some point
export GPG_TTY=$(tty)
# Database URL for a command-line collaborative issue tracker I'm building
export TM_DATABASE_URL=postgres://Johannes:@localhost/tm

# Setup hub
eval "$(hub alias -s)"

# ??
. ~/.bash_completion

# load the login prompt (see below)
source ~/.bash_login
# custom `vi` and `vim` with no config for quick editing in `git` an others
source ~/.bash_aliases
# pyenv setup (copied from the repo I believe)
if command -v pyenv 1>/dev/null 2>&1; then
  eval "$(pyenv init -)"
fi

export LANG=en_US.UTF-8
# load z again (???)
. /usr/local/etc/profile.d/z.sh

I feel like you can learn some software development lessons from this one :smile:
Here’s my accompanying ~/.bash_login, basically just checks some birthday tracker thing I wrote (to learn Rust) for whose birthday it is and displays a random quote from fortune.

rusday dashboard
fortune | cowsay

Hey, this one’s clean at least.

Also @AstonJ, seeing your aliases for moving around, you might want to take a look at z - it’s a huge timesaver for me for this common cd-ing around :smile:

1 Like
#10

I’m glad I started this thread now - definitely pinching stuff from it :lol:

:003:

Thanks, I will check it out :023: Usually tho, I use those at a start of a session and tend to keep windows per project with multiple tabs (so one main one in the project directory, another for iex and another for phx.server of that project, etc)

2 Likes
#11

Nothing better than good mechanical keyboard and and shell :smiley:
This is why I :heart: Linux
My shell


My nice commands replacement




Something to inspire



https://jvns.ca/blog/2017/04/23/the-fish-shell-is-awesome/

1 Like
#12

Don’t know if Bash has something similar, but I use Oh My ZSH and we have a good one for cd:

cd ..   # one directory up
cd ...  # two directories up
cd .... # three directories up and so on...

Come to the dark side, we have cookies and fancy plugins. :smiling_imp:

2 Likes
#13

The only things in mine at the moment are shortcuts to switch between heroku accounts, some code to set the iterm tab colour and this journal command I stole from somewhere that lets me create a daily journal on my iCloud drive.

My .gitconfig has a ton of shortcuts though I again stole from somewhere that have made working with branches a lot easier.

journal() {
  mkdir -p ~/Library/Mobile\ Documents/com~apple~CloudDocs/journal/`date +%Y`
  $EDITOR ~/Library/Mobile\ Documents/com~apple~CloudDocs/journal/`date +%Y`/`date +%m-%d`
}
alias j=journal
2 Likes
#14

There’s shopt autocd. To quote the manpage:

              autocd  If set, a command name that is the name of a directory is executed as if it were the argument to the cd command.
                      This option is only used by interactive shells.

So for instance, running .. will put you one directory up. Doesn’t work for multiple parent directories though.
There’s also other fancy options here like cdspell or dirspell. I never really looked much into all these options but I believe you can customize bash quite a lot using them :slight_smile:

1 Like
#15

This is how my .bash_profile file looks:

# /etc/skel/.bash_profile

# This file is sourced by bash for login shells.  The following line
# runs your .bashrc and is recommended by the bash info pages.
[[ -f ~/.bashrc ]] && . ~/.bashrc

Well … hello from Gentoo/Funtoo world :077:

Now seriously here is a bit of my bash code (can’t show all):

if [[ $- != *i* ]] ; then
	# Shell is non-interactive.  Be done now!
	return
fi

# git

export EDITOR="nano"
export VISUAL=$EDITOR

# asdf base

. $HOME/.asdf/asdf.sh
. $HOME/.asdf/completions/asdf.bash

# Erlang

export ERL_AFLAGS="-kernel shell_history enabled"
export KERL_BUILD_DOCS=yes

# Elixir

ASDF_CURRENT_ELIXIR=`asdf current elixir | awk -F"(" '{print $1;}'`
ASDF_MIX_DIR="~/.asdf/installs/elixir/$ASDF_CURRENT_ELIXIR/.mix"
export PATH="$PATH:$ASDF_MIX_DIR:$ASDF_MIX_DIR/escripts/"

# Secrets

# …

# Private projects data

# …

# Aliases

alias git_branch="git symbolic-ref --short HEAD"
alias git_head_revision="git rev-parse HEAD"
alias git_number_of_changed_files="git diff --shortstat | xargs | grep -Po '\d+' | sed -n 1p"
alias git_number_of_deleted_files="git ls-files --deleted --exclude-standard | wc -l"
alias git_number_of_deletions="git diff --shortstat | xargs | grep -Po '\d+' | sed -n 3p"
alias git_number_of_insertions="git diff --shortstat | xargs | grep -Po '\d+' | sed -n 2p"
alias git_number_of_modified_files="git ls-files --exclude-standard --modified | wc -l"
alias git_number_of_others_files="git ls-files --exclude-standard --others | wc -l"
alias git_origin_url="git config --get remote.origin.url"
alias git_repo="git_origin_url | sed -E 's/^git@|^git:\/\/|^https:\/\//\\\\e[93m/' | sed 's/:/ \\\\e[94m/' | sed 's/\// \\\\e[96m/' | sed -E 's/.git$/\\\\e[39m/'"
# …

# Prompt

set_prompt_vars() {
  FULL_DATE=$(date '+\e[38;5;172m%-V\e[39m|\e[38;5;143m%-j \e[39m\e[1m%A\e[21m, \e[96m%_d %B %Y\e[39m - \e[95m%-H:%M:%S:%N \e[93m%Z\e[39m \e[38;5;244m(%-::z)\e[39m')
  GIT_INFO=""
  if git rev-parse --is-inside-work-tree > /dev/null 2>&1
  then
  GIT_INFO="\
$(git_repo)\n\
\e[95m$(git_branch)\e[97m ($(git_head_revision)): \
\e[93m$(git_number_of_changed_files)C\e[39m-\
\e[91m$(git_number_of_deleted_files)D\e[39m-\
\e[95m$(git_number_of_modified_files)M\e[39m-\
\e[92m$(git_number_of_others_files)N\e[39m\
\e[97m | \
\e[92m+$(git_number_of_insertions)\e[39m/\
\e[91m-$(git_number_of_deletions)\e[39m\n"
  fi
  
  PROMPT_LINE="\e[32m\w \e[34m|> \e[97m"
  export PS1="\n$FULL_DATE\n$GIT_INFO$PROMPT_LINE"
}
PROMPT_COMMAND=set_prompt_vars

# and so on …

For sure I have removed lots of parts as it’s (at least not yet) right time to show them + it’s a really long file (1200+ lines spitted into multiple script files with custom functions, aliases and so on …). :smiley: Rest of my ~/.bashrc for now is still top secret. :icon_wink:

1 Like