If you're on DEV, chances are you spend at least some time in the terminal, maybe even a lot of time.
Over the years, I've picked up a number of tips and tricks from fellow developers. Almost every time I pair program with someone new, chances are I'll notice them doing something neat and ask them how they did it.
Here are some of my favorites.
I use bash as my default terminal, but most of these tips translate to other terminals as well.
Note: This post isn't meant to teach the basics of using the terminal. There are many great resources online (I remember doing Codecademy's Command Line course when I was starting out.
The - operator
Do you find yourself switching back and forth between two directories often?
You can use cd - to change to the last directory you were in like this:
~ $ cd directory1
~/directory1 $ cd directory2
~/directory2 $ cd -
~/directory1 $
This also works with git when switching between branches:
~/my-project(main)$ git checkout feature-branch
~/my-project(feature-branch)$ git checkout -
~/my-project(main)$
The !! operator
This happens a lot!
You type a command, only to get a "Permission denied" so you have to retype the command again, this time using sudo.
The !! operator echoes the last command you typed into your terminal.
You can use it like this:
$ some-dangerous-script.sh
=> Error: Permission Denied
$ sudo !!
=> Enter password for some-dangerous-script.sh:
{curly brace expansion}
If you ever need to run a series of very similar commands that differ by just a few characters (like for example, if you want to create a few filenames with lightly different extensions) you can use the characters that will be different between two curly braces and the command will run once for each one.
Like this:
$ touch file-{1,2,3}.md
$ ls
=> file-1.md file-2.md file-3.md
You can also pass in a range:
$ touch file-{1..3}.md
$ ls
=> file-1.md file-2.md file-3.md
Search using Ctrl+R
Are you like me? Would you press the up button 20 times to avoid typing out a 7 character command?
This next one was a lifesaver for me!
You can type Ctrl + R followed by the first few letters of the command you want to search through your bash history and bring up the command you need.
(Sorry, I can't think of how to demonstrate that with a code snippet. Just go to your terminal, type in Ctrl + R and start typing).
Aliases
Aliases are a great way to save time and keystrokes. If there's a command or a series of commands you find yourself typing often, it's making an alias can be very helpful.
In order to set aliases, first open the ~/.bashrc file in your favorite editor and check if it has the following lines in it:
if [ -f ~/.bash_aliases ]; then
. ~/.bash_aliases
fi
It should be there already, if it isn't just add it to the bottom of the file.
Next open ~/.bash_aliases in your editor (or create it if it doesn't exist) and add your aliases in the following format:
alias something="definition"
Some playful aliases I have in my .bash_aliases are:
alias please="sudo "
alias yeet="rm -rf
I also have a number of functions defined there, for more complex command series:
mk() {
mkdir $1 && cd $1
}
gclone() {
git clone "$1" && cd "$(basename "$1" .git)"
}
The mk alias takes a directory name as an argument, mks the directory and then cds into it.
The gclone alias takes a git repo, clones it, and then cds into it.
After adding aliases to your .bash_aliases they should load automatically every time you start a new terminal session.
If you would like to use your aliases in your current session, run:
source ~/.bash_aliases
That's what I can think of for now.
Do you have any favorite tips and tricks?
Please please do share them! I always love learning new ones!
Latest comments (34)
These are life saving tips ...
I was expecting an explanation of the mysterious dot source (as one will probably encounter it sooner or later).
For instance, could ". ~/.bash_aliases" be used as an alternative to "source ~/.bash_aliases"? If it could, perhaps add something about to the end of the this article?
"Ae you like me" → "Are you like me"
Whoops! Thanks!
In the function
mk, you can usemkdir -pinstead ofmkdirto create subfolders if requiredThanks!
Awesome post!
Thanks! 🙏
Tip: if you add aliases you can add the original command as prefix, so that if you have auto-completion, you can remember your stuff. :)
e.g.: aliases
github.com/voku/dotfiles/blob/mast...
e.g.: functions
github.com/voku/dotfiles/blob/mast...
Great idea!
My favorite alias is
grip='grep -i'for case-insensitive grep.Other readline shortcuts are also pretty handy:
These also work with many terminal applications such as repls.
Awesome!
I've been using the Home and End buttons to go to the beginning/end of the line.
I clearly didn't look that hard, but I tried to find these hotkeys once and couldn't find them super quick. This is exactly the list I've been looking for. Thanks!
I am having trouble figuring out what the alt is on Mac for this. Often alt = option, but in this case it just does the special characters like ∫ for alt + b.
!$ is the last word from the previous command. Say you run
ls -l myfile.exeand notice no execution bit is set: chmod +x !$.
One of the less funky bash tricks, which I find it a bit overlooked is the ability to trim or slim the command line history.
Here is an example:
Lifted from my "Today I Learned" collection