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, mk
s the directory and then cd
s into it.
The gclone
alias takes a git repo, clones it, and then cd
s 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 -p
instead ofmkdir
to 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.exe
and 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
Great post 👍 Actually it seems to be Ctrl + R for search on Mac as well, at least on my Mac.
Oh whoops! Let me check! (I don't use Macs often lol)
Thanks for the correction, I'll fix it as soon as I confirm!
You have these stored in
.bash_aliases
(which is conventional) but I use an.aliases
filename, because the alias command is pretty much the same in most shells. That means when I'm usingzsh
on a Mac, I don't feel dirty loading abash
configuration file.I also put functions that are very close to being aliases in that file, which feels a little wrong, but if it's just to allow passing a single parameter to the middle of what would otherwise be an alias, I'm okay with it :)
I conditionally apply aliases that shadow other commands so as not to break functionality:
And I split things up if I need to like this:
I actually store mine in a git repo and then add a line to my .bashrc to source the files in that repo (the repo is here if you're curious), but I used .bash_aliases here to follow convention (and because most .bashrc's are already configured to look there).
Yeah, I also have a few functions in my alias file, for the same reason. If it looks like an alias, acts like an alias... 😉
The CLI fzf is a nice addition to CTRL+r. I can't search in my command line history without it anymore. You can as well search a file in directories / sub-directories with it, or even with Vim / Neovim to search a file in your project, or some text in a bunch of file combined with ripgrep! Really cool.
github.com/junegunn/fzf
I've seen that being used!
Personally I use z (github.com/rupa/z) to jump around between directories, but I wanted to limit the post to features built in to bash rather than tools I have installed.
Loved the tips. Thanks for sharing!
Great tips! Thanks for sharing!
You should also check out this open-source Bash eBook here:
github.com/bobbyiliev/introduction...
Any contributions would be appreciated!