DEV Community

My Favorite Bash Tips, Tricks, and Shortcuts

Yechiel Kalmenson on January 11, 2021

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

Collapse
 
dougaws profile image
Doug

!$ is the last word from the previous command. Say you run

ls -l myfile.exe

and notice no execution bit is set: chmod +x !$.

Collapse
 
killianfrappartdev profile image
Killian Frappart

Awesome list of tricks! 🔥🔥🔥

Collapse
 
dovidweisz profile image
dovidweisz

My favorite alias is grip='grep -i' for case-insensitive grep.

Collapse
 
petermortensen profile image
Peter Mortensen

"Ae you like me""Are you like me"

Collapse
 
yechielk profile image
Yechiel Kalmenson

Whoops! Thanks!

Collapse
 
villivald profile image
MV

Great post 👍 Actually it seems to be Ctrl + R for search on Mac as well, at least on my Mac.

Collapse
 
yechielk profile image
Yechiel Kalmenson

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!

Collapse
 
shwetabh1 profile image
Shwetabh Shekhar

Loved the tips. Thanks for sharing!

Collapse
 
jorgemadson profile image
Jorge Madson

Awesome post!

Collapse
 
yechielk profile image
Yechiel Kalmenson

Thanks! 🙏

Collapse
 
scrabill profile image
Shannon Crabill

Ooooooh, these tips are great. Thanks for sharing!

Collapse
 
aabidsofi profile image
aabidsofi19

These are life saving tips ...

Collapse
 
yiddishekop profile image
Yehuda Neufeld

Thanks, good tips, and very well written!