My favourite and most used terminal commands
If you’re a developer, or like messing with your computer, there’s no way to avoid the terminal. It’s almost all keyboard-based and can be intimidating when you first start. As you get used to it though, it becomes second-nature. Here are some shortcuts and tricks that will make your time in there far more pleasant.
This is my current list of useful things and I will keep coming back to update this.
July 2023 - I’m using iTerm on MacOS with zsh. I’ve been using Fig a bit but I recently saw in the Activity Monitor that it was using a massive amount of memory so have stopped using that for now.
Navigating a long line of text
Cmd | Action |
---|---|
CTRL+A | go to the start of the line |
Alt+B | move back one word |
CTRL+E | move to the end of the line |
Alt+F | move forward a word |
CTRL+U | erase everything from the current cursor position to the beginning of the line |
CTRL+K | erases everything from the current cursor position to the end of the line |
Use aliases
alias gpom="git push origin main"
I have a .aliases
file that I just append aliases to. Mostly for navigating to common directories.
I actually have an alias for editing my aliases file
alias aliases='vim ~/.aliases'
I even have a method in my aliases file for adding new aliases form the command line without having to open up the aliases file:
# Create a new aliases and append it to this file.
alias newalias='function _newalias() { echo "alias $1=\"$2\"" >> ~/.aliases; source ~/.aliases; unset -f _newalias; }; _newalias'
Use it this way:
$ newalias gpom 'git push origin master'
This appends alias gpom="git push origin master"
to my aliases file. Very useful for not losing your train of thought.
Another useful method is to create and move into a new directory in one command. This even creates the intermediary directories if they don’t exist
# Name and change into new directory. July 2023
alias cdd='function _cdd() { mkdir -p "$1" && cd "$1"; unset -f _cdd; }; _cdd'
#MAKE Terminal better (natelandau.com)
alias cp='cp -iv' # ask before overwriting, verbose
alias c="clear"
Other things
Cmd | Action |
---|---|
Ctrl R | to search your terminal history |
Up arrow | to flick back through your past commands |
$ ag | (The Silver Surfer) lightning fast way to search through source code |
Esc + . | to get the arguments from the previous commands (I use it so much I don’t even notice I’m doing it) |
- Homebrew is great (essential?) for installing new packages on Mac
More aliases
Most of these are ones I’ve borrowed from other people. I would recommend only taking one at a time from other people or you will have an aliases file full of things you don’t use.
# Show/hide hidden files in Finder
alias show="defaults write com.apple.finder AppleShowAllFiles -bool true && killall Finder"
alias hide="defaults write com.apple.finder AppleShowAllFiles -bool false && killall Finder"
# Hide/show all desktop icons (useful when presenting)
alias hidedesktop="defaults write com.apple.finder CreateDesktop -bool false && killall Finder"
alias showdesktop="defaults write com.apple.finder CreateDesktop -bool true && killall Finder"
# Get macOS Software Updates, and update installed Ruby gems, Homebrew, npm, and their installed packages
alias update='sudo softwareupdate -i -a; brew update; brew upgrade; brew cleanup; npm install npm -g; npm update -g; sudo gem update --system; sudo gem update; sudo gem cleanup'
alias hosts="sudo $EDITOR /etc/hosts"
Have a nicer ls
command
# Detect which `ls` flavor is in use
if ls --color > /dev/null 2>&1; then # GNU `ls`
colorflag="--color"
else # OS X `ls`
colorflag="-G"
fi
# List all files colorized in long format, including dot files
alias ll='ls -lha'
#alias ls='ls'
alias la="ls -lha"
# List only directories
alias lsd='ls -lha | grep "^d"'
# Always use color output for `ls`
alias ls="command ls ${colorflag} -lah"
export LS_COLORS='no=00:fi=00:di=01;34:ln=01;36:pi=40;33:so=01;35:do=01;35:bd=40;33;01:cd=40;33;01:or=40;31;01:ex=01;32:*.tar=01;31:*.tgz=01;31:*.arj=01;31:*.taz=01;31:*.lzh=01;31:*.zip=01;31:*.z=01;31:*.Z=01;31:*.gz=01;31:*.bz2=01;31:*.deb=01;31:*.rpm=01;31:*.jar=01;31:*.jpg=01;35:*.jpeg=01;35:*.gif=01;35:*.bmp=01;35:*.pbm=01;35:*.pgm=01;35:*.ppm=01;35:*.tga=01;35:*.xbm=01;35:*.xpm=01;35:*.tif=01;35:*.tiff=01;35:*.png=01;35:*.mov=01;35:*.mpg=01;35:*.mpeg=01;35:*.avi=01;35:*.fli=01;35:*.gl=01;35:*.dl=01;35:*.xcf=01;35:*.xwd=01;35:*.ogg=01;35:*.mp3=01;35:*.wav=01;35:'
Remove .DS_Store files
# Recursively remove .DS_Store files
alias cleanupds="find . -type f -name '*.DS_Store' -ls -delete"