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!

Oldest comments (45)

Collapse
 
thefluxapex profile image
Ian Pride

I pretty much skip aliases and only make functions most of the time. I do have exceptions, but for the most part I have a "master" .bash_funcs config that I use on most of my machines that I have been building/editing/refactoring for MANY years that has all my favorite stuff in it.
Even for non-args stuff.
It's really hard to pick; so much of it saves time or keystrokes and I find most of them essential.
If you consider small one liner or simple functions as aliases then a few I love are:

Alternate touch

Use null to zero/create a file.

function touchf(){ [[ $# -gt 0 ]] && cat /dev/null > "$*"; }

Get cursor coordinates with xdotool

function getmousecoords(){
        xdotool getmouselocation|awk \
                '{sub(/^x:/,"",$1);\
                sub(/^y:/,"",$2);\
                print $1 " " $2}'
}

Use realpath with Fuzzy Finder to get the a full path (alternate method)

function realfzf() { find "$(realpath $*)" -iname "*" | fzf; }

I have a much smaller alias file (< 10 old aliases that plan to functionize at some point lol)

Always force make parent directories if they don't exist

alias mkdir='mkdir -p'

'ls' with no color for various reasons

I usually either have 'ls' aliased to default list with colors (I think it's preset in some envs) and this overrides it when I need output without color

alias lsn='ls --color=never'

And one for fun :D Show time in the console but in alternating colors.

I have plans to rewrite this using '\r' instead of clear

alias colortime='while :; do for i in {{30..37},{90..97}}; do echo -en "\e[0;${i}m$(date +%r)\e[0m ";sleep 1;clear; 
done; done'

I have so many functions I love I could just keep going, but my main function file is around 8500 lines so I won't do that to you lol.

Collapse
 
wulymammoth profile image
David • Edited

Do you have these functions in a public repository? I'd love to check them out.

I've a handful of functions that I rarely use anymore like one that took an argument like docs mdn that would open my browser to Mozilla's documentation and docs til that would open my TIL (today I learned) repository on GitHub with rendered markdown.

I've only a handful of aliases as well allowing me to jump around (change dirs) into personal or work, and my dotfiles along with a couple more just masking other commands, like alias vim=nvim since I use Neovim and alias make=mmake (Modern Make)

I keep a long history and much prefer using reverse-i-search to dig up a command that I use somewhat frequently

Collapse
 
thefluxapex profile image
Ian Pride • Edited

I do actually plan on a Shell section on my main misc programming GitHub IO site that has lots of it organized, but it's still "under construction" lol. In the next couple of days I'll make a copy and clean lots of crap and redundancy out of it and put it up in a temp repo and post it back here.

I use history/fzf, but I have extra laptops that I'm always testing distros on and so I always just import my dots, but yeah I'm constantly doing Ctrl+r; FZF is just amazing.

Thread Thread
 
wulymammoth profile image
David

Don’t have to mention fzf twice. It’s a must have both in my shell and Vim coupled with ripgrep. Ah! You’re a distro junkie! Haven’t settled on Arch or Gentoo?

Thread Thread
 
thefluxapex profile image
Ian Pride

I try anything out, but stick to Debian based. Ubuntu on main/family member machines usually. I do have a soft spot for Bodhi with Enlightenment.

Collapse
 
moopet profile image
Ben Sinclair • Edited

Use null to zero/create a file

I'd do :> foo to do this (well, I use :>| foo because I have noclobber set, but...)

As for needing to output ls without colour, I think color=auto works for most colour-aware commands in that you get colours, but they're suppressed if the output's to a pipe. That should cover most cases?

Collapse
 
thefluxapex profile image
Ian Pride

The reason why I originally created that alias is because at some point, for whatever glitched reason, I couldn't force colors off and that was the only way I could force it off. Don't remember exactly what it was; it's been a few years and my memory has been slipping lately.

Collapse
 
alchermd profile image
John Alcher

I've aliased pt to a long string of a Python test runner (formerly on PHPUnit as well). I also alias py to whatever version of Python I prefer working with.

Collapse
 
amritanshupandey profile image
Amritanshu Pandey • Edited

One alias that I use many many times every day:

‘’’
alias cdtemp='cd $(mktemp -d)'
‘’’

It creates a new temporary directory and switch to that directory.

Collapse
 
nocnica profile image
Nočnica Mellifera Heroku

interesting! is that just for experimenting with files you might not keep? I'm always curious how others use filesystems, I make a lot of temp files but don't ever make a temp directory to put them in!

Collapse
 
josephj11 profile image
Joe

I don't either, but it's a cool idea because you can create a bunch of files for something and delete them all when you're done just by deleting the directory they're in. Also makes it so you don't have to worry about file name collisions between tasks.

Thread Thread
 
amritanshupandey profile image
Amritanshu Pandey

Yes thats exactly how I use this alias. Another benefit is that in most of linux distros the ‘/tmp’ directory is cleared upon each reboot.

Thread Thread
 
nocnica profile image
Nočnica Mellifera Heroku

Whaaaaat I did not know this.

Collapse
 
vonheikemen profile image
Heiker • Edited

Oh no I don't have favorites, but if I did it would be these.

alias -- -='cd -'

alias tvi='tmux new-session -A -D -s vi'
alias tmus='tmux new-session -A -D -s music "$(which cmus)"'
alias pomd='tmux new-session -A -D -s pomodoro'
alias pmd-start='pomd gone -e "notify-send -u critical Pomodoro Timeout"'

alias npr='pnpm run'

Now on the subject of functions I recently added these two.

# Transform the arguments into a valid url querystring
urlencode()
{
  local args="$@"
  jq -nr --arg v "$args" '$v|@uri'; 
}

# Query duckduckgo
duckduckgo()
{
  lynx "https://lite.duckduckgo.com/lite/?q=$(urlencode "$@")"
}

That duckduckgo has ? as an alias, so I can search from the terminal like this.

? how do I get out of vim

If anyone is curious I have those things in my dotfile repo.

Collapse
 
bhupesh profile image
Bhupesh Varshney 👾

I am stealing the duckduckgo one 🔥
Thanks

Collapse
 
wulymammoth profile image
David

Nice! I literally spend all my time in my shell and am not sure why I hadn’t thought of this or employ Alfred (macOS). May try this and see if it sticks

Collapse
 
nocnica profile image
Nočnica Mellifera Heroku

I really love the duckduckgo one

Collapse
 
skipadu profile image
Sami Korpela

I'm curious how you set up the ? as an alias for the duckduckgo ?
I couldn't get it working when I tried and can't find that from your dotfiles either. I'm pretty new with these aliases.

Collapse
 
vonheikemen profile image
Heiker • Edited

I believe this is what you are looking for.

alias '?'='duckduckgo'

It's not on the dotfiles because I don't know if I'm going to keep it.

Thread Thread
 
skipadu profile image
Sami Korpela

Thanks! I did not have the single quotes on the ?, TIL :)

Thread Thread
 
vonheikemen profile image
Heiker

You're welcome.

This also seems to work.

alias -- ?='some-command'
Collapse
 
rafi993 profile image
Rafi
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.

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
 
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 Heroku

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

Collapse
 
mishal23 profile image
Mishal Shah

Sure :D

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
 
matt profile image
Matt seymour • Edited
alias was-it-me=git blame