Hacker News new | ask | show | jobs
by sethrin 3205 days 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/
2 comments

Curly 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}

Worth 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
Well you learn something every day. That's pretty awesome! Is there an easy way to add options? Like -O2
yeah, reading make man, and also http://nullprogram.com/blog/2017/08/20/
also, useful to know - zsh supports tab-completion for curly brace expansion.
I should use history substitution a lot more. Thanks
Everyone 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.
Don'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'.

Well, I tend to assume that I'm not the only one who continually forgets to add 'sudo' in front of 'apt-get', or similarly that journalctl usually requires elevated privileges, or that I don't own the files I'm trying to chmod. I suspect that people who do recall such things perfectly are rare. As to muscle memory, well, probably my assumptions there are off. I've been using Linux exclusively both professionally and at home for more than ten years now, and I have a bad habit of coding at odd hours of the night. Other people may not have quite the same exposure or error rate.

I certainly wouldn't advocate blindly retrying; I only use 'sudo !!' when my shell tells me to. That unfortunately is pretty often :(

!$ 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.
That'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.
wow ok, neat... gonna have to spend some time with your book, Thanks!
oh 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