DEV Community

Nočnica Mellifera for Heroku

Posted on

What are your preferred bash aliases?

Bash aliases are great ways to increase productivity by bundling commands behind a single alias. What are your favorites? Share in the comments!

Latest comments (45)

Collapse
 
jrbrtsn profile image
John Robertson

The next time you are debugging a bash script, give this one a try:

PS4='+ $BASH_SOURCE:$LINENO '
set -xv
Enter fullscreen mode Exit fullscreen mode
Collapse
 
vtvh profile image
Hải
# Clone and edit function
cled() {
    local dir_name clone_dir
    clone_dir=~/github
    dir_name=$(echo "$1" | cut -d '/' -f5)
    echo "$dir_name"

    test -d $clone_dir && cd $clone_dir || mkdir $clone_dir; cd $clone_dir
    git clone --depth=1 $1
    cd $dir_name && code ./
}

I use this function to quickly (shallow) clone a github repo and open it with my favorite editor.

Collapse
 
eljayadobe profile image
Eljay-Adobe • Edited

I like to make my ~/.bash_profile able to be re-sourced multiple times. Because I'll change it, and then re-source it. But I don't want my PATH to become super long with duplicates. So I check PATH before appending or prepending a path.

[[ ":${PATH}:" == *":/usr/local/bin:"* ]] || PATH="/usr/local/bin:${PATH}"
Enter fullscreen mode Exit fullscreen mode

I use SCOWL to look up words frequently. So I like to be able to grep words no matter where I am.

function scowl {
  pushd /Users/eljay/bin/scowl-2018.04.16/final >/dev/null
  grep "$@" *
  popd >/dev/null
}
Enter fullscreen mode Exit fullscreen mode

I'm often on a Macintosh using Terminal.app, and I like to be able to clear the screen and clear the scrollback buffer in one command.

alias cls="clear; printf '\033[3J'"
Enter fullscreen mode Exit fullscreen mode

I'm always making little C++ toy programs to test out a thought. I like to compile in a specific way, so a handy alias to the rescue.

export CXXWARN='-Weverything -Wno-c++98-compat -Wno-c++98-compat-pedantic -Wno-padded -pedantic -fsanitize=undefined,null,address,bounds,bool,enum'
alias c17='/usr/bin/clang++ ${CXXWARN} -std=c++17'
Enter fullscreen mode Exit fullscreen mode

I often want to go up directory levels or to HOME quickly.

alias ..='cd ..'
alias ..2='cd ../..'
alias ..3='cd ../../..'
alias ~='cd ~'
Enter fullscreen mode Exit fullscreen mode

When I do mkdir I often follow it with cd to that directory. So I've combined the operations. (Sometime I tweak it to be lazy and cd into the directory if it already exists. Sometimes I don't like that behavior, and take it out.)

function mkcd {
  if [ -n "$1" -a ! -a "$1" ]
  then
    mkdir "$1"
    cd "$1"
  else
    echo NO
  fi
}
Enter fullscreen mode Exit fullscreen mode

Sometimes I'm editing an HTML file (and possibly associated CSS and JavaScript files), and I want Chrome to reload my local page for me when it changes. (I think I got fswatch through brew.)

function watch {
    local DIR=$PWD

    if [ -n "$1" ]
    then
        DIR="$1"
    fi

    fswatch -o "$DIR" | xargs -n1 -I {} osascript -e 'tell application "Google Chrome" to tell the active tab of its first window to reload'
}
Enter fullscreen mode Exit fullscreen mode
Collapse
 
oinak profile image
Oinak

.. is cd ..
e is $EDITOR (gvim on Linux, mvim on Mac)
e. is e . to open $EDITOR on the current folder
g is git and I use git aliases ci co pus pul for sub-commands
dk is docker and dc is docker-compose
And we have a rich bin folder on each project with specific scripts like bin/qa_deploy_branch etc

Collapse
 
crcastle profile image
Chris Castle • Edited

The usuals:

alias g='git'
alias h='heroku'

A shortcut: c to be able to quickly jump to any of my local project directories inside my ~/Code directory.

Fish shell function:

function c
  if test -z $argv
    cd ~/Code
  else
    cd ~/Code/$argv
  end
end

Full source (including tab completion) installable using Fisher.

Also, vpn to connect to or disconnect from corporate (Cisco AnyConnect) VPN (source).

Collapse
 
nflamel profile image
Fran C.

I think that my 2 favortites and the 2 I use more are, no doubt:

alias e='${(z)VISUAL:-${(z)EDITOR}}'
alias g='git'
Collapse
 
nocnica profile image
Nočnica Mellifera

Wait, what is that first one foist?

Collapse
 
nflamel profile image
Fran C. • Edited

I didn't know what it was doing until you asked. I've had it copy/pasted around my dot-files many times. I've just realized now that it does not work on BASH, only on ZSH, sorry :(

EDIT: I mean... I know I always use e to open my editor. What I didn't know was why was I using such a complex alias to do it.

TL;DR:

It uses $VISUAL or $EDITOR and makes sure that it properly handles any extra parameter given to it. So somehting line vim -a -b -c works.

Detailed explanation

Got it from here github.com/sorin-ionescu/prezto/bl...

What it does If I'm not mistaken is to alias e to either the value of $VISUAL or $EDITOR. But to only do that it would be enough with:

alias e='${VISUAL:-${EDITOR}}`

There's also this (z) that I just learned on ZSH is an expansion flag. Looking into ZSH manual it says:

