New post: shell tricks that aren't exactly secret, but aren't always taught either.

Split into two sections: what works on any POSIX sh (FreeBSD, OpenBSD, Alpine...) and what's Bash/Zsh-specific. Because not everyone is on Linux with bash as their login shell.

Things like CTRL+W, $_, pushd/popd, fc, set -euo pipefail caveats, and more.

https://blog.hofstede.it/shell-tricks-that-actually-make-life-easier-and-save-your-sanity/

#unix #bash #shell #sysadmin #freebsd #linux #cli

Shell Tricks That Actually Make Life Easier (And Save Your Sanity)

Watch someone backspace 40 characters instead of pressing CTRL+W, and you’ll understand why this list exists. A collection of shell tricks-grouped by what works everywhere and what’s Bash/Zsh-speci...

Larvitz Blog

@Larvitz I recently learned a funny trick from Kirils Solovjovs in a bash workshop. He had in his path a script called `\#` that he used to comment out pipe elements like `mycmd1 | \# mycmd2 | mycmd3`. This was how the script was written:

```
#!/bin/sh
cat
```

@Larvitz I often use "Meta+." (in your config: ALT+. I guess) to get the last argument of the last executed command.

@Larvitz side note, I don’t think OpenBSD has POSIX sh. Quoting the man page

This version of sh is actually ksh in disguise. As such, it also supports the features described in ksh(1). This manual page describes only the parts relevant to a POSIX compliant sh.

Just saying as I got caught thinking my scripts were POSIX compliant on OpenBSD and failed when running them using FreeBSD sh 🤦‍♂️

@joel thanks for the hint. I actually wasn’t aware of that! Always thought it’s defaulting to a korn shell.
@Larvitz I learned so much from this. It’s going in my bookmarks.
@Larvitz I wish I'd known about C-x C-e years ago, and also yesterday it would have been convenient. But now I know it for the future. Thank you!
@Larvitz THANK YOU VERY MUCH!!!! A lot of things I did not know up to now. That helps a lot.

@Larvitz

Ah, you don’t really need that anymore these days 🫣 — Gemini CLI and Codex are faster anyway 😉

@Larvitz Extra bonus fact, this works on anything that uses readline. Python REPL, password prompt, etc. And for even more fun, in your shells, (most shells afaik) you can switch the mode, and use vi style navigation instead of the default emacs style: https://www.gnu.org/software/bash/manual/html_node/Readline-vi-Mode.html
Readline vi Mode (Bash Reference Manual)

Readline vi Mode (Bash Reference Manual)

@Larvitz

I find that I tend not to use the `sudo !!` since it's about as easy to type control+p (recall previous command from history), control+a (beginning of the line) and type my "sudo " there which works in most shells (not all support "!!").

One other one that I use regularly is control+x,control+e (use my $EDITOR/$VISUAL to edit the command I'm currently typing) and its sibling `fc` (use my $EDITOR/$VISUAL to edit the previous command I just issued). Support varies, but they work in bash & zsh; and OpenBSD's ksh supports `fc` (though it uses ed(1) not your $EDITOR/$VISUAL).

@Larvitz About the `pushd` / `popd` trick: you can use it to switch between set of directories with `dirs -v` to get list of directories on stack and `pushd +<n>` (e.g. `pushd +1`) to switch to n-th directory on the stack.

About `tee`: I have recently found a nice trick to make the `tee` command useful in presence of progress bars and colors, namely `| tee >(grep -v $'\r' | sed 's/\x1B\[[0-9;]\{1,\}[A-Za-z]//g' >command.out)` - which probably could be simplified.

@Larvitz You must tell in your article about Alt-[0-9] (really Alt-n where n is a number) feature when you talk about ALT-F, ALT-B and ALT-. , CTRL-W and maybe more ...

It allow you to modify the target. Move n words before / after o select the n th argument, removing n words ...

Great post!!