DEV Community

Yechiel Kalmenson
Yechiel Kalmenson

Posted on • Updated on

My Favorite Bash Tips, Tricks, and Shortcuts

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 $
Enter fullscreen mode Exit fullscreen mode

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)$
Enter fullscreen mode Exit fullscreen mode

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: 
Enter fullscreen mode Exit fullscreen mode

{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
Enter fullscreen mode Exit fullscreen mode

You can also pass in a range:

$ touch file-{1..3}.md
$ ls
=> file-1.md file-2.md file-3.md
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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"
Enter fullscreen mode Exit fullscreen mode

Some playful aliases I have in my .bash_aliases are:

alias please="sudo "
alias yeet="rm -rf 
Enter fullscreen mode Exit fullscreen mode

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)"
}
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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!

Top comments (34)

Collapse
 
vonheikemen profile image
Heiker
# Up Arrow
bind '"\e[A": history-search-backward'

# Down Arrow
bind '"\e[B": history-search-forward'
Enter fullscreen mode Exit fullscreen mode

The history-search-* enable a "search by prefix".

Let's say I have this in my history.

node ./test.js
vi /tmp/text.txt
nvim /tmp/text.txt
echo "a string with vi in it"
vi /tmp/other-text.txt
Enter fullscreen mode Exit fullscreen mode

if I type vi and start pressing the up arrow bash will only show me the entries that begins with vi. Which is awesome. I have a couple of commands that have been on my history for months, and I have never typed them twice, it's always the first two letters + up + enter.

Here is another trick

# Space, but magical
bind Space:magic-space
Enter fullscreen mode Exit fullscreen mode

This one enables history expansion with the space key. So you can do sudo !! + space, and it will show you the entire command before you run it.

Collapse
 
nilkun profile image
Nilkun

Just copy-pasted your code into my .bashrc! That is an amazing trick!

Collapse
 
aashutoshrathi profile image
Aashutosh Rathi

Hey! Would you like to add this sorcery at github.com/aashutoshrathi/awesome-...?

Collapse
 
vonheikemen profile image
Heiker

Sure. I'll do that.

I would like to mention that I got this from the oh-my-bash repo. They have a ton of interesting things there.

Thread Thread
 
loebkes profile image
Lui

You should really have a look at github.com/junegunn/fzf

This changed my life and how I work with the command line forever.

Thread Thread
 
vonheikemen profile image
Heiker

Oh I know about fzf, I use it extensively inside vim (I would be lost without it). Sadly I don't have many use cases for it in my day to day.

But this reminds me of yet another thing you can do with bind. One can bind key sequence with a "macro", it's like simulating keystrokes.

I have these two.

# Alt + f 
bind '"\ef": "\C-e | fzf"'

# Alt + p
bind '"\ep": "\C-e | less"'
Enter fullscreen mode Exit fullscreen mode

Alt + f will append | fzf at the end of a command and Alt + p does the same but with less. This works by using the sequence \C-e (ctrl + e) which makes the cursor go to the end of the line and basically typing the rest for you.

Collapse
 
aghost7 profile image
Jonathan Boudreau

Other readline shortcuts are also pretty handy:

  • alt + f: move forward one word
  • alt + b: move backward one word
  • ctrl + e: move to end of line
  • ctrl + a: move to start of line
  • ctrl + w: delete the previous word
  • alt + d: delete the next word

These also work with many terminal applications such as repls.

Collapse
 
camerenisonfire profile image
Cameren Dolecheck • Edited

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.

Collapse
 
yechielk profile image
Yechiel Kalmenson

Awesome!

I've been using the Home and End buttons to go to the beginning/end of the line.

Collapse
 
moopet profile image
Ben Sinclair

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 using zsh on a Mac, I don't feel dirty loading a bash 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:

if command -v bat >/dev/null; then
  alias cat='bat'
fi
Enter fullscreen mode Exit fullscreen mode

And I split things up if I need to like this:

case $OSTYPE in
  linux-gnu)
    alias ls='ls --color --group-directories-first -FhN'
    alias pbcopy='xclip -selection clipboard'
    ;;
  darwin*)
    alias ls='ls -FhG'
  ;;
esac
Enter fullscreen mode Exit fullscreen mode
Collapse
 
yechielk profile image
Yechiel Kalmenson • Edited

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... 😉

Collapse
 
nilkun profile image
Nilkun

Didn't know about Ctrl-R. That really is a lifesaver!

I also just published an article with my favorite bash functions and aliases.

Collapse
 
jonasbn profile image
Jonas Brømsø

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:

export HISTIGNORE="pwd:ls:ls -l:cd:clear"
Enter fullscreen mode Exit fullscreen mode

Lifted from my "Today I Learned" collection

Collapse
 
suckup_de profile image
Lars Moelleken • Edited

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...

# show the biggest files in a folder first
alias du_overview='du -h | grep "^[0-9,]*[MG]" | sort -hr | less'
Enter fullscreen mode Exit fullscreen mode

e.g.: functions
github.com/voku/dotfiles/blob/mast...

# history_top_used: show your most used commands in your history
history_top_used()
{
  history | awk '{a[$2]++}END{for(i in a){print a[i] " " i}}' | sort -rn | head
}
Enter fullscreen mode Exit fullscreen mode
Collapse
 
yechielk profile image
Yechiel Kalmenson

Great idea!

Collapse
 
petermortensen profile image
Peter Mortensen • Edited

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?

Collapse
 
phantas0s profile image
Matthieu Cneude

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

Collapse
 
yechielk profile image
Yechiel Kalmenson

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.

Collapse
 
bobbyiliev profile image
Bobby Iliev • Edited

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!

Collapse
 
roneo profile image
Roneo.org

In the function mk, you can use mkdir -p instead of mkdir to create subfolders if required

Collapse
 
yechielk profile image
Yechiel Kalmenson

Thanks!