Live data from Hacker News

Zsh Tricks to Blow Your Mind

twilio.com

191–200 of 218 posts

Re: Zsh Tricks to Blow Your Mind

#191
post #138

Knew most of those, not much interested in "shell plugins". One thing that many people miss are parameter expansions. Those are in Posix, so pretty much any bare shell should be able to use that, too. So dash/bash/zsh/ksh… echo Hello ${PLANET:-World} to have a default if you're not sure a variable is set. Replace "-" with "=" to actually set it, not just use it for this statement. Or removing suffixes: filename=/tmp/…

That's a really neat way of trimming the file extension, something that can end up being quite tedious to do with SED. Do you know of a cheatsheet that describes what all the percentage sign means and other things you can do with parameter expansions?

The bash hackers wiki has a pretty good list of the bash/posix one, tldp does too, but I find it less straight forward.

https://wiki.bash-hackers.org/syntax/pe

https://tldp.org/LDP/abs/html/parameter-substitution.html

The zsh docs are good for the zsh specific expansions, but more man page than cheatsheet, though they do have an intro doc on it that's more cheatsheet like but less complete.

http://zsh.sourceforge.net/Doc/Release/Expansion.html

http://zsh.sourceforge.net/Intro/intro_12.html

Re: Zsh Tricks to Blow Your Mind

#192
post #10

zsh4humans is the most recent zsh “trick” I’ve discovered. zsh is a bit like emacs when first installed: it can do a lot of things but it’s not nearly as useful without a lot of manual configuration. I’m honestly surprised it doesn’t have more stars on GitHub. https://github.com/romkatv/zsh4humans

Is this similar to Oh My Zsh [1]? I personally have been using Oh My Zsh for years and I've always been a fan of it. I can quickly add themes and plugins and have my shell ready to go how I like it quite quickly on new systems.

[1]: https://ohmyz.sh/

Re: Zsh Tricks to Blow Your Mind

#193

Earlier quoted context omitted.

Solid selection. I'd also add nnn as the fastest tool to navigate directories I've ever seen.

Have you been able to get it to cd on quit? it's the only thing I can't seem to get. It's amazing though.

Hmm, if you mean the C-g behaviour then you need to set $NNN_TMPFILE:

    nnn() {
      declare -x +g NNN_TMPFILE=$(mktemp --tmpdir $0.XXXX)
      trap "rm -f $NNN_TMPFILE" EXIT
      =nnn $@
      [ -s $NNN_TMPFILE ] && source $NNN_TMPFILE
    }
You can use a static file if you're sure you'll never be running more than one instance.

