Live data from Hacker News

Shell Tricks That Make Life Easier (and Save Your Sanity)

blog.hofstede.it

21–30 of 287 posts

Re: Shell Tricks That Make Life Easier (and Save Your Sanity)

#21
post #13
post #8

CTRL + W usually deletes everything until the previous whitespace, so it would delete the whole '/var/log/nginx/' string in OP's example. Alt + backspace usually deletes until it encounters a non-alphanumeric character. Be careful working CTRL + W into muscle memory though, I've lost count of how many browser tabs I've closed by accident...

In my terminal it's the exact opposite – Alt-Backspace deletes to the previous space, whereas Ctrl-W deletes to the last non-alphanumeric (such as /). I'm using fish shell in an Alacritty terminal. Yeah, pressing Ctrl-W accidentially is a pain sometimes ... but Ctrl-Shift-T in Firefox is a godsend.

> Yeah, pressing Ctrl-W accidentially is a pain sometimes ... but Ctrl-Shift-T in Firefox is a godsend.

Fun fact: despite having absolutely no menu entry for it, and I believe not even a command available with Ctrl+Shift+P, Vscode supports Ctrl+Shift+T to re-open a closed tab. Discovered out of pure muscle memory.

Re: Shell Tricks That Make Life Easier (and Save Your Sanity)

#22
post #9

> The “Works (Almost) Everywhere” Club > The Backspace Replacements Also known as "emacs editing mode". Funnily enough, what POSIX mandates is the support for "vi editing mode" which, to my knowledge, almost nobody ever uses. But it's there in most shells, and you can enable it with "set -o vi" in e.g. bash.

Vi mode is also available in Claude code and gemini-cli to give some recent examples, and a bunch of other places you might not expect it, as well the more obvious places where code is written. Once you get used to it, it is painful to go back.

My biggest complaint about the fish shell is the lack of true vi mode. They attempt to emulate it and it works to some degree, but it's no comparison to readline's implementation.

Re: Shell Tricks That Make Life Easier (and Save Your Sanity)

#23
Something that should be mentioned is starting a command with a space doesn't add it to your history in most shells, really useful for one-off commands that you don't want cluttering your history.

Also, increase your `$HISTSIZE` to more than you think you would need, there have been cases where it helped me find some obscure command I ran like 3 years before.

Re: Shell Tricks That Make Life Easier (and Save Your Sanity)

#27

I love this, from a comment on the article: 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 ```

Yes! That one's going in my $PATH. Such a useful use of cat!

Re: Shell Tricks That Make Life Easier (and Save Your Sanity)

#28
post #8

CTRL + W usually deletes everything until the previous whitespace, so it would delete the whole '/var/log/nginx/' string in OP's example. Alt + backspace usually deletes until it encounters a non-alphanumeric character. Be careful working CTRL + W into muscle memory though, I've lost count of how many browser tabs I've closed by accident...

> Be careful working CTRL + W into muscle memory though, I've lost count of how many browser tabs I've closed by accident...

This hurts.

Also, for the shell, if you do C+w, you can "paste" it back using C+y. Assuming you have not removed that configuration.

Re: Shell Tricks That Make Life Easier (and Save Your Sanity)

#29
One trick I use all the time:

You're typing a long command, then before running it you remember you have to do some stuff first. Instead of Ctrl-C to cancel it, you push it to history in a disabled form.

Prepend the line with # to comment it, run the commented line so it gets added to history, do whatever it is you remembered, then up arrow to retrieve the first command.

$ long_command

$ #long_command

$ stuff_1 $ stuff_2

$ #long_command

$ long_command

Re: Shell Tricks That Make Life Easier (and Save Your Sanity)

#30

Using the terminal becomes much more cozy and comfortable after I activate vim-mode. A mistake 3 words earlier? No problem: 3bcw and I'm good to go. Want to delete the whole thing? Even easier: cc I can even use v to open the command inside a fully-fledged (neo)vim instance for more complex rework. If you use (neo)vim already, this is the best way to go as there are no new shortcuts to learn and memorize.

Oh wow I didn't know about this, thank you. The underlying feature is called "readline vi-mode" for folks who want to search more about it.
Post reply on HN