DEV Community

What are your favorite bash aliases?

Roelof Jan Elsinga on February 21, 2020

What are your favorite bash aliases? I use a lot of bash aliases on a daily basis! I love the fact that you can bundle commands and acti...
Collapse
 
whusterj profile image
William Huster • Edited

I use Ubuntu -- I have a ~/scripts directory, where I put useful scripts. I then alias these for quick use.

Here's a shortcut to open any VSCode workspace saved in ~/workspace/vscode-workspaces/

#!/bin/bash
#
# Author: William Huster
# Description: Shortcut to open a VSCode workspace
#
code $HOME/workspace/vscode-workspaces/$1.code-workspace

In ~/.bashrc:

alias workspace="bash $SCRIPTS_DIR/vscode-workspace.sh"

I also have a git-versioned folder for notes in markdown. I like to create a file for daily notes, so I have note alias and script to create/open that file:

#!/bin/bash
#
# Author: William Huster
# Description: Shortcut to launch an editor with my today's notes.
#
WORKSPACE_DIR=~/workspace
NOTES_DIR=~/notes

cd $NOTES_DIR

# Make the filename
NOW=$(date +"%Y-%m-%d")
NOTE_TITLE="__Notes.md"
todays_notes="$NOW$NOTE_TITLE"

# Open it!
code $WORKSPACE_DIR/vscode-workspaces/notes.code-workspace "$todays_notes"

In ~/.bashrc:

alias note="bash $SCRIPTS_DIR/take-note.sh"
Collapse
 
roelofjanelsinga profile image
Roelof Jan Elsinga

That's definitely something I should do! Such great ideas, thank you for these!

Collapse
 
waylonwalker profile image
Waylon Walker

This one is my favorite. I use conda environments and switch between them many times per day. It pipes a list of available environments into fzf, trims out just the name then runs source activate on it.

a() {source activate "$(conda info --envs | fzf | awk '{print $1}')";}
Collapse
 
dak425 profile image
Donald Feury • Edited

Some generic aliases I use often

alias poweroff="systemctl poweroff"
alias reboot="systemctl reboot"
alias v="vim"
alias sv="sudo vim"

For updating xrdb resource definitions quickly

alias xup="xrdb -merge ~/.Xresources"

For navigating to common directories

alias gtproj="cd ~/projects"
alias gtconf="cd ~/.config"

For quickly editing configs I touch frequently

alias exr="vim ~/.Xresources"
alias evim="vim ~/.vimrc"
alias ezsh="vim ~/.zshrc"
alias esxhkd="vim ~/.config/sxhkd/sxhkdrc"
alias ebspwm="vim ~/.config/bspwm/bspwmrc"
alias edunst="vim ~/.config/dunst/dunstrc"
alias epolytop="vim ~/.config/polybar/top.ini"
alias epolybot="vim ~/.config/polybar/bottom.ini"
alias epolycol="vim ~/.config/polybar/colors.ini"
alias epolyuser="vim ~/.config/polybar/user_modules.ini"
alias epolysys="vim ~/.config/polybar/system_modules.ini"
Collapse
 
roelofjanelsinga profile image
Roelof Jan Elsinga

Those are different than mine, but definitely have the same idea! I haven't posted them here, but I have a lot of "cd" like aliases and "edit file" commands in there as well! Very useful aliases, nice!

Collapse
 
mediumrowdy profile image
medium-rowdy

Not too many, as I move around between systems quite a lot. The couple I can't do without are:

alias ll="ls -l"
alias la="ls -la"

And to help with finding large directories/files:

alias du1="du -k -d 1 | sort -n"
alias ll5="ls -la | sort -k 5 -n"
Collapse
 
roelofjanelsinga profile image
Roelof Jan Elsinga

100% adding those to my list!!

Collapse
 
gianpaj profile image
Gianfranco P.
alias run="npm run"

alias cat='bat'
alias ping='prettyping --nolegend'
alias top="htop" # alias top and fix high sierra bug
alias du="ncdu --color dark -rr -x --exclude .git --exclude node_modules"

# requires brew info grep
alias grep="ggrep  --color=auto --exclude-dir={.bzr,CVS,.git,.hg,.svn}"
Enter fullscreen mode Exit fullscreen mode
Collapse
 
alexanderalemayhu profile image
Alexander Alemayhu • Edited

I use git-amn a lot to reword the last commit or append some hunks

alias git-amn='git ci -v --amend'

