Live data from Hacker News

Zsh Tricks to Blow Your Mind

twilio.com

141–150 of 218 posts

Re: Zsh Tricks to Blow Your Mind

#141
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?

Re: Zsh Tricks to Blow Your Mind

#144

> Ctrl-q "parks" the command What is the zsh functionality behind this? I couldn't test it because my terminal eats Ctrl-q and a search for "zsh ctrl-q" didn't turn up anything useful.

Yes please -- this sounds great, I used to use `ctrl-u` then `ctrl-y` with bash, and that doesn't work with zsh, alas. I tried searching for `zsh "park" command` and got nothing useful.

Re: Zsh Tricks to Blow Your Mind

#145
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/…

  echo $filename:r
is the shorter, Zsh way to strip a file extension.

What am I missing from Ksh93?

Re: Zsh Tricks to Blow Your Mind

#146

Been using zsh for years and many of these were new to me. I love #3 and use it daily. I find it most helpful to use this trick to type ".." or "..." and quickly navigate back a dir or two. Of course you can do this in bash (and other shells) with aliases, it does not come out of the box like so many niceties in zsh.

I have a favorite alias `u` (stands for "up") to go up a directory. 50% keystroke savings ;)

Re: Zsh Tricks to Blow Your Mind

#147
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…

> Zsh's extended history format that includes both the time started as well as duration of all my commands.

Woah, how do I enable this? From what I can tell this information is not being stored in $HISTFILE:

  ...
  : 1613659860:0;sleep 1
  : 1613659863:0;sleep 3
  : 1613659871:0;exec zsh
  : 1613659880:0;vim $HISTFILE

Re: Zsh Tricks to Blow Your Mind

#148
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 old way to remove a file extension is using basename:

   basename -s .png example.png
   example

Re: Zsh Tricks to Blow Your Mind

#149
post #37

Earlier quoted context omitted.

Bash supports completions as well.

IIRC, bash doesn't support extended completion editor features of ZSH, which GP alluded to - like generating a table of options with help strings that can be selected by arrow keys and the like.

A (slightly contrived) example:

  > rsync --e«TAB»
  Completing file
  some-file-with-extra-hyphens
  Completing host
  a-n-example.local
  Completing option
  --cvs-exclude                      -- auto-ignore files the same way CVS does
  --delete-excluded                  -- also delete excluded files on the receiving side
  --exclude                          -- exclude files matching pattern
  --exclude-from                     -- read exclude patterns from specified file
  --executability                    -- preserve executability
  --ignore-existing                  -- ignore files that already exist on receiving side
  --ignore-non-existing  --existing  -- ignore files that do not exist on receiving side
There are various ways to present the completion; I have the simple "keep typing letters" way, but Zsh can also present a table with an entry to highlight.

Re: Zsh Tricks to Blow Your Mind

#150
post #53

Earlier quoted context omitted.

If the dizzying assortment of shell parameter expansion features (see man zshexpn), isn't enough to convince you, then the cause is lost.

A "dizzying assortment of features" is not automatically a good thing. The features need to be able to do useful things better somehow, and that's where articles demoing such applications come in. "Better" also doesn't necessarily mean "more concise," otherwise APL would be one of the best programming languages. A language with a smaller number of accessible, comprehensible, memorable features can prove more useful i…

I've used Zsh for 15 years or more, and when I started I read the manual. (I realize this is unthinkable nowadays.)

I noted down the things I thought would be useful day-to-day, and started using them. A few months later, I skimmed through the manual again. I still do so, every year or two. My work gradually changes, and I find Zsh has features that are now useful to me.

This is the suggested manpage: https://linux.die.net/man/1/zshexpn

Post reply on HN