Shell aliases for Bash or ZSH
See my full .aliases on GitHub.
Storing aliases
Aliases can be set directly in the shell config.but I prefer to keep them in a separate file called ~/.aliases
. And then use it like this:
if [ -f ~/.aliases ]; then
source ~/.aliases
fi
Here are some aliases.
List
# List file and dir names vertically.
alias l1='ls -1 -F'
e.g.
$ cd ~
$ l1
Desktop/
Documents/
Downloads/
Dropbox/
Music/
Pictures/
Public/
Temp/
Templates/
Videos/
npm/
public_repos/
repos/
Zipping
Here is how I handle zipping:
# Zip
alias tarz='tar czvf'
# Unzip
alias taru='tar xzvf'
e.g.
$ tarz foo.tar.gz fizz.txt buzz.py
$ taru foo.tar.gz
Add confirmation when losing files
Force file operations to be interactive, which means I get a confirmation message to accept before I copy or move that would overwrite a file, an whenever I delete a file.
alias rm='rm -i'
alias cp='cp -i'
alias mv='mv -i'
e.g.
$ touch foo.txt
$ rm foo.txt
rm: remove regular empty file 'foo.txt'? y
$
Jekyll
For Jekyll commands running through bundler.
alias jek='bundle exec jekyll'
e.g.
$ jek serve
Python packages
I don't like installing packages in my global Python environment, especially by accident. So I set this up in my shell config. This flag is standard for Python.
export PIP_REQUIRE_VIRTUALENV=false
Then if I run this, I can an error to protect me.
$ pip3 install requests
ERROR: Could not find an activated virtualenv (required).
So then I activate my virtual environment. Or I override with PIP_REQUIRE_VIRTUALENV=true
using my alias:
alias pip-user='PIP_REQUIRE_VIRTUALENV=false python3 -m pip'
$ pip-user requests
...
Instant web server
I also find myself often starting a Python server in a directory.
So with this:
alias pserver='python3 -m http.server'
I can just run:
$ pserver
Serving HTTP on 0.0.0.0 port 8000 (http://0.0.0.0:8000/) ..
View aliases
To see all the aliases that are setup, run this:
$ alias
alias ag='alias | grep'
alias apt-i='sudo apt install'
...
alias jek='bundle exec jekyll'
alias l='ls -C -F'
alias l1='ls -1 -F'
...
That is a standard feature of alias
, if not given any arguments.
I added my own feature like this for viewing all my git aliases. See that in my next post on Git Configs - Part 4.
If you have any aliases to share or need help with your syntax, comment here.
Top comments (0)