I'd prefer something like:

    nnn() {
      local tmp=$(mktemp --tmpdir $0.XXXX)
      trap "rm -f $tmp" EXIT
      =nnn -p $tmp $@
      [ -s $tmp ] && cd ${"$(
That will cd to the selected file's directory with enter/right, or do nothing if you simply quit. I guess it depends if you use it for browsing a lot or simply picking a file.

Re: Zsh Tricks to Blow Your Mind

#194

Apple has kind of herded me into zsh for work in the terminal. So far it has been pleasant, with interesting plugins, but it's periodic updates are a little annoying. Despite remaining in zsh while in a terminal, I still find myself writing only bash scripts when a script it needed.

>Despite remaining in zsh while in a terminal, I still find myself writing only bash scripts when a script it needed. I do this too. I think it's good practice; zsh is amazing as an interactive shell, but bash is everywhere.

Bash is not everywhere, but /bin/sh is..!

If portability is important, I'd recommend Bourne shell instead.

Sometimes there's a justification for writing shell scripts in bash. E.g. if you only care about Linux forever.

Frequently though, a simple script will unexpectedly grow, and start to require non-trivial data structures, etc. If there's no time for a rewrite in a more appropriate language (Python/Ruby/Perl), then moving to bash can be a compromise.

Re: Zsh Tricks to Blow Your Mind

#195
post #49

Earlier quoted context omitted.

What was the reason for them doing this do you know? From my experience with it over the last year, zsh is slightly worse at autocomplete than bash, with no advantages. Is there something I'm missing, like a good default .zshrc?

Check out ohmyzsh for a great ~/.zshrc

Awesome, that's pretty amazing, thanks a mill!

Re: Zsh Tricks to Blow Your Mind

#196

Earlier quoted context omitted.

What was the reason for them doing this do you know? From my experience with it over the last year, zsh is slightly worse at autocomplete than bash, with no advantages. Is there something I'm missing, like a good default .zshrc?

Completion is one of the biggest reasons people switch to zsh. Did you initialize the completion system in your .zshrc? https://wiki.archlinux.org/index.php/Zsh#Command_completion

I had done that, but my ssh completions weren't working great at all. I've installed ohmyzsh as recommended below and it's great, definitely a step up from bash.

Re: Zsh Tricks to Blow Your Mind

#197

Earlier quoted context omitted.

That's a really neat way of trimming the file extension, something that can end up being quite tedious to do with SED. Do you know of a cheatsheet that describes what all the percentage sign means and other things you can do with parameter expansions?

The old way to remove a file extension is using basename: basename -s .png example.png example

Also strips the path and you have to know the extension name(s). For my examples, "cut" would be appropriate. But those substitutions tend to work in anything POSIX, not just bash/zsh.

Re: Zsh Tricks to Blow Your Mind

#198
post #113
post #12

Earlier quoted context omitted.

Last time I checked it was slower than https://github.com/romkatv/powerlevel10k , because it doesn't decouple the rendering of the prompt from the background processes collecting status information. Ergo no instant prompt. Also, some other cool tricks w.r.t. prompt vs. backscroll. Has that changed?

Happy fish+starship user here. Recently all modules where refactored to load lazily: https://github.com/starship/starship/issues/2111 A PR adding full async support is close to being merged, which should fix lag issues for good: https://github.com/starship/starship/pull/2223

The power of powerlevel10k is that it can draw the plain prompt immediately, and respond to keyboard immediately, and then backfill any advanced prompt information (and properly shifting the already added text to the right and wrapping it correctly). I know that zsh has support for this, and bash does not (powershell neither); my prime reason why I use zsh. Fish, I don't know how it handles prompts.

This is very powerful in large git repos etc. You're never waiting for the prompt. You just type immediately, w/immediate response. So, you only wait for the status information to show up when you need it. So you also don't pay an excessive penalty if you prefer overly chatty prompts.

AFAICT the lazy-loading and async-support tickets above talk about internal implementation details, timeout support (so cancelling stuff like a too slow status report and showing a simplified prompt). I may misunderstand that, I only gave it a quick glance.

Maybe starship already supports this for zsh, or not, but at least they don't promote it front and center.

Re: Zsh Tricks to Blow Your Mind

#199
I am using zsh and several of these "tricks" don't work.

1- "take" is not a default built-in

2a- that up-arrow is not default behavior

2b- ok, ctrl-r works as expected (works in bash too)

3- automatic cd is not default behavior

4- ok, zmv is a standard module that is explicitly autoloaded in the article

5- zcalc, like zmv, requires loading a standard module, the article doesn't mention that

6- ok, oh-my-zsh has plugins

7- ok, ctrl-q works as expected

8- ctrl-x-e doesn't work for me (but works in bash)

9- ok, ctrl-l works as expected (works in bash too)

All these are probably specificities of oh-my-zsh, a very popular module that I don't use personally, or require special configuration. Nothing wrong with that, but these should be mentioned.

Re: Zsh Tricks to Blow Your Mind

#200
post #124

Earlier quoted context omitted.

This is always my complaint about posts like this. I read them looking for the killer feature to make me consider switching from bash and it's always stuff bash does natively or with a one line alias.

As many others here have noted, terse syntax for things like `repeat` loops or normal `for`/`while` loops or shell var expansion can all be addictive, but some specific things I miss in Bash that are easy in Zsh are: 1) auto cd to any evar just from its name e.g. lb=/usr/local/bin; lb ($lb works in bash, but I always forget the $. I know I could do aliases...Yuck.) 2) auto cd does not seem to use $CDPATH this is a re…

For your #8 a function might be helpful to remove the $[] characters.

     $ function c;{echo $[$*]}
     $ c 1234./56
     22.035714285714285
     $ a=33;c a / 3
     11
Post reply on HN