Live data from Hacker News

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

blog.hofstede.it

211–220 of 287 posts

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

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

You're telling me!!!

(I use vim daily, with multiple splits in a single instance.)

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

#212

Its almost ironical that we still use the Terminal - and many use it like in the eighties using Bash - and seem to have forgotten that we should invent a better terminal & shell than doing all the workarounds to handle the quirks of the current systems.

Make a better system, and we'll consider using it. A Terminal + Bash/ZSH is soooo sticky because they are VERY good at what they do once you learn the basics and quirks. And now with LLMs, CLIs are even better because LLMs talk in text and CLIs talk in text. Microsoft tried with PowerShell to design a better system; it "technically" is better, but not "better enough" to justify the cost of switching (on Linux). The s…

I use the Terminal all the time and write my own CLI tools, but I'm feeling more and more the limits of the current system. With the years I have used almost all available shells (EShell was even my default for some time). Right now my favorite shell is Nushell, but still, it feels dated compare to what is possible on modern computers.

> Make a better system, and we'll consider using it. It's on my TODO list, but it will break with all conventions and tools (no TTY). My idea is to bring the chain-things-together idea to the 21st century using a keyboard first GUI.

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

#213
post #207
post #184

Earlier quoted context omitted.

for multiline pipes, it's WAY better to format like foo | bar | baz You don't have to use backquotes, AND, it allows you to comment line by line, because there's no backslash messing with the parser. I also use a last `|\ncat` so you can delete any line and you don't have to worry about the last line being a bit different than the rest I created a list of similar tricks in https://github.com/kidd/scripting-field-guid…

You'll probably dislike this too: $ { > echo foo \ > && echo bar \ > || echo baz ; > } foo bar $ IFS ${IFS# echo foo && echo bar || echo baz ; } $ _ There's good and bad to both approaches. I like how I can use () and {} to bracket things and otherwise every line that end in \ is continued. I line-up on the left with the operator, you with indentation. When you use a # style comment, you have to look up and back and…

aha! I see what you mean, it's indeed a nice option, yep.

Using brackets like this is something I never thought of, and it's probably why it's hard for me to process it, but I can see it provides nice annotation capabilities, and it's a more self-contained style.

Thx for sharing!

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

#214

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.

I've never understood why emacs mode became the default. "set -o vi" is the _first_ command I type in a new shell.

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

#215

For me the ultimate trick is to open the current prompt in vim with F2 (Ctrl+X ctrl+E seems to work too): # Use F2 to edit the current command line: autoload -U edit-command-line zle -N edit-command-line bindkey '^[OQ' edit-command-line # f2 is ^[OQ; to double check, run `xargs` and then press f2

> # f2 is ^[OQ; to double check, run `xargs` and then press f2

I remember using `cat -v` before learning that `xargs` exists… or maybe before `xargs` actually existed on systems I used :)

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

#216

One thing I find life-changing is to remap the up arrow so that it does not iterates through all commands, but only those starting with the characters I have already written. So e.g. I can type `tar -`, then the up arrow, and get the tar parameters that worked last time. In zsh this is configured with bindkey "^[OA" up-line-or-beginning-search # Up bindkey "^[OB" down-line-or-beginning-search # Down

Atuin is better than anything I’ve used in a shell.

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

#217

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

it is an additional burden to switch to shell vi mode, it is not the standard. Maybe you can put it in all of yout bashrc files but you will probably hear some swearing from the people logging to your machines :).

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

#218

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 $ stu…

You missed an easier alternative that was in the article: ctrl-u saves and clears the current line, then you can input new commands, then use ctrl-y to yank the saved command. With zsh, I prefer to use alt-q which does this automatically (store the current line, display a new prompt, then, after the new command is sent, restore the stored line). It can also stack the paused commands, e.g.: $ cp foo/bar dest/ $ wcurl…

When you're killing (C-u, C-k, C-w, etc) + yanking (C-y), you can also use yank-pop (bound to M-y in bash and zsh by default) to replace the thing you just yanked with the thing you had killed before it.

  $ asdf
  $                  # now kill ring is ["asdf"]
  $ qwerty
  $                  # now kill ring is ["qwerty", "asdf"]
  $             # "yank", pastes the thing at the top of the kill ring
  $ qwerty      # "yank-pop", replaces the thing just yanked with the next
                     # thing on the ring, and rotates the ring until the next yank
  $ asdf

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

#219
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... You're telling me!!! (I use vim daily, with multiple splits in a single instance.)

CTRL+SHIFT+T will resurrect your most recently closed tab, with history. Pressing it again will bring up the next most recently closed tab, with history. Etc.

Or maybe you don’t use SHIFT. Can’t recall right now. My fingers know but I’m not at a computer.

Anyway, browser menus can also show you recently closed tabs and bring them back.

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

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

Firefox v147 finally added the ability to redefine keyboard shortcuts, including ^w: https://news.ycombinator.com/item?id=46952095

1. Load about:keyboard

2. Find "Close tab" and click "Clear" or "Change".

Post reply on HN