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!

Top comments (45)

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
 
nocnica profile image
Nočnica Mellifera

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
 
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
 
bhupesh profile image
Bhupesh Varshney 👾

I am stealing the duckduckgo one 🔥
Thanks

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

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

Whaaaaat I did not know this.

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