As a designer or developer person who uses a computer, the terminal is your best friend! But understandably, that big white or black box with a ticking pointer can appear very intimidating. Especially when filled with red error messages or when entering commands blindly from the internet (don’t do that!).
Education is key here. When you understand what some basic commands are doing, you can overcome terminal-dation (just go with it) and really improve your work flow!
Please Note: The following article focuses on unix-based systems (like Mac) only. I can’t help you with Windows. Sorry!
Working the Command Line
If you’re only using the terminal sometimes — maybe to run task runners (like Gulp or Grunt), and use Github, there are a few key commands that will help you with your work flow.
Moving Around the Terminal Prompt
ctrl
+A
will bring you to the beginning of your line. Forgot to add sudo? Don’t wait to space through to the beginning of your line, simply pressctrl
+A
and get there. Alternatively,!!
repeats the last command, so you could typesudo !!
to yield the same result.ctrl
+E
puts the cursor at the end of the line.
Controlling Processes
ctrl
+Z
pauses a process. If you’re running Gulp and commit your changes, you don’t have to open a new tab. Just pause the process. Typectrl
+Z
, and your terminal should allow entry for a new command. To see all of the paused processes, enterjobs
— a command that will list all of the paused jobs. Resume the process withfg
, which stands for “foreground”. It brings your job from the background to the foregroundctrl
+C
cancels a process. This is how you stop a process (such as Gulp). You will need to restart it
Dealing with Files
open .
will open up the directory which you are currently in. This is great if you need a view of your file system.open filename
will open the file itself.mkdir foldername
creates a folder (mkdir = make directory) within the directory you are currently inside of.touch filename
creates files. I use this often! You can also use a shortcut:>filename.txt
to save a few keystrokes. The:
denotes an empty command.
Editing Files
vim
will open an in-terminal text-editor called Vim. You can dovim .
to open and navigate the directory or open a specific file immediately withvim filename
. Simply creating a file withvim filename
and then saving it within vim:w
creates a file on the fly as well. (Not going to go through all of Vim’s commands, but it’s pretty powerful and you should look into it)- The
&&
allows you to string commands together. The;
also allows this. The difference is that commands using;
are executed regardless of the completion of other tasks in the system, while the&&
are logical and only executed if the previous task completes. So you can domkdir hello; cd hello; touch world.txt && vim world.txt
subl .
is a popular symlink that enables you to open files directly in Sublime text. You need to set up this command with this tutorial.
Customizing Your Terminal
Z Shell (Zsh) is a Unix shell that boosts the power of your typical Bash prompt. Specifically, it has better tab completion among other things, or so I hear. You will now have to edit your .zshrc
instead of .bashrc
profile for aliases and some other tasks. Oh My Zsh is a community-driven effort to provide customizations for Z Shell, and can really transform your terminal experience. Not only are there tons of beautiful themes, but it allows easy and invaluable git integration that will surely change your life. You can install it with one simple script:
curl -L https://raw.github.com/robbyrussell/oh-my-zsh/master/tools/install.sh | sh
Custom Commands
Writing your own terminal shortcuts is easy and fun! You can do this in a few ways. I’m going to assume you’ve installed Z Shell and Oh My Zsh from the previous section (because you should!) and it would take a while to go through every method to add aliases. So I’ll talk about two places you can write your aliases.
Aliases
The first is in your .zshrc file (typically located in your root directory, so you can access it with vim ~/.zshrc
or subl ~/.zshrc
or open ~/.zshrc
(look at all of the options you now have!)). Oh My Zsh will have some configuration settings you can uncomment in there, and you can also create aliases in this file begining with alias
!
The second thing you can do is create a new shell script file and link to it from the .zshrc file using source
. For example, if I make a file within the same directory, I would put source "/Users/unakravets/.aliases.sh"
in the .zshrc file. All of my aliases would go into aliases.sh. This allows for a better file organization and overall cleaner system.
Whichever way you do it, let’s see an example. This alias will open up the emoji cheat sheet. Super important for git commit messages and life in general:
# open up emoji cheat sheet in browser
alias emojis="open http://www.emoji-cheat-sheet.com/"
Here’s another example. This alias gives me a beautiful visualization of my git history and branches right inside of my terminal. All i’d have to do is type git-pretty and BAM!:
# pretty visual git history
alias git-pretty="git log --graph --oneline --all --decorate"
Functions
Functions allow for some great extensibility of custom commands in your terminal. Here is an example of a basic function that makes a folder and then opens up into it:
function mkd() {
mkdir "$@" && cd "$@"
}
You can get a bit fancier with functions by including loops and logic. Here is a function I wrote to add items to my content-I-want-to-read/watch/etc list in my Open Source Personal Goals Repo:
# add to content list (opens content list folder in vim)
# i.e. pg-add blog-post or pd-add resource
function pg-add() {
if [ $# -eq 0 ]; then
print "Oops. Please enter a content type! (i.e. pg-add video)"
else
vim ~/Desktop/Dev/personal-goals/content-list/"$1"s.md
fi
}
Let’s break down some of that function syntax:
$#
= these are your parameters. For instance,$1 $2
refer to your first two parameters after your command. If I typedreturn-me wild unicorn
with the function beingreturn-me
,$1
would bewild
and$2
would beunicorn
.-eq
= is equal to. What the function above is saying is “if the number of parameters is equal to zero” AKA if I have no parameters, let me know I messed up.$@
captures all of the arguments (first example).- For more information on how to write your own functions and what the proper syntax is, check out a unix shell scripting tutorial.
fi
means finish. But you knew that already.
You can use functions and aliases to do pretty much anything and really tailor them to your personal work flow. Open a specific folder often? Write an alias so you can access it from anywhere. Once you start, you’ll find more and more ways to optimize your setup and love the terminal!
tl;dr: Terminal is your friend. Embrace it!