If you've been using the Unix/Linux command line for any length of time, you're certainly familiar with time-saving techniques like tab completion and reverse-i-search. Chances are you use these darling keystroke-savers daily.
These features of course make a ton of sense—why should *nix force you through the unbearable tedium of typing a full directory name when the computer is perfectly capable of guessing what you're about to type?
Well, today I'm about to (hopefully) blow your mind with another time-saver of whose existence you might not have been aware. It's called brace expansion.
The problem
Tell me if this looks familiar: you want to make just a minor change to a filename, but since the file is so deeply nested in your directory structure, you have to type SO...MUCH...STUFF just to slap a few characters onto the beginning:
mv app/views/appointments/_services.html.erb app/views/appointments/_service_list.html.erb
Totally un-DRY.
The solution
Thanks to brace expansion, you can type only the unique parts. The following command is functionally equivalent to the first one:
mv app/views/appointments/{_services.html.erb,_service_list.html.erb}
Isn't that wonderful? Go ahead and pop open a console right now to try it out. It's an absolute delight.
It works everywhere
The utility of brace expansion is not limited to the mv
command. You can use it withcp
or anywhere else you think makes sense. If you're a Vim user, you can try something like this, which will open three files in tabs:
vi -p app/views/appointments/{new.html.erb,edit.html.erb,show.html.erb}
In addition to useful things you can do silly things: touch file{1..5}.html
Why would you ever want to do that? I don't know, but you can!
That's it. Hopefully that seems useful. You'll thank me when you're 90 years old and thinking back to all the wonderful things you did with those extra 12 minutes you saved over the years, courtesy of brace expansion.