From 43ea7acd7429edfc31df7e5c6d5ed3c40b2b4bba Mon Sep 17 00:00:00 2001 From: jbranchaud Date: Mon, 21 Oct 2024 11:21:21 -0500 Subject: [PATCH] Add Use A Space To Exclude Command Fromm History as a Zsh TIL --- README.md | 3 +- ...a-space-to-exclude-command-from-history.md | 37 +++++++++++++++++++ 2 files changed, 39 insertions(+), 1 deletion(-) create mode 100644 zsh/use-a-space-to-exclude-command-from-history.md diff --git a/README.md b/README.md index d2146ba..24dec9c 100644 --- a/README.md +++ b/README.md @@ -10,7 +10,7 @@ pairing with smart people at Hashrocket. For a steady stream of TILs, [sign up for my newsletter](https://crafty-builder-6996.ck.page/e169c61186). -_1478 TILs and counting..._ +_1479 TILs and counting..._ --- @@ -1758,6 +1758,7 @@ _1478 TILs and counting..._ - [Add To The Path Via Path Array](zsh/add-to-the-path-via-path-array.md) - [Link A Scalar To An Array](zsh/link-a-scalar-to-an-array.md) +- [Use A Space To Exclude Command From History](zsh/use-a-space-to-exclude-command-from-history.md) ## Usage diff --git a/zsh/use-a-space-to-exclude-command-from-history.md b/zsh/use-a-space-to-exclude-command-from-history.md new file mode 100644 index 0000000..d5203bf --- /dev/null +++ b/zsh/use-a-space-to-exclude-command-from-history.md @@ -0,0 +1,37 @@ +# Use A Space To Exclude Command From History + +When using a shell like `zsh`, you get the benefit of it keeping track of the +history of the commands you've entered into the shell. This means you can +quickly traverse pack to a previous command that you want to run again. It also +means [a tool like `fzf` can hook into your history +file](https://github.com/junegunn/fzf?tab=readme-ov-file#key-bindings-for-command-line) +so that you can fuzzy-search for a command you may have executed weeks ago. + +The history is stored on your machine in a plaintext file. Not every command +should be stored in a plaintext file. For instance, you don't want `zsh` to +persist a command that includes a password. + +With the `histignorespace` option enabled in `zsh`, we can put a leading space +in front of our command and it will be excluded from the history file. + +Try it yourself: + +```bash +$ echo 'this command will be remembered' +this command will be remembered + +$ echo 'this command will be forgotten' +this command will be forgotten +``` + +Notice the leading space in the second command. Trying pressing your _up_ arrow +and notice only that first `echo` is remembered. + +Make sure `histignorespace` is included in the list when you run `setopt`. If +it isn't, then add it: + +```bash +$ setopt histignorespace +``` + +[source](https://stackoverflow.com/questions/8473121/execute-a-command-without-keeping-it-in-history/49643320#49643320)