My favourite function which I use a few times a year to study codebases for fun and profit 😉

# Clone a whole github organization.
orgclone() {
  mkdir -p $1
  cd $1
  curl -s https://api.github.com/orgs/$1/repos?per_page=200 | ruby -rubygems -e 'require "json"; JSON.load(STDIN.read).each { |repo| %x[git clone #{repo["ssh_url"]} ]}'
  cd -
}
Collapse
 
roelofjanelsinga profile image
Roelof Jan Elsinga

I'm adding that amend alias to my list too, that's great! That method is really cool and takes care of so much manual work! Productivity 😅

Collapse
 
aetherunbound profile image
Madison Swain-Bowden

Good topic Roelof!

I've got some similar ones, with one minor difference:

alias ebrc='vim ~/.bashrc && source ~/.bashrc'

This makes it so that my .bashrc is sourced right after I finish editing it!

I've also got a few convenience functions for common operations:

cdl_function () {
    cd $1 && ls -lah
}

mkdirc_function () {
    mkdir $1 && cd $1
}

alias cdl=cdl_function
alias mkdirc=mkdirc_function

And some conda specific ones:

# Tab completed virtual environments
# e.g. `vact <tab>`
vact () {
    conda activate ${1}
}
__vact()
{
    local cur=${COMP_WORDS[COMP_CWORD]}
    IFS=$'\n'
    tmp=( $(compgen -W "$(ls ${CONDA_PREFIX}/envs/ )" -- $cur))
    # COMPREPLY=( "${tmp[@]// /\ }" )
    COMPREPLY=( "${tmp[@]}" )
}
complete -F __vact vact

# List environments
alias le="conda info --envs"
Collapse
 
roelofjanelsinga profile image
Roelof Jan Elsinga

Oh I'm definitely adding those first three! They cover about 99% of my manual actions automatically, great aliases!

The tab completion I'm not familiar with, but that does look like some amazing productivity hacks!

Collapse
 
mzaini30 profile image
Zen

alias? I use bash files to run commands like alias.

Example:

  • Create file in ~/bin/ (eg: hello file)
  • Run sudo chmod +x hello
  • Type command in hello
  • Then, if I'll run the command, I type hello (like alias)

And these're my fav:

github

git clone --depth=1 user:pass@github.com/user/$1

upload

git status
git add -A .
git commit -m "upload"
git push
Collapse
 
roelofjanelsinga profile image
Roelof Jan Elsinga

That's a good idea as well! Perhaps a cleaner solution as well. I like aliases because I can add them to my setup bash script for new systems, append them to the .bashrc, source it and I'm good to go! I'll definitely look into your solution though, because that sounds very clean and allows for much more complex workflows as well.

Collapse
 
waylonwalker profile image
Waylon Walker

I have replaced a few of my aliases with forgit. a package of several really good fuzzy git commands.

ga -> git add

git add

Collapse
 
roelofjanelsinga profile image
Roelof Jan Elsinga

That's a cool solution! I've seen many people use the ga command as well and always assumed it was manually done through aliases. This takes the manual work out of it, very exciting!

Collapse
 
waylonwalker profile image
Waylon Walker

I actually had a set of fzf commands that I had made personally that this replaces as it is way more engineered and robust than what I came up with.

Collapse
 
akkis profile image
Akis Laoutaris

Your idea for alias sharing is great! Thank you all of you!
I personally use:

alias l='ls -lah'
alias brewup='brew update && brew upgrade && brew cleanup && brew doctor'
Collapse
 
gathuku profile image
Moses Gathuku

Made this repo,few months ago.
github.com/gathuku/bash_alias

Collapse
 
roelofjanelsinga profile image
Roelof Jan Elsinga

Wow that is amazing! So clean and well documented, great job!

Collapse
 
gathuku profile image
Moses Gathuku

Thanks

Collapse
 
33nano profile image
Manyong'oments

doas=sudo

Collapse
 
denshadev profile image
DenshaDev

I've got

alias a='ng serve'

to launch my Angular local dev server. 😄

Collapse
 
roelofjanelsinga profile image
Roelof Jan Elsinga

Short but sweet, I love it!

Collapse
 
henrikwirth profile image
Henrik Wirth
alias g='git'
alias gg='cd ~/path/to/git/folder'
Collapse
 
roelofjanelsinga profile image
Roelof Jan Elsinga

Nice shirt ones! Probably helps a lot with your productivity as well!