5 ms·
The ones I get a lot of use out of are: curly brace substitution: $ mkdir -p new_project/{img,js,css} mkdir -p new_project/img new_project/js new_project/
by sethrin 9y ago
The ones I get a lot of use out of are:
curly brace substitution:
$ mkdir -p new_project/{img,js,css}
mkdir -p new_project/img new_project/js new_project/css
$ mv some_file.txt{,.old}
mv some_file.txt some_file.txt.old
Caret substitution:
# systemctl status mysql.service
-- snip output --
# ^status^restart
systemctl restart mysql.service
Global substitution (and history shortcut):
$ echo "We're all mad here. I'm mad. You're mad."
we're all mad here. I'm mad. You're mad.
$ !!:gs/mad/HN/
we're all HN here. I'm HN. You're HN.
I have a (WIP) ebook with more such tricks on GitHub if anyone is interested:
https://tenebrousedge.github.io/shell_guide/ https://tenebrousedge.github.io/shell_guide/
- godelski 9y agoCurly brace expansion is one of the most useful and overlooked topic in these kind of posts. And I'm always surprised it isn't mentioned with compiling. g++ -o foo{,.cpp}
- tych0 9y agoWorth noting that for the particular case you've cited, you can just use make, even without a makefile: ~ cat test.cpp #include <iostream> using namespace std; int main() { cout << "hi\n"; return 0; } ~ stat Makefile stat: cannot stat 'Makefile': No such file or directory ~ 1 make test g++ test.cpp -o test ~ ./test hi
- skykooler 9y agoalso, useful to know - zsh supports tab-completion for curly brace expansion.
- agumonkey 9y agoI should use history substitution a lot more. Thanks
- sethrin 9y agoEveryone should probably have $ sudo !! ingrained in muscle memory. The other even more useful one is !$ to get the last word of the previous line. It's probably the terminal feature I use most.
- agumonkey 9y agooh I use !! but for a reason the ^old^new never stuck. I don't like ^ maybe and instead of !$ I use ALT + . maybe it will stick this time
- lozf 9y ago!$ is not merely the last word, but the last argument - particularly handy when it's a long path. Also love the way zsh expands these with a single tab press - great for clarity or further editing.
- sethrin 9y agoThat's technically incorrect. !$ is a shortcut for !!:$, which is the last word of the previous command in history. A filename is treated as a single word, as you say. This is however distinct from the last argument, which is accessible with $_ . As an example: $ echo 'example' > file.txt Here !$ would return the filename, and $_ would return 'example', which was the last argument to echo.
- lozf 9y agowow ok, neat... gonna have to spend some time with your book, Thanks!
- mynewtb 9y agoDon't blindly retry with sudo. I cannot imagine in which kind of environment you would use 'sudo !!' so much, that it becomes muscle memory. That sounds too much like my colleagues' favorite way of ruining their windows systems: 'It did not work so I ran it again as administrator'.