Ask HN: Share your favourite bash/zsh aliases
alias o="open"
alias x="exit"
alias g="git"
alias gs='git status'
alias gd='git diff'
alias g-='git checkout -'
alias serve='python -m SimpleHTTPServer 8000'
alias mem='top -l1 | grep PhysMem'
alias o="open"
alias x="exit"
alias g="git"
alias gs='git status'
alias gd='git diff'
alias g-='git checkout -'
alias serve='python -m SimpleHTTPServer 8000'
alias mem='top -l1 | grep PhysMem'
function up {
if [[ "$#" < 1 ]] ; then
cd ..
else
CDSTR=""
for i in {1..$1} ; do
CDSTR="../$CDSTR"
done
cd $CDSTR
fi
}
I can just run "up" to "cd ..", or I can run "up 6" to "cd ../../../../../.." # Fast shortcuts
alias c='clear'
alias r='reset'
alias q='exit'
alias cd..="cd .." # I often make this mistake
# Because sometimes you don't have the time to put this two letters
alias ..='cd ..'
alias ...='cd ../..'
alias -- -="cd -"
# Doing a fast proxy, good for watching netflix and youtube without restrictions
alias proxy='ssh -C2qTnN -D 8080'
# To search process with more details
alias pp="ps axuf | pager"
# Because I forget the name :(
alias explore="ranger"
# Because NeoVim is awesome :)
alias vim=nvim
# Do something and receive a desktop alert when it completes `sudo apt-get install something | alert`
alias alert='notify-send --urgency=low -i "$([ $? = 0 ] && echo terminal || echo error)" "$(history|tail -n1|sed -e '\''s/^\s*[0-9]\+\s*//;s/[;&|]\s*alert$//'\'')"'
# Search process by name and highlight !
function psgrep() { ps axuf | grep -v grep | grep "$@" -i --color=auto; }
# Search for files and page it
function search() { find . -iname "*$@*" | less; }shopt -s autocd
# eXpand anything
alias xx="atool -x"
( http://www.nongnu.org/atool/ )The incredible amount of time this has saved over the years is great, but the real benefit never having remember either inspect archive files for the initial directory or remember to do this dance:
% mkdir foo
% cd foo
% tar zxvf /path/to/bar.tar.gz # or "unzip foo.zip" or "7x x foo.7z" etc
% mv bar-1.2.3 ..
% cd ..
% rmdir fooWithout atool you have to either 1) create a temporary dir youself and do the dance I showed above, or 2) always inspect every archive before you decompress it to see if it has multiple top-level files, or 3) accept that typing "tar xvf" or similar will sometimes spam your current directory (often $HOME) with thousands of files.
Similar to how git tends to remove mental blocks around branching (you not longer have ot worry about the cost of making an arbitrary change), atool frees you from having to worry about how an archive was created. This has, in practice, saved huge amount of time and mental effort.
First, alias this on Linux to get the 'open' command to work like OSX (e.g. 'open file.pdf'). It shadows a real 'open' command, but I have never used it, so no big loss :
alias open='xdg-open'
The second one I use all the time. A process that is stuck on I/O (or other uninterruptible syscalls), or even a Python interpreter stuck in a C extension, won't always respond well to Ctrl-C. However, Ctrl-Z (suspend) and this alias (kills the last suspended process group) do work relatively well : alias killit='kill -9 %%'
This one I use a lot for copying the output of a command to the clipboard. Just run 'command | pbcopy' and then Ctrl-V elsewhere. pbcopy is actually an OSX command, I aliased it on Linux for ease of use : alias pbcopy='xclip -selection clipboard'
alias pbpaste='xclip -selection clipboard -o'
This one runs a program under GDB. You can run e.g. 'gdbrun =python test.py' and debug C extensions more easily: alias gdbrun='gdb -ex=r --args' alias l='ls -laFT'
alias ll='ls -1aF'
alias k='ls -laFTrt'
alias kk='ls -1aFrt'
Some X + tee magic for logging. alias xlog='mv .xlog.txt .xlog.txt.old && startx -- -nolisten tcp 2>&1 | tee . xlog.txt > /dev/console'
alias xdbg='mv .xdbg.txt .xdbg.txt.old && startx -- /usr/X11R6/bin/X -keepPriv 2>&1 | tee .xdbg.txt > /dev/console'
And in ~/.xinitrc run an oddly colored xterm with /dev/console output: xterm -C -fg '#b00020' -geometry 80x38+0+0 &
Most folks don't start X manually but when working on X itself, extra logging can often help.#e.g. mktouch ~/Desktop/test/file.md would make the test dir if it didn't exist then touch file.md inside of it
mktouch() {
if [ $# -lt 1 ]; then
echo "Missing argument";
return 1;
fi
for f in "$@"; do
mkdir -p -- "$(dirname -- "$f")"
touch -- "$f"
done
} alias GD='git diff HEAD~1 HEAD'
alias grv='git review'
alias gunmod='git status | grep "modified" | awk "{print \$2}" | xargs -I{} git checkout -- {}'
alias gv='gvim --remote-silent'
alias json='| python -m json.tool'
alias vg='vagrant'
alias ans='ansible '
alias ansp='ansible-playbook '
alias vv='virtualenv venv'
alias vvp='venv/bin/python'
alias vvpi='venv/bin/python setup.py develop'
alias yt='cd $HOME/downloads; youtube-dl --verbose'
alias yt3='cd $HOME/downloads; youtube-dl --verbose --extract-audio --audio-format mp3 '
I keep all of my shell history, right now it think it goes back ~3 years, so every now and then I filter it by frequency and check if I am using a command often enough to justify an alias. Works quite well > alias json='| python -m json.tool'
Is the beginning pipe intended? If I just run 'python -m json.tool' then it starts reading from stdin by itself (and works as intended). And with the pipe, I can’t seem to make the alias work. $ echo '{ "foo": 1234}' | json
-bash: syntax error near unexpected token `|'
$ json
-bash: syntax error near unexpected token `|' # git branch ahead/behind another
function gahead() {
# use first argument or master
original=${1-master}
# use the second argument or current branch if not given
current=`git branch | grep \* | sed s/\*\ //`
compare=${2-$current}
# run git rev-list and capture numbers returned
result=`git rev-list --left-right --count $original...$compare`
# print numbers in a pretty message
numbers=($result);
echo -e "$Red-$Color_Off $compare is currently $IRed${numbers[0]}$Color_Off commits behind $original";
echo -e "$Green+$Color_Off $compare is currently $IGreen${numbers[1]}$Color_Off commits ahead of $original";
}
Edit: if it's not clear you would use this like: $ gahead #checks current branch against master
$ gahead development #checks current branch against development
$ gahead development foo #checks foo branch against development
NB: this is checking local branches, not remote branches. #!/bin/bash
nohup "$@" > /dev/null 2>&1 < /dev/null &Common GUI programs I often launch from the command line I put an alias in my .bashrc for. e.g.
alias thg="~/bin/nnohup /usr/bin/thg"
alias rabbitvcs="~/bin/nnohup /usr/bin/rabbitvcs"
alias meld="~/bin/nnohup /usr/bin/meld"
alias xnview="~/bin/nnohup /usr/bin/xnview"
alias dolphin="~/bin/nnohup /usr/bin/dolphin"
alias vlc="~/bin/nnohup /usr/bin/vlc" alias ..='cd ..'
alias ...='cd ../..'
alias ....='cd ../../..'
alias .....='cd ../../../..'
alias ......='cd ../../../../..'
# search history (??) and processes (???)
alias '?=fc -li 1'
alias '??=fc -li 1 | grep '
alias '???=ps-grep'
alias m='less'
alias su='sudo -H -s'
# move to trash rather than deleting
rm='trash-put -v'
# simplify hard to remember command names
alias pk-show='apt-cache show'
alias pk-install='sudo apt-get install'
alias pk-update='sudo apt-get update'
alias pk-update-upgrade='sudo apt-get update && sudo apt-get upgrade'
alias pk-uu=pk-update-upgrade
alias pk-remove='sudo apt-get remove'
alias pk-debinst='sudo dpkg -i'
pk-search () {
apt-cache search $1 | sort | egrep "${1}|$"
}Use magit in emacs as a git front-end instead of dozens of git aliases. You can continue to use vi or eclipse or whatever for editing. Use dired in emacs to replace a lot of file manipulation aliases.
Replace all your complicated aliases with ^r reverse search. For example, instead of typing
sudo dnf --refresh check-update
I type ^rche
because 'check' is the most distinct string in that command in my mind, and by 'che' bash has already located the last time I typed it in the history. Save lots of history: HISTSIZE=100000 # Lots of history.
HISTFILESIZE=100000 # Lots of history in the file.
HISTCONTROL=ignoreboth # Ignore entries with leading white space and dupes.
HISTIGNORE="ls:ll:cd:fg:j:jobs" # Uninteresting commands to not record in history.
shopt -s histappend # Append history to file, don't overwrite.
shopt -s histverify # Show expanded history before running it.
shopt -s cmdhist # Store multiline cmds as single
Which directory is hiding all the bytes? alias dux='du -x --max-depth=1 | sort -n'
Machines on the local network: alias nwho='ping -b -c 2 255.255.255.255 2>&1 | grep "bytes from" | cut -d " " -f 4 | sort | uniq | sed -e "s/://g"'
Set the permissions to allow sharing of files in a directory between users in the same group (probably doesn't work on bsd/osx): function rwxs {
find $1 -type d | xargs chmod u+rwx,g+rwxs,o+rx
find $1 -type f | xargs chmod ug+rw,o+r
}ali <name of alias> <command>
to get it put this in your .bashrc
function ali() {
echo "alias $1='${@:2}'" >> ~/.bashrc #in ubuntu use ~/.bash_aliases
echo "made alias:";
echo "alias $1='${@:2}'";
source ~/.bashrc; #reload bashrc
}
When I realize I type a command often, I just push the up arrow to get the command again, append "ali" infront of it and turn it to an alias. Works direclty.And of course the alias to show the list of all aliases:
alias alis='cat ~/.bash_aliases'That only lists what's in .bash_aliases, along with any comments.
Why not just run alias without arguments? It lists all active aliases, however and whenever they were defined.
$ man bash
/^SHELL BUILTIN COMMANDS
...
alias [-p] [name[=value] ...]
"Alias with no arguments or with the -p option prints the list of aliases in the form alias name=value on standard output."alias _=sudo
alias aria='aria2c --file-allocation=none -c -x 10'
alias aria_list='aria2c --file-allocation=none --force-sequential=true -c -x 10 -i'
alias d='dirs -v | head -10'
alias hdmi='xrandr --output HDMI1 --auto --right-of LVDS1'
alias history='fc -l 1'
alias la='ls -lAh'
alias lock='i3lock -c #1E90FF'
alias ls='ls --color=tty'
alias md='mkdir -p'
alias pyfind='find . -name ".py"'
alias pygrep='grep --include=".py"'
alias rd=rmdir
alias reload_sound='pulseaudio -k && sudo alsa force-reload' # when sound messes up
alias settings=gnome-control-center
alias sl=ls
alias utorrent='utserver -settingspath /opt/utorrent-server-alpha-v3_3/ &'
alias vga='xrandr --output VGA1 --auto --right-of LVDS1'
alias vga_same='xrandr --output VGA1 --auto --same-as LVDS1'
alias vup='vagrant up'
alias vdn='vagrant halt'
alias sv='ssh vagrant'
Some apt-get ones: alias sapu='sudo apt-get update'
alias sapi='sudo apt-get install'
alias sapr='sudo apt-get remove'
When I'm in Windows I use Cygwin (as a dropdown terminal[1]), these are useful: alias tk='taskkill -f -im'
alias ping='PING'
alias flush='ipconfig /flushdns'
[1]: http://blog.elamperti.com/2014/11/dropdown-cygwin-terminal-i... alias sucs='sort | uniq -c | sort -n’ # Makes a histogram of line-based input
# Example
$ echo c c c c b c b a a a | xargs -n1 | sucs
2 b
3 a
5 c alias mvi="mv -i"
alias cpi="cp -i"
Truncate a file to empty: alias zeroout="truncate -s 0"For example, YADR is pretty good ( https://github.com/skwp/dotfiles ).
Of the ones not listed already in this thread, a few standards that are great to have:
L - less
H - | head
T - | tail
G - | grep
C - | wc -l
N - /dev/null
S - | sort
_ - sudo
alias ffs=sudo
example usage:
$ grep 'something' some.conf
grep: some.conf: Permission denied
$ ffs !!
Password:` alias v='workon "${PWD##*/}"'
Also, I really like the -p flag of mkdir (creating child directories as needed, without failing if one doesn't exist) alias mkdir='mkdir -p'
# i.e. mkdir ./a/b/c/ alias i='i3-msg -q exec --'
alias jql='jq . -C | less -R'
Also bindings: bind -m vi-command -x '"\C-n": new-term-here'
bind -m vi-insert -x '"\C-n": new-term-here'
#which is `i gnome-terminal --working-directory="$PWD"` alias gadd='git add'
and so on.I find the oh-my-zsh git plugin's default aliases to be unintuitive and hard to remember. Simply not having to type 'git ' before everything is good enough for me.
alias c='noglob perl -e '\''shift; $x = eval qq(@ARGV); print $x; printf " 0x%x 0%o %b\n", $x, $x, $x'\'' _' ' "'
Quick and easy perl calculator, with octal, decimal and hex output. Example usage: root:~$ c 365 * 24 * 6 / (14 +2)
3285 0xcd5 06325 110011010101alias cpcd="echo -ne \$(pwd) | pbcopy"
Useful when you have a file picker in front of you, but you know you have a terminal already in the directory you want. Switch to the term, cpcd, switch to the file picker, Go to folder (⌘-Shift-G), Paste (⌘-V).
when you have a terminal open in a directory, but another app has a file picker box.
alias gl="git log -n 15 --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset' --abbrev-commit"
Copy current working dir (OSX), very handy. alias cpwd='pwd | pbcopy' alias sl ls
https://github.com/andreastt/dotfiles/blob/master/login#L44 sudo dnf install sl l() { tree -L 1 -CDFha --du --dirsfirst $@; }
ll() { l -L 2 $@; }
lll() { l -L 3 $@; }
md() { open -F -a /Applications/Marked\ 2.app/ $@; }
st() { open -F -a /Applications/Sublime\ Text\ 2.app/ $@; } # quick sketching
alias ep='emacs -eval "(picture-mode)"'
# quick timer
alias timer="time read -sn1"
# quick look
alias ql='qlmanage -p 2>/dev/null' alias lc="ls -C | less"
alias ll="ls -lh"
function mkd() {
if [[ $# == 0 ]] ; then
return
fi
mkdir "$1"
cd "$1"
} alias rstrip='sed -i "s/[ \t]*$//"'
# delete .pyc files alias delpyc='find . -type f -name "*.pyc" -delete' alias ga="git add ."
alias gs="git stash"
alias gas="ga && gs"
alias gmm="git merge master"
alias grm="git rebase master" alias myip="ifconfig | grep 'inet ' | grep -v 127.0.0.1 | cut -d\ -f2"https://github.com/colindean/getip
I've not actually used it as a script in a while, and it appears to be broken in that regard at the moment (something about ld not being able to write to the tmp directory). However, its Makefile is still operational. Maybe someone will see this and submit a PR that fixes it?
alias ff="find . -iname"
alias size="du -h --max-depth=1"
alias latest="ls -lt |head"
alias orly='sudo $(history -p \!\!)'
ssh h.e.c alias l='ls -1F' alias dc=docker-composealias diskspace="du -S | sort -n -r |less"
alias pyserver='python -m SimpleHTTPServer'
alias phpserver='php -S localhost:8000'
alias phplog='tail -f /var/log/apache2/error.log'
alias hn='elinks http://news.ycombinator.com/news'Shows up all the things I still need to work on in a given project (assuming of course that you annotate your code...).
...=../..
....=../../..
.....=../../../..
......=../../../../..
1='cd -'
2='cd -2'
3='cd -3'
4='cd -4'
5='cd -5'
6='cd -6'
7='cd -7'
8='cd -8'
9='cd -9'
_=sudo
afind='ack-grep -il'
d='dirs -v | head -10'
g=git
ga='git add'
gaa='git add --all'
gap='git add --patch'
gb='git branch'
gba='git branch -a'
gbr='git branch --remote'
gc='git commit -v'
'gc!'='git commit -v --amend'
gca='git commit -v -a'
'gca!'='git commit -v -a --amend'
gcl='git config --list'
gclean='git reset --hard && git clean -dfx'
gcm='git checkout master'
gcmsg='git commit -m'
gco='git checkout'
gcount='git shortlog -sn'
gcp='git cherry-pick'
gcs='git commit -S'
gd='git diff'
gdc='git diff --cached'
gdt='git difftool'
gg='git gui citool'
gga='git gui citool --amend'
ggpnp='git pull origin $(current_branch) && git push origin $(current_branch)'
ggpull='git pull origin $(current_branch)'
ggpur='git pull --rebase origin $(current_branch)'
ggpush='git push origin $(current_branch)'
gignore='git update-index --assume-unchanged'
gignored='git ls-files -v | grep "^[[:lower:]]"'
git-svn-dcommit-push='git svn dcommit && git push github master:svntrunk'
gk='gitk --all --branches'
gl='git pull'
glg='git log --stat --max-count=10'
glgg='git log --graph --max-count=10'
glgga='git log --graph --decorate --all'
glo='git log --oneline --decorate --color'
globurl='noglob urlglobber '
glog='git log --oneline --decorate --color --graph'
glp=_git_log_prettily
gm='git merge'
gmt='git mergetool --no-prompt'
gp='git push'
gpoat='git push origin --all && git push origin --tags'
gr='git remote'
grba='git rebase --abort'
grbc='git rebase --continue'
grbi='git rebase -i'
grep='grep --color=auto --exclude-dir={.bzr,.cvs,.git,.hg,.svn}'
grh='git reset HEAD'
grhh='git reset HEAD --hard'
grmv='git remote rename'
grrm='git remote remove'
grset='git remote set-url'
grt='cd $(git rev-parse --show-toplevel || echo ".")'
grup='git remote update'
grv='git remote -v'
gsd='git svn dcommit'
gsps='git show --pretty=short --show-signature'
gsr='git svn rebase'
gss='git status -s'
gst='git status'
gsta='git stash'
gstd='git stash drop'
gstp='git stash pop'
gsts='git stash show --text'
gts='git tag -s'
gunignore='git update-index --no-assume-unchanged'
gunwip='git log -n 1 | grep -q -c "\-\-wip\-\-" && git reset HEAD~1'
gup='git pull --rebase'
gvt='git verify-tag'
gwc='git whatchanged -p --abbrev-commit --pretty=medium'
gwip='git add -A; git ls-files --deleted -z | xargs -r0 git rm; git commit -m "--wip--"'
history='fc -l 1'
l='ls -lah'
la='ls -lAh'
ll='ls -lh'
ls='ls -G'
lsa='ls -lah'
md='mkdir -p'
please=sudo
po=popd
pu=pushd
rd=rmdir
run-help=man
which-command=whenceI stopped using oh-my-zsh because of these. There is a popular program called 'gap' which got over-written by one of these aliases.
One problem I had with gulp. I had dir name gulp and zsh got confused weather to go to the dir or execute the cmd.
prompts when you overwrite something using cp ("oops, wrong folder")
alias rm='rm -I'
prompts when you delete more than three files ("oops, a space too much in rm ./ *")
alias nope="git commit -m 'ಠ_ಠ'"alias serv="ssh mylogin@myserv.com"
alias work="cd /Users/myname/Sources/mycompany/myproject"
alias ..='cd ..'sed -i "$ a\#custom shortcuts \nx(){ case \$1 in .tar.bz2) tar xjf \$1;; .tar.gz) tar xzf \$1;; .bz2) bunzip2 \$1;; .rar) rar x \$1;; .gz) gunzip \$1;; .tar) tar xf \$1;; .tbz2) tar xjf \$1;; .tgz) tar xzf \$1;; .zip) unzip \$1;; .Z) uncompress \$1;; esac; } \nf(){ grep -R -I \"\$1\" ./*; } \np(){ ps aux | grep \$1 | grep -v grep; } \nown(){ sudo chmod 777 -R \${1:-.} && sudo chown -R \${USER} \${1:-.}; } \nalias u='sudo apt-get update && sudo apt-get dist-upgrade -y && sudo apt-get autoremove -y' \nalias l='ls -la' \nalias ..='cd ..' \nalias i='sudo apt-get install -y ' \nalias gg='git add . && git commit -a -m "." && git push -u -f origin master'" ~/.bashrc && source ~/.bashrc
More here https://github.com/gulakov/nodejs-starter/blob/master/README...