z

    Split the result of the expansion into words using shell parsing to find
    the words, i.e. taking into account any quoting in the value. Comments are
    not treated specially but as ordinary strings, similar to interactive
    shells with the INTERACTIVE_COMMENTS option unset (however, see the Z flag
    below for related options)

    Note that this is done very late, even later than the ‘(s)’ flag. So to
    access single words in the result use nested expansions as in
    ‘${${(z)foo}[2]}’. Likewise, to remove the quotes in the resulting words
    use ‘${(Q)${(z)foo}}’.

but I still didn't know what it means... so I tried to do:

VISUAL='nvim -v' # This just writes the nvim version
alias e='${VISUAL:-${EDITOR}}'

If I then I've tried

$ e
zsh: command not found: nvim -v

That doesn't happen if I try with (z) flag on the expansion alias e='${(z)VISUAL:-${(z)EDITOR}}'.

PS: Thanks a lot for making me look it up :)

Thread Thread
 
nocnica profile image
Nočnica Mellifera

Please write this up into an article or I will, it’s great! And since OS X switched from Bash to zsh I Can actually use it!

Thread Thread
 
nflamel profile image
Fran C.

Done! dev.to/nflamel/til-how-my-complica...

I'll try to see if I have more things like that around my dot-files to see which ones are worthy of some more articles.

Collapse
 
josephj11 profile image
Joe

Here are a few of mine:

Make log times human readable

alias dmesg='dmesg -T'

Remove editor backup files

alias rmtl='rm -f *~'

And a couple of functions (in ~/.bashrc)

Change to my ~/bin directory

bin() {
cd $HOME/bin
echo -en "\t\t"
pwd
}

Quick calculation

## Command line calculator
calc() {
    bc -l <<< "$@"
}

I have several aliases that just cd to directories I use a lot.

I have a simple activity log program I wrote in bash that writes to a text file. It has eleven aliases to invoke its features and to create entries for my most common activities that I can just tack details on the end of.

Collapse
 
rafed123 profile image
Rafed Muhammad Yasir

rafed.github.io/devra/posts/termin...

You might find the section on aliases interesting.

Collapse
 
victorysokolov profile image
Viktor Sokolov

One of my favorite aliases is to get a quick answer from StackOverflow
using howdoi package

sudo apt-get install howdoi
alias how="howdoi $* -c"

The cross-platform solution to open current directory form terminal

if [ ! $(uname -s) = 'Darwin' ]; then
    if grep -q Microsoft /proc/version; then
        # Ubuntu on Windows using the Linux subsystem
        alias open='explorer.exe';
    else
        alias open='xdg-open';
    fi
fi

more of my settings can be found here: github.com/victory-sokolov/dotfiles

Collapse
 
josephj11 profile image
Joe

If you like howdoi, check out tldr. It works like man, but only shows usage examples. It's in the regular Ubuntu repos.

Collapse
 
nocnica profile image
Nočnica Mellifera

Definitely going to use this.

Collapse
 
cod3slinger profile image
Cod3slinger • Edited

I'm always a fan of:

alias please='sudo $(fc -ln -1)'

That is, rerun the last typed command, but prepend it with 'sudo' this time.

Collapse
 
matt profile image
Matt seymour • Edited
alias was-it-me=git blame
Collapse
 
darkwiiplayer profile image
𒎏Wii 🏳️‍⚧️ • Edited

Not really a bash alias, but

git config --global alias.dog "log --decorate --oneline --graph"

is quite useful :D

oh, also this one

alias qed='[ $RANDOM -ge $((32767 / 100 * 10)) ] && echo Quod Erat Demonstrandum || echo Quo Errat Demonstrator'
Collapse
 
mishal23 profile image
Mishal Shah • Edited

I've noted down all the aliases I use here: link

My favorite ones are:

function lazygit() {
   local message="$1"
   local push_branch="${2:-$(git rev-parse --abbrev-ref HEAD)}"
   echo "Commit Message: $message"
   echo "Push Branch: ${push_branch}"
   git add --all
   git commit -m "$message"
   git push origin "$push_branch"
}

alias lazygit=lazygit

alias fullupdateandclean='sudo apt-get update && sudo apt-get upgrade && sudo apt-get dist-upgrade && sudo apt autoclean && sudo apt autoremove'
Collapse
 
nocnica profile image
Nočnica Mellifera

really like 'lazygit' I may steal that one :)

Collapse
 
mishal23 profile image
Mishal Shah

Sure :D

Collapse
 
nksprzak profile image
Nick Kasprzak

So my favorite is a git alias. When I have PR request open and I get comments on it and need to address them and then commit and rebase them.

The need for rebasing the last commit is to squash the fixing comments commit into the one that the PR is actually for, ie. Add some feature

I do this all the time and with git when you rebase it opens up your text editor so you can edit the commits you are rebasing to tell git whether you want to squash, drop, edit the commit messages.

I found it annoying that you have to manually type go down and change pick to s every time I had to address PR comments. So I found a Stack Overflow answer and turned that into this alias.

gg='ga && git commit --fixup=HEAD && GIT_SEQUENCE_EDITOR=: git rebase HEAD~2 -i --autosquash'

This improved my life a lot. Try it for yourself if you want and if anything gets messed up remember git reflog and git reset HEAD@{#} are your friends.

Collapse
 
adam_cyclones profile image
Adam Crockett 🌀 • Edited

I don't remember my own aliases so I just stick to out of the box docs.