Hacker Newsnew | past | comments | ask | show | jobs | submit | refp's commentslogin

FAQ updated: https://refp.se/articles/your-shell-and-the-lazy-exclamation...

really appreciate the feedback, thanks a million!


I think you are very much on point, I can definitely relate to the complexity of C++ turning it both into a wonderful adventure, but also a swamp where every step feels uncertain.

Disclaimer; author of this post, and also https://refp.se/articles/non-constant-constant-expressions and related post in that series.


Author here, thanks for reading and taking time to reply!

---

You are very much correct that `Alt-.` with modifiers can do more than what has been described, it is however important to note that the behavior of `Alt-<key>` changes heavily both between zsh and bash, but also between `vi-mode` and `emacs-mode`.

For instance, zsh will switch to `vi-mode` depending on env variables such as `$EDITOR`, making `Alt-<key>` unavailable in that regard.

---

It's also worth noting that `ctrl-alt-e` is _not_ a zsh default, though it is a fair point that it could have been added in the below linked section:

https://refp.se/articles/your-shell-and-the-lazy-exclamation...

For what it's worth, I had troubles finding a proper scope for the article contents — and with that the lines became a bit blurry in terms of what to include, and what not to include.

---

I really appreciate constructive comments like yours, and hope it together with the article can help future shell ninjas!


Also, on the note of "you can do everything with Alt-<key>", I'd like to highlight that modifiers such as `:h` (directory part), `:t` (filename), and `:r` (strip extension) etc., is what the article was aimed at when saying you can do more with event-designators.

What you can do with what is always hard to answer given that all roads lead to Rome, though some roads have more turns than others.


Your questions would be a great addition to the FAQ, thank you!

--

> When I enter `sudo !!` will it be stored unchanged in the shell history, or stored after interpolation of `!!`?

event-designators themselves do not end up in history, it will be the expanded contents:

    $ echo "hello world"
    hello world
    $ echo !$:s/world/idoubtit/
    echo "hello idoubtit"
    hello idoubtit
    $ history
        1  echo "hello world"
        2  echo "hello idoubtit"
        3  history
The above is ironically enough quite annoying when writing a post about event-designators, as you'd want to reach for previous pre-interpolation commands.

> When I start a command line with a space, so that zsh does not put it in the shell history, will !… also ignore that line?

leading space to ignore adding things to the history is not default behavior, but event designators will still be able to capture their contents. Notice how "echo 321" is _not_ part of the history, but our usage of !$ is:

    % setopt HIST_IGNORE_SPACE
    % echo "abc"
    abc
    %  echo "321"
    321
    % echo last-argument-was !$
    echo last-argument-was "321"
    last-argument-was 321
    % history
    22092  setopt HIST_IGNORE_SPACE
    22093  echo "abc"
    22094  echo last-argument-was "321"
> Is `!$` recursive?

I think this is indirectly answered by the above, if not let me know and I will see if I can answer.

---

Will try to update the post within the next few hours with your additional questions — much much appreciate the feedback, and thanks for reading!


Worth noting that `ssh[up]` isn't available by default in bash nor zsh — though it wouldn't surprise me if helpers such as oh-my-zsh enables it for you. `ssh[up]` is certainly an awesome feature — though (perhaps sadly) not default behavior.

> I've been burnt before using ! and forgetting what command I typed previously

If you want to make sure you do not execute commands you did not intend to, the section linked below is a great start:

- https://refp.se/articles/your-shell-and-the-lazy-exclamation...

You may also look into `magic-space` which makes the expansion of event-designators happen inline, I thought about including it but the article felt long even without it.

Thanks for reading the article, I hope it was a worthwhile read even with alternate workflows elsewhere!


Thanks for the tips. These days I don’t use sh derivatives as my primary shell and the shell I do use has a few features to make command search more ergonomic and safer. But these are useful tips nonetheless


> you have to accurately remember the past

From personal experience I rarely reach for event-designators beyond the current viewport of the terminal. Referring to a command beyond that is, as you very much correctly point out, an easy way to get behavior you don't want. However, if you are always referring to things that are near-memory or even still directly visible in your terminal buffer — the gun-to-foot ratio becomes manageable. I use many (many) of these daily without issues, but it does require some additional discipline.

Also, and this I find important to note, `!!` and `!$` sit outside of the memory argument as they always refer to the last command and last-command's last-argument respectively — as such there shouldn't be any recall related problems (unless you are context switching and come back to a shell in a state you don't remember).

A common occurrence when my posts acquire attention is that I retroactively realize that some things could have been conveyed better; I read every comment and yours is a blessing in disguise to improve future writing.

Thanks for reading the article, and thanks for indirectly making me a better writer!

---

EDIT:

by the way, for what it's worth, the below linked section describes how to effectively explicitly confirm what you would run as a safety measure if unsure:

https://refp.se/articles/your-shell-and-the-lazy-exclamation...


Frankly,

    !-2 && !!
and its variations (like '!-2 | !!') is pretty much the most use I've gotten out of this feature.


The above is a useful idiom that has a somewhat different feel than using !$, etc. Construction versus surgery.


the "mixing of sudo"-example is using `!!`, which as explained is the immediately previous command, a little confused how that translates to the "execute command somewhere in the history" argument?


I suppose it depends on the kinds of mistakes to which one is prone.


I wonder which language creator would be hired to figure that one out; are you creative enough to think of one on the spot?


Lisp translator


https://www.reddit.com/r/dataisbeautiful/comments/1v874lz/my...

I submitted the above link, seems HN is rewriting it automatically. :-/


> My girlfriend sat me down on June 20th and told me that she had been cheating on me and was choosing to be with "the other man".

> This is my body's reaction to that news.

> HR, Sleep, and Energy scores recorded by my [smartwatch]


Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: