DEV Community

Cover image for Dotfiles - Shell Aliases
Michael Currin
Michael Currin

Posted on

Dotfiles - Shell Aliases

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 
Enter fullscreen mode Exit fullscreen mode

Here are some aliases.

List

# List file and dir names vertically. alias l1='ls -1 -F' 
Enter fullscreen mode Exit fullscreen mode

e.g.

$ cd ~ $ l1 Desktop/ Documents/ Downloads/ Dropbox/ Music/ Pictures/ Public/ Temp/ Templates/ Videos/ npm/ public_repos/ repos/ 
Enter fullscreen mode Exit fullscreen mode

Zipping

Here is how I handle zipping:

# Zip alias tarz='tar czvf' # Unzip alias taru='tar xzvf' 
Enter fullscreen mode Exit fullscreen mode

e.g.

$ tarz foo.tar.gz fizz.txt buzz.py $ taru foo.tar.gz 
Enter fullscreen mode Exit fullscreen mode

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' 
Enter fullscreen mode Exit fullscreen mode

e.g.

$ touch foo.txt $ rm foo.txt rm: remove regular empty file 'foo.txt'? y $ 
Enter fullscreen mode Exit fullscreen mode

Jekyll

For Jekyll commands running through bundler.

alias jek='bundle exec jekyll' 
Enter fullscreen mode Exit fullscreen mode

e.g.

$ jek serve 
Enter fullscreen mode Exit fullscreen mode

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 
Enter fullscreen mode Exit fullscreen mode

Then if I run this, I can an error to protect me.

$ pip3 install requests ERROR: Could not find an activated virtualenv (required). 
Enter fullscreen mode Exit fullscreen mode

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' 
Enter fullscreen mode Exit fullscreen mode
$ pip-user requests ... 
Enter fullscreen mode Exit fullscreen mode

Instant web server

I also find myself often starting a Python server in a directory.

So with this:

alias pserver='python3 -m http.server' 
Enter fullscreen mode Exit fullscreen mode

I can just run:

$ pserver Serving HTTP on 0.0.0.0 port 8000 (http://0.0.0.0:8000/) .. 
Enter fullscreen mode Exit fullscreen mode

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' ... 
Enter fullscreen mode Exit fullscreen mode

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)