zsh hacking thread!!!

Forum rules
Share your brain ;)
User avatar
DebianJoe
Frame Buffer
Posts: 1915
Joined: Mon Jul 01, 2013 5:41 am
Location: emacs.d

zsh hacking thread!!!

Unread post by DebianJoe » Wed Sep 11, 2013 10:26 am

Why have we not started one of these yet? We have all sorts of threads on how to set up zsh for users/ALL users/global default/etc. We need to share some zsh wizardry.

+++++++++++++++++
I really wish I could remember where in the PATH my foobar file is.

Code: Select all

ls =foobar
....wait a minute, I built that one from source. I should probably find ALL cases of the foobar, in case I left a binary laying around.

Code: Select all

ls /**/foobar
...but what about the sources, headers, etc? I wonder where those are, and also how many of them there are.

Code: Select all

ls /**/foobar*
ls /**/foobar* | wc -l
..please share neat zsh tricks, aliases, hacks, etc.
|>>BBQ Roaster, Alpha Branch<< | >> clinky << | >> X11 must die << |
Thanks BASIC

User avatar
gutterslob
Resident Tranny
Posts: 1124
Joined: Thu Aug 08, 2013 7:13 pm

Re: zsh hacking thread!!!

Unread post by gutterslob » Wed Sep 11, 2013 4:27 pm

Let's start out slow, continuing with the ls and ** juju


See everything changed/modified in the past day.

Code: Select all

ls -ldt **/*(m-1)
Same as above, ignoring directories

Code: Select all

ls -ldt **/*(.m-1)

User avatar
gutterslob
Resident Tranny
Posts: 1124
Joined: Thu Aug 08, 2013 7:13 pm

Re: zsh hacking thread!!!

Unread post by gutterslob » Wed Sep 11, 2013 4:37 pm

Oh wait. Doesn't BBQ use grml's zsh config? I know it did when Bacon first started the project. If that's still the case, anyone using a BBQ release owes it to themselves to read grml's zsh reference card: http://grml.org/zsh/grml-zsh-refcard.pdf

Frankly, just studying all of grml/zshlovers' resources patiently is one of the best ways to learn zsh. I find it a bit sad that so many users (my past self included) wget the grml zshrc and yet only use about 1/100th of it's functionality. There's so much in there that's been painstakingly put together and annotated over the years.

machinebacon
Baconator
Posts: 10253
Joined: Thu Sep 16, 2010 11:03 am
Location: Pfälzerwald
Contact:

Re: zsh hacking thread!!!

Unread post by machinebacon » Wed Sep 11, 2013 5:13 pm

Yes, the Q used grml's zshrc and recently ohmyzsh for a change (for all interested, it is in ~/bin), but we will revert to grml again, it's way more powerful than the 'other' (which actually only shines because of its theme, and nothing else) IMO
..gnutella..

User avatar
DebianJoe
Frame Buffer
Posts: 1915
Joined: Mon Jul 01, 2013 5:41 am
Location: emacs.d

Re: zsh hacking thread!!!

Unread post by DebianJoe » Thu Sep 12, 2013 9:48 am

Not knowing any better when I started, I just did a "touch .zshrc" from home and started reading documentation and adding stuff I figured I'd actually use. I probably wouldn't know what to do with a well-written and thought-out configuration.

Still, grml and the zshlovers stuff is really good reading.
|>>BBQ Roaster, Alpha Branch<< | >> clinky << | >> X11 must die << |
Thanks BASIC

User avatar
gutterslob
Resident Tranny
Posts: 1124
Joined: Thu Aug 08, 2013 7:13 pm

Re: zsh hacking thread!!!

Unread post by gutterslob » Thu Sep 12, 2013 1:16 pm


User avatar
gutterslob
Resident Tranny
Posts: 1124
Joined: Thu Aug 08, 2013 7:13 pm

Re: zsh hacking thread!!!

Unread post by gutterslob » Thu Sep 12, 2013 1:30 pm

Compile and run .c programs
Assumes you have gcc.

In ~/.zshrc (or local/sourced equivalent)

Code: Select all

# crun -- compile and run .c program
crun() {
  local cprog=$1; shift
  local n=$@[(i)--]
  gcc -o ${cprog%.*} $cprog $@[1,n-1] && ./${cprog%.*} $@[n+1,-1]
}
Last edited by gutterslob on Thu Sep 12, 2013 1:34 pm, edited 1 time in total.

User avatar
gutterslob
Resident Tranny
Posts: 1124
Joined: Thu Aug 08, 2013 7:13 pm

Re: zsh hacking thread!!!

Unread post by gutterslob » Thu Sep 12, 2013 1:34 pm

Run zsh function with tracing

In ~/.zshrc (or local/sourced equivalent)

Code: Select all

# tracing 
tracing() {
  local f=$1; shift
  functions -t $f
  $f "$@"
  functions +t $f
}

User avatar
gutterslob
Resident Tranny
Posts: 1124
Joined: Thu Aug 08, 2013 7:13 pm

Re: zsh hacking thread!!!

Unread post by gutterslob » Thu Sep 12, 2013 1:55 pm

I'm often surprised how so few people know this.

Use ls to list more than one type of file.
Lets say you have a directory full of archive files and documents, but only want to list .rar, .zip and .pdf files.

Code: Select all

ls -l *.(rar|zip|pdf)

machinebacon
Baconator
Posts: 10253
Joined: Thu Sep 16, 2010 11:03 am
Location: Pfälzerwald
Contact:

Re: zsh hacking thread!!!

Unread post by machinebacon » Thu Sep 12, 2013 3:07 pm

..gnutella..

User avatar
DebianJoe
Frame Buffer
Posts: 1915
Joined: Mon Jul 01, 2013 5:41 am
Location: emacs.d

Re: zsh hacking thread!!!

Unread post by DebianJoe » Fri Sep 13, 2013 7:53 am

Not exactly zsh specific, but one of my new favorite tricks is writing scripts that use #!/bin/zsh functions, and then write an alias that calls the full path of the script (generally $HOME/scripts/foobar) in my .zshrc.

I'm sure that if you're writing stuff for public consumption, then bash is probably the way to go, but for personal use, it works fantastically without having to move the script into your PATH. This keeps my scripts that I'm working on, and the ones that are functional all in a single directory, and the aliases only point to things that actually have been tested and work.
|>>BBQ Roaster, Alpha Branch<< | >> clinky << | >> X11 must die << |
Thanks BASIC

User avatar
gutterslob
Resident Tranny
Posts: 1124
Joined: Thu Aug 08, 2013 7:13 pm

Re: zsh hacking thread!!!

Unread post by gutterslob » Fri Sep 13, 2013 3:10 pm

I'm not 100% certain whether this will work on a stock zsh and keymap setup, but it does on mine, at least.

You can bind the F1 - F12 keys to commands, or possibly even scripts and whatnot. First you need to find out how the keys are supposed to be represented. I'll use F6 and F7 as examples. Note that the output in yours might differ to mine, depending on stuff like locale, keymap, whatever (I don't know, tbh).
If I do a Ctrl-v and then press F6. I get ^[[17~ returned.
...and if I try Ctrl-v and F7. I see ^[[18~

So now, in my ~/.zshrc (or local/sourced equivalent) I can add this to make the F6 key call ls -l

Code: Select all

# F6 for ls -l
bindkey -s "^[[17~" "ls -l\n" 
...or make the F7 key call a script or alias

Code: Select all

# F7 for ansi-colors script
bindkey -s "^[[18~" "ansi-colors\n"
Obviously some terminal emulators and applications already have their own navigational functions or commands binded to the F keys, so you'll have to be careful with these. For example, some term emulators like xfce4-terminal or terminator let you press F11 to go fullscreen (depending on DE/WM in use) so in this case you'll have to avoid using ^[[23~ as a keybind in zsh.

User avatar
gutterslob
Resident Tranny
Posts: 1124
Joined: Thu Aug 08, 2013 7:13 pm

Re: zsh hacking thread!!!

Unread post by gutterslob » Sat Sep 14, 2013 2:38 am

Oh, totally forgot about this one.
Any FISH (Friendly Interactive Shell) fans here? Such a swell shell. Shame the community never expanded beyond the early contributors.
Now you can add a healthy dose of fish to zsh: https://github.com/zsh-users/fizsh
It's available in the Sid/Unstable repos, btw.

User avatar
gutterslob
Resident Tranny
Posts: 1124
Joined: Thu Aug 08, 2013 7:13 pm

Re: zsh hacking thread!!!

Unread post by gutterslob » Sat Sep 14, 2013 2:50 am

machinebacon wrote:Yes, the Q used grml's zshrc and recently ohmyzsh for a change (for all interested, it is in ~/bin), but we will revert to grml again, it's way more powerful than the 'other' (which actually only shines because of its theme, and nothing else) IMO
I wouldn't be so hard on oh-my-zsh. It does have it's perks, particular portability between distros/platforms. The grml configs tend to be very Debian-centric. Oh-my-Zsh has got some great ruby plugins, and also Homebrew support for OS X users (ZSH-Lovers only does the old MacPorts and Fink). It's also (probably) easier to adapt to for newcomers.

You should look at this, Bacon. I haven't tried it yet, but looks promising.
https://github.com/zsh-users/antigen

machinebacon
Baconator
Posts: 10253
Joined: Thu Sep 16, 2010 11:03 am
Location: Pfälzerwald
Contact:

Re: zsh hacking thread!!!

Unread post by machinebacon » Sat Sep 14, 2013 11:36 am

Thanks for the tip - shame on me that I often switch back to bash. And happy to see FISH mentioned. Reminds me of having supper.
..gnutella..

User avatar
DebianJoe
Frame Buffer
Posts: 1915
Joined: Mon Jul 01, 2013 5:41 am
Location: emacs.d

Re: zsh hacking thread!!!

Unread post by DebianJoe » Sat Sep 14, 2013 2:54 pm

Putting antigen into killx, will report back with my thoughts on it.

Edit:
Image
It's a piece of cake to install and get working. (Curl to file, source, get plugins, source those, add sources to .zshrc). Seems to work flawlessly as of the two plugins that I've added to it. I approve.
|>>BBQ Roaster, Alpha Branch<< | >> clinky << | >> X11 must die << |
Thanks BASIC

User avatar
GekkoP
Emacs Sancho Panza
Posts: 5877
Joined: Tue Sep 03, 2013 7:05 am

Re: zsh hacking thread!!!

Unread post by GekkoP » Mon Sep 23, 2013 3:34 pm

I began my zsh exploration today (thanks to the almighty Elektra, of course). So chsh, switch to zsh and now subscribed to this topic. Keep going, guys, keep going.

User avatar
GekkoP
Emacs Sancho Panza
Posts: 5877
Joined: Tue Sep 03, 2013 7:05 am

Re: zsh hacking thread!!!

Unread post by GekkoP » Tue Sep 24, 2013 3:40 pm

I love this one:

Code: Select all

ls -ld **/*(/^F)
List all the empty directories. Useful when I need some cleaning up.

User avatar
DebianJoe
Frame Buffer
Posts: 1915
Joined: Mon Jul 01, 2013 5:41 am
Location: emacs.d

Re: zsh hacking thread!!!

Unread post by DebianJoe » Wed Oct 30, 2013 11:02 am

Here's one I just added to my .zshrc:

Code: Select all

widthcheck() {
    echo ${(l:79::-:)}
}
When programming for pep8 or simply maintaining good practice, you should never exceed 79 characters. This function slaps 79 dashes into your terminal, so that you can see if something you're doing is longer or shorter than that.

...but I guess you could simply throw out "echo $COLUMNS" if you want a number instead of a ruler.
|>>BBQ Roaster, Alpha Branch<< | >> clinky << | >> X11 must die << |
Thanks BASIC

User avatar
DebianJoe
Frame Buffer
Posts: 1915
Joined: Mon Jul 01, 2013 5:41 am
Location: emacs.d

Re: zsh hacking thread!!!

Unread post by DebianJoe » Wed Nov 27, 2013 10:33 am

Dkeg prompt, with exit code right prompt:

I like the dkeg 2-layered prompt, so I wrote out a zsh prompt that's similar, but that also prints out the status of the last executed command as a code on the right side.

Code: Select all

autoload -U colors && colors
RPROMPT="[%{$fg_no_bold[red]%}%?%{$reset_color%}]"
PROMPT="%{$fg[blue]%}╭─[%T%{$fg[red]%} %D%{$fg[blue]%}]	%{$fg[green]%}%~ \      
%{$fg[blue]%}                                                                   
╰─%B$%b %{$reset_color%}"

Code: Select all

Looks like this:
╭─[17:29 13-11-27] ~ 
╰─$                                                [0]
...and that's cool and all, but I ALSO like to see what branches I'm on when working on git files....so I wrote up a little function to switch to a revision system based prompt when I want it.

Code: Select all

 
git_prompt () {
autoload -Uz vcs_info

zstyle ':vcs_info:*' stagedstr '%F{28}●'
zstyle ':vcs_info:*' unstagedstr '%F{11}●'
zstyle ':vcs_info:*' check-for-changes true
zstyle ':vcs_info:(sv[nk]|bzr):*' branchformat '%b%F{1}:%F{11}%r'
zstyle ':vcs_info:*' enable git svn
precmd () {
    if [[ -z $(git ls-files --other --exclude-standard 2> /dev/null) ]] {
        zstyle ':vcs_info:*' formats ' [%F{green}%b%c%u%F{blue}]'
    } else {
        zstyle ':vcs_info:*' formats ' [%F{green}%b%c%u%F{red}●%F{blue}]'
    }

    vcs_info
}

setopt prompt_subst
RPROMPT='%F{blue}%n@%m %c${vcs_info_msg_0_}%F{blue} %(?/%F{blue}/%F{red})%% %{$reset_color%}'
}
Which is pretty useful. It shows me the branch I'm on and if I've made any modifications to the repo since the last update/pull/push.

SCREENSHOT!!
Attachments
2013-11-27-191930_1366x768_scrot.png
|>>BBQ Roaster, Alpha Branch<< | >> clinky << | >> X11 must die << |
Thanks BASIC

Post Reply