By using this website, you agree to the cookie policy

Blog Publications About πŸŒ™

Shell aliases

During my time as a software developer I’ve stumbled across various shell aliases which have made my life easier. I’ve written them down here in hopes that you will find them helpful as well.

Improved Git log

This may be my favorite and is certainly one of my most used aliases. It provides a very compact yet understandable view of a Git project’s history.

alias gitlog="git log --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset' --abbrev-commit"

If you prefer a Git alias instead of a shell alias, the following Git config will cause git lg to have the same effect.

git config --global alias.lg "log --color --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset' --abbrev-commit"

This is what it looks like:

~/code/example_project master ❯ git lg
* 0fbaf73 - (HEAD -> master) Change .gitignore (4 days ago) <Tim>
*   a0fafcb - Merge branch 'example-dev-branch' (4 days ago) <Tim>
|\  
| * d6ef10e - (example-dev-branch) Add another file (4 days ago) <Tim>
| * 5434c8e - Added some files (4 days ago) <Tim>
* | fb95415 - Add a file (4 days ago) <Tim>
|/  
* bf7889c - Add .gitignore (4 days ago) <Tim>

Quick navigation

These aliases allow you to quickly navigate up one or more directory levels. For example, .. will navigate up one level.

alias ..="cd .."
alias ...="cd ../.."
alias ....="cd ../../.."

Detailed folder contents

exa is a modern replacement for ls with heavy use of colors and additional features such as Git support.

alias e='exa -la --git'

The following figure shows exa in action and git status is entered to provide some insight to the meaning of the Git info column.

~/code/example_project exa-example-branch +2 !1 ?1 ❯ e
drwxrwxr-x - tim 13 Aug 14:46 -- .git
.rw-rw-r-- 2 tim 13 Aug 14:42 M- .gitignore
.rw-rw-r-- 2 tim 13 Aug 14:42 -M changed_file.txt
.rw-rw-r-- 0 tim 13 Aug 14:42 N- staged_file.txt
.rw-rw-r-- 0 tim  9 Aug 17:28 -- tracked_file.txt
.rw-rw-r-- 0 tim 13 Aug 14:43 -N untracked_file.txt
~/code/example_project exa-example-branch +2 !1 ?1 ❯ git status
On branch exa-example-branch
Changes to be committed:
  (use "git restore --staged <file>..." to unstage)
	modified:   .gitignore
	new file:   staged_file.txt

Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git restore <file>..." to discard changes in working directory)
	modified:   changed_file.txt

Untracked files:
  (use "git add <file>..." to include in what will be committed)
	untracked_file.txt

Folder/file search

Quickly find the full name of a folder in you current working directory when you can only remember one part of the name.

alias lg='ls -lah | grep'

Folder size

This alias is helpful in figuring out which folders are taking up the most disk space.

alias d='du -d1 -h | sort -h'

Python virtual environment

Save yourself time from activating/deactivating different virtual environments while switching between projects.

alias epy='env/bin/python3'

Vim mode for IPython

Everything is better with Vim, and IPython is no exception. This alias will allow ipy to launch IPython in Vim mode.

alias ipy='ipython3 --TerminalInteractiveShell.editing_mode=vi'

Tarball handling

Here are some helpful functions for creating and extracting tarballs.

xtar() { tar -xzvf $1 ; echo "---Finished---" }
ctar() { tar -czvf $1.tar.gz $1 ; echo "---Finished---" }
ctarbar() { tar cf - $1 | pv -s $(du -sb $1 | awk '{print $1}') | gzip > $1.tar.gz }

For example, with the following command, example.tar.gz will be created from the folder example while showing a progress bar:

$ ctarbar example

Improved file inspection

The classic tool for file inspection is cat, but cat offers nothing in the way of syntax highlighting. An improved version of cat is bat and can be found here.

I prefer a simpler presentation than the default options offer, and the tool installs as batcat on Ubuntu, so I use the following alias:

alias bat='batcat --style="numbers,changes"'

The output looks like this:

~/example ❯ bat README.md
   1   # Header
   2   text
   3   **bold text**
   4   *italic text*
   5   `code`
   6   > blockquote
   7   [link](url)



Edits

  • 16 August 2021: Make code blocks scrollable and add syntax highlighting
  • 28 January 2022: Add aliases for handling tarballs
  • 2 February 2022: Add alias for batcat


Published on 9 August 2021

If you enjoy this blog, please consider supporting me by buying one of my books. πŸ“š
(click here)