Live data from Hacker News

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

blog.hofstede.it

191–200 of 287 posts

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

#191
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...

'man readline' contains all the useful key combinations.

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

#192
post #158

Just recently, I came up with this in my .bashrc, basically a "deep cd" command: dcd() { # If no argument is given, do nothing [ -z "$1" ] && return # Find the first matching directory under the current directory local dir dir=$(find . -type d -path "*$1*" -print -quit 2>/dev/null) # If a directory was found, cd into it [ -n "$dir" ] && cd "$dir" } I thought this would be way too slow for actual use, but I've come to…

you should look into autojump which has `jc` (jump child), or other similar flavours of "smart cd" (z, fzf, etc)

Thanks, but I like that this is just a small, simple bash function without the need to install 3rd-party software.

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

#193

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.

This reminds me of an excerpt from an old Emacs manual: . . . if you forget which commands deal with windows, just type @b[ESC-?]@t[window]@b[ESC]. This weird command is presented with such a benevolent innocence as if it's the simplest thing in the world. I think the better advice for command-line editing would be to set up the mouse.

I have yet to see a shell that has mouse enabled line editing support. It should certainly be possible though.

I do prefer vi bindings at the same time though. Vi bindings and mouse support complement each other well, you don't have to choose one or the other, just use whichever feels most natural and convenient in that exact moment.

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

#194

Maybe not a shell trick per-se but I have been a very big fan of zoxide. It can jump around your common directories. If you have a ~/workspace/projects and you are anywhere and type `cd projects` it will take you to that directory. I never realized how much I got hooked onto it, until I used a system without it.

Built in functionality that may offer something similar:

https://linuxhandbook.com/cdpath/

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

#196
post #2

Not a fan of the LLM-flavoured headings, and the tips seem like a real mixed bag (and it'd be nice to give credit specifically to the readline library where appropriate as opposed to the shell), but there are definitely a few things in here I'll have to play around with. One thing I dislike about brace expansions is that they don't play nicely with tab completion. I'd rather have easy ways to e.g. duplicate the last…

Not just the heading is LLM-flavoured. So is the writing, e.g. "The shell is a toolbox, not an obstacle course."

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

#197
post #147

Here's my favorite tip: If you use bash, you can write bash on your prompt (duh). But this is one of the biggest reasons I stick with bash everywhere, as I am quite comfortable and experienced in bash and sometimes it's just easier to write things like `for i in *.mp3; do ffmpeg -i $i ...` etc. If it's re-usable, I write it to a bash script later.

That's vaccously true as you said isn't it? I write fish on my shell and then I can save it as a fish script. Worth noting that bash is much more portable and available by default, but if I'm going for portability I go straight to /bin/sh

Fair point, but for scripting I don't feel fish (or zsh) offer an advantage big enough to bother learning that language with their rather narrow scope. But bash it's good to anyways know, you don't really get around it either. Larger/more complex scripts I write in other languages (depending on domain I and other requirements I guess). It's also not that I daily write those scripts on my shell, so I also think that even if I learned fish or zsh, I would have to look up things again every time I need to write something again.

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

#198
post #141
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...

I've installed "More Better Ctrl-W" for Chromium, and mapped Ctrl-W to do nothing, and Ctrl-D to close the current tab

But how am I supposed to create or edit a bookmark?

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

#199

Earlier quoted context omitted.

I've been a (n)vim user for 20+ years now, but I hate vi-mode in the shell. However if I feel that I need to do a complex command, I just do ctrl-x+e to open up in neovim (with EDITOR=nvim set). I find it a good middle ground.

It’s strange. I have heard this from lots of others too. I think I am an anomaly here. I can’t live without shell vi mode

Same - shell vi mode is critical for intensive terminal sessions.

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

#200

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.

3bcw What is your keyboard layout? This looks like a crime against humanity on a regular qwerty kb.

Remap Capslock to Esc. Possible in every OS now.
Post reply on HN