Hacker News new | ask | show | jobs
by agumonkey 3198 days ago
I should use history substitution a lot more. Thanks
1 comments

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