Live data from Hacker News

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

blog.hofstede.it

101–110 of 287 posts

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

#101

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

Did this many years ago (but with bash) -- life changing is an apt way of saying it.

Here's the Bash commands for this in case anyone is looking for them

  bind '"\e[A"':history-search-backward
  bind '"\e[B"':history-search-forward

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

#102

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.

I use qwerty and azerty, and in both I never felt typing the sequence was any harder than typing any other regular word. Generally speaking, I prefer sequential "shortcuts" then multikey bindings.

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

#103

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

This can also be achieved with `.inputrc`:

    "\e[A": history-search-backward
    "\e[B": history-search-forward

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

#104
post #15

> cd -: The classic channel-flipper. Perfect for toggling back and forth. And not only cd. Gotta love 'git checkout -'

The '-' shortcut is weird. In 'git commit -F -', the '-' is actually /dev/stdin.

- is a pretty standard idiom for using stdin/stdout instead of a named file that you can find in many commands. I don't think it conflicts with the cd/checkout usage though as there the argument normally does not refer to a file so having - mean stdin/stdout doesn't make sense.

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

#105
post #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.

HISTCONTROL=erasedups can also help keeping more obscure commands in your history, at the expense of context around commands.

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

#106
post #87

Earlier quoted context omitted.

Once you start using CTRL+r, you may find that you never reach for up arrow again.

And once you want to one-up this look into fzf.

And once you get tired of fzf and want something better, you reach for https://atuin.sh.

Completely transformed all of my workflows

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

#107

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 been a vim/nvim casual user for the past year or two, and I still feel as if I'm slightly less proficient in it for the amount of time that I put into it.

I really need to get around to playing with it more. I just hope that especially now with genAI that it's not too late for learning it further.

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

#108
post #15

> cd -: The classic channel-flipper. Perfect for toggling back and forth. And not only cd. Gotta love 'git checkout -'

The '-' shortcut is weird. In 'git commit -F -', the '-' is actually /dev/stdin.

`-` is the traditional shell way to refer to stdin/stdout (as with your git commit example) but also the traditional way to refer to the last directory you were in (as with git checkout/switch).

You would never pipe the output of a command to `cd` so the `-` shortcut couldn't be helpful to cd as-is. So rather than invent yet another shortcut to memorize for `cd` they reused the existing one which otherwise would be redundant, which I appreciate at least.

But git is simply being consistent with the shell to further reduce the cognitive complexity of reusing shell commands you're used to in analogous git contexts.

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

#110

Earlier quoted context omitted.

> cc Doing control+o in insert mode temporarily places you into normal mode so that you can execute one normal-mode command, and then go back to insert mode again--no need to hit 'i' again. So, instead of ' cc', ' S'.

The vim version is much easier, if you ask me: 3 strokes, 2 keys and 0 combinations. The one you suggest however requires 4 strokes (ctrl then o then shift then s), 4 keys (ctrl, o, shift, s) and 2 combinations. The "cc" sequence deletes the line and switches automatically to insert mode. To forgo the switch, the sequence then becomes "dd".

Maybe I have my bash/readline vi mode configured specially to do this, but if I want to delete the entire line and type a new one (from anywhere in that line), I do something simpler than either of these alternatives:

S

Esc exits insert mode (of course) and capital S erases the line and puts you in insert mode at column 0 (just like in (n)vim, right?).

Like I said, maybe I configured that? But 'S' is standard vim-stuff... (I'm not able to double check my config at the moment).

[Edit: right after hitting submit I realized that my way is perhaps "arguably" simpler because I do have to hit shift to get capital S. So I'm also hitting three keys...]

Post reply on HN