Live data from Hacker News

Zsh Tricks to Blow Your Mind

twilio.com

151–160 of 218 posts

Re: Zsh Tricks to Blow Your Mind

#151

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

It's a feature of the zsh line editor called "push-line". http://zsh.sourceforge.net/Doc/Release/Zsh-Line-Editor.html

Re: Zsh Tricks to Blow Your Mind

#152
post #124

Earlier quoted context omitted.

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

Not sure why you are getting all zeros in your duration column. Should be setopt ExtendedHistory (Zsh setopts, like Nim identifiers, are "style insensitive").

From the Zsh man page:

EXTENDED_HISTORY Save each command's beginning timestamp (in seconds since the epoch) and the duration (in seconds) to the history file. The format of this prefixed data is: `: :;'.

(EDIT: My best guess explanation is some competing/conflicting setopt, but the Zsh mailing list could almost surely help you. Also, `history -fD` may show the correct answer even if the saved file has all 0s.)

Re: Zsh Tricks to Blow Your Mind

#153

My favourite feature of zsh (it might be oh-my-zsh), is the tab expansion of unambiguous abbreviated paths. Say if you do: $ cd /u/b/l That will expand into /usr/bin/local. If there are ambiguities at any point, it will expand as much as it can and let you fix it at the point where a decision needs to be made (pressing tab again will show you the options, like Bash et al). For example: $ /u/b/gr $ /usr/bin/gr grep gr…

As far as I know, that's not oh-my-zsh, since I can do that and I don't have omz installed.

Re: Zsh Tricks to Blow Your Mind

#154
post #152

Earlier quoted context omitted.

> 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

Not sure why you are getting all zeros in your duration column. Should be setopt ExtendedHistory (Zsh setopts, like Nim identifiers, are "style insensitive"). From the Zsh man page: EXTENDED_HISTORY Save each command's beginning timestamp (in seconds since the epoch) and the duration (in seconds) to the history file. The format of this prefixed data is: `: : ; '. (EDIT: My best guess explanation is some competing/con…

Thank you! How strange. `history -fD` does work, but only for commands in the current zsh session. It seems durations are being stored in memory but written out as 0's. I'll try the mailing list soon :)

Re: Zsh Tricks to Blow Your Mind

#155

None of these blew my mind. If you like terminal productivity I recommend: fzf, Facebook path picker (aka fpp), fd, ripgrep, lf, tig. Some honorable mentions: tokei, hyperfine, lazydocker, ctop, ncspot.

I'm getting pretty into fasd as well. It's basically a drop-in replacement for z, but it's got extra goodies that merge in some of fzf as well.

Re: Zsh Tricks to Blow Your Mind

#156
post #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 ;)

I have a shell function `.` that goes up one:

    .() { if [ $# -eq 0 ]; then cd ..; else source $\*; fi; }
then

    alias ..='cd ../..'
    alias ...='cd ../../..'
    .
    .
So that "however many periods" goes up however many directory levels. Seems a coherent shorthand to me, though I guess uuuu also works.

Re: Zsh Tricks to Blow Your Mind

#157
post #151

Earlier quoted context omitted.

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.

It's a feature of the zsh line editor called "push-line". http://zsh.sourceforge.net/Doc/Release/Zsh-Line-Editor.html

Thanks very much! Adding:

    bindkey '^q' push-line
works perfectly. Day improved!

Re: Zsh Tricks to Blow Your Mind

#158
post #152

Earlier quoted context omitted.

Not sure why you are getting all zeros in your duration column. Should be setopt ExtendedHistory (Zsh setopts, like Nim identifiers, are "style insensitive"). From the Zsh man page: EXTENDED_HISTORY Save each command's beginning timestamp (in seconds since the epoch) and the duration (in seconds) to the history file. The format of this prefixed data is: `: : ; '. (EDIT: My best guess explanation is some competing/con…

Thank you! How strange. `history -fD` does work, but only for commands in the current zsh session. It seems durations are being stored in memory but written out as 0's. I'll try the mailing list soon :)

Looks like you probably also have SHARE_HISTORY set? [1]

[1] https://unix.stackexchange.com/questions/396809/zsh-share-hi...

Re: Zsh Tricks to Blow Your Mind

#159
post #11

A lot of these can be easily done in Bash, too! 1. `mkcdir () { mkdir -- "$1" && cd -- "$1"; }` 2a. `bind '"\e[A":history-search-backward'; bind '"\e[B":history-search-forward'` 2b. Default behavior for Bash. 3. `shopt -s autocd` 4. `mmv`, `perl-rename` 5. `qalc` from libqalculate: https://github.com/Qalculate/libqalculate 8. Default behavior for Bash. 9. Default behavior for Bash.

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.

#7 is one of my favorite zsh features though, even though it doesn't seem like that big of a deal if you haven't used it.

Re: Zsh Tricks to Blow Your Mind

#160
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.
Post reply on HN