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...
For further actions, you may consider blocking this person and/or reporting abuse
The history-search-* enable a "search by prefix".
Let's say I have this in my history.
if I type
vi
and start pressing theup arrow
bash will only show me the entries that begins withvi
. 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
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.Just copy-pasted your code into my .bashrc! That is an amazing trick!
Hey! Would you like to add this sorcery at github.com/aashutoshrathi/awesome-...?
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.
You should really have a look at github.com/junegunn/fzf
This changed my life and how I work with the command line forever.
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
will append| fzf
at the end of a command andAlt + p
does the same but withless
. 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.Other readline shortcuts are also pretty handy:
These also work with many terminal applications such as repls.
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.
Awesome!
I've been using the Home and End buttons to go to the beginning/end of the line.
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... 😉
Didn't know about Ctrl-R. That really is a lifesaver!
I also just published an article with my favorite bash functions and aliases.
A small byte of bash aliases and functions.
Nilkun ・ Jan 12 ・ 1 min read
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
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!
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?
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.
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!
In the function
mk
, you can usemkdir -p
instead ofmkdir
to create subfolders if requiredThanks!
!$ is the last word from the previous command. Say you run
ls -l myfile.exe
and notice no execution bit is set: chmod +x !$.
Awesome list of tricks! 🔥🔥🔥
My favorite alias is
grip='grep -i'
for case-insensitive grep."Ae you like me" → "Are you like me"
Whoops! Thanks!
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!
Loved the tips. Thanks for sharing!
Awesome post!
Thanks! 🙏
Ooooooh, these tips are great. Thanks for sharing!
These are life saving tips ...
Thanks, good tips, and very well written!