Live data from Hacker News

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

blog.hofstede.it

181–190 of 287 posts

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

#181
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

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

#183
Great write up, had to bookmark so I can go through it more later there so much good stuff in there.

For the CTRL + R tip, you can make it even better if you install fzf. Massively improves searching through history. It's worth the install just for that one feature.

Best thing I ever did as a dev was start spending more time in the terminal. Getting familiar with the tools and how they interact makes life so much easier.

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

#184
post #174

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 ```

Wow I hate* that. I use bracket comments. They're cool cause they are bracket comments, so I use it in scripts to document pipelines. They are annoying cause they are bracket comments, in an interactive shell I have to type more and in TWO places. It's fun to reason-out how it works ;) $ echo foo | tr fo FO | sed 's/FOO/BAR/' BAR $ echo foo | ${IFS# tr fo FO | } sed 's/FOO/BAR/' foo It's nice to have a way to both /*…

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-guide in case anyone wants to take a look

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

#185

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.

> 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'.

Or just in insert mode. and are standard Vim insert mode commands.

https://vimhelp.org/insert.txt.html#i_CTRL-U

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

#186
post #64

My header on top of every script #!/usr/bin/env bash set -eEuo pipefail # shellcheck disable=SC2034 DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )" #######################################################

Wait... Most of my shell scripts have zero unused variables: I prefer to comment them if I may need them later on. Why do you disable SC2034? I don't think not having unused variables prevent me from doing things in my scripts!? I understand if it's a preference but SC2034 is basically one of my biggest timesavers: in my case unused variables are typically a bug. Except, maybe, ANSI coloring variables at the top of t…

I disable it only for the DIR variable which I might not use.

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

#187
post #72

Regarding history: I have a function in my ZSH config which excludes certain things from the history. Especially things that can break stuff when my sausage fingers CTRL-R the wrong thing Something like this: # Prevent certain strings from appearing in the history # Anything starting with a leading space is ignored # Anything containing "--force" or "whatever" is ignored function zshaddhistory() { emulate -L zsh if !…

That's very cool!

To take advantage of the "leading space" one, I have this, to mark some commands that I never want to record:

       unhist () {
         alias $1=" $1"
       }
       unhist unhist
       unhist fzf
       unhist rghist     #custom command that greps .zhistory,...

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

#188
post #87

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

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

Prefix search is faster for the majority of cases. CTRL-r / FZF is useful for the remaining ones.

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

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

Depends on the shell - bash on my Ubuntu deletes entire '/var/log/nginx/', while after switching to sh it deletes only nginx

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

#190

There's one thing you need to only think about once, and has the potential to save you a ton of time: profile your ZSH startup time! Stuff like NVM or Oh My ZSH will add a few seconds to your shell startup time.

Agreed. I lazy-load NVM to get around that:

  lazy_nvm() {
    unset -f nvm node npm npx
    [ -s "$NVM_DIR/nvm.sh" ] && . "$NVM_DIR/nvm.sh"
  }
  nvm()  { lazy_nvm; nvm "$@"; }
  node() { lazy_nvm; node "$@"; }
  npm()  { lazy_nvm; npm "$@"; }
  npx()  { lazy_nvm; npx "$@"; }
Post reply on HN