zsh hacking thread!!!

Forum rules
Share your brain ;)
User avatar
rhowaldt
Dog
Posts: 4565
Joined: Wed Oct 17, 2012 9:01 am
Contact:

Re: zsh hacking thread!!!

Unread post by rhowaldt » Wed Nov 27, 2013 12:26 pm

^ hmm, that's smart!
All statements are true in some sense, false in some sense, meaningless in some sense, true and false in some sense, true and meaningless in some sense, false and meaningless in some sense, and true and false and meaningless in some sense.

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

Re: zsh hacking thread!!!

Unread post by GekkoP » Wed Nov 27, 2013 12:57 pm

Pretty cool.

User avatar
dkeg
Configurator
Posts: 3782
Joined: Sun Nov 18, 2012 9:23 pm
Location: Mid-Atlantic Grill

Re: zsh hacking thread!!!

Unread post by dkeg » Wed Nov 27, 2013 1:35 pm

@joe, nice! like the the colored ball idea for stage status

Work hard; Complain less

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 » Sun Dec 01, 2013 5:16 pm

@dkeg: I saw that used elsewhere and thought that it was a really interesting way to get as much info as possible without wasting space.
|>>BBQ Roaster, Alpha Branch<< | >> clinky << | >> X11 must die << |
Thanks BASIC

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

Re: zsh hacking thread!!!

Unread post by machinebacon » Mon Dec 02, 2013 2:35 am

I wonder how much it slows down the console - because I noticed that zsh generally is a good amount slower in processing information than other shells.
..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 » Mon Dec 02, 2013 6:18 am

I can't tell a noticeable difference between a stock zsh and a highly modified one. I'm sure that there would be one, but the opening lag doesn't seem to increase (insofar as I can tell without timing it.) Zsh does take longer to load up than the other shells, certainly.
|>>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 » Wed Dec 11, 2013 5:32 pm

Put this in your .zshrc to get DEL key working as you might expect.

Code: Select all

function zle-line-init () { echoti smkx }
function zle-line-finish () { echoti rmkx }
zle -N zle-line-init
zle -N zle-line-finish
Works fine with st.
Actually, not mine. Taken from suckless, but still pretty useful. :)

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

Re: zsh hacking thread!!!

Unread post by GekkoP » Fri Jan 17, 2014 4:57 pm

Not a hack, but still a little fix that anyone who uses zsh with emacs mode could find useful.
When in tmux, C-a prefix conflicts with emacs mode shortcut for quickly going at the beginning of the line (a trick I'm used to in Emacs already).
Basically I did this in my .tmux.conf:

Code: Select all

set -g prefix C-o
unbind C-b
bind-key C-o send-prefix
It didn't take long the get used to this new setup and now zsh, emacs mode and tmux are a happy family.

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 Jan 18, 2014 4:06 am

^ works in other shells too :)
..gnutella..

User avatar
wuxmedia
Grasshopper
Posts: 6445
Joined: Wed Oct 17, 2012 11:32 am
Location: Back in Blighty
Contact:

Re: zsh hacking thread!!!

Unread post by wuxmedia » Sat Jan 18, 2014 8:26 pm

I found a similar thing, in bash + tmux, but through idiotic bashing of the keys, double ctrl a (hit 'ctrl a' then 'ctrl a' again) would bring me to the start of the line...
"Seek, and Ye shall find"
"Github | Chooons | Site"

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

Re: zsh hacking thread!!!

Unread post by machinebacon » Sun Jan 19, 2014 3:27 am

^ That's actually pretty cool.
..gnutella..

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

Re: zsh hacking thread!!!

Unread post by GekkoP » Wed Feb 19, 2014 8:36 pm

Since I needed a way to check my laptop battery status in JWM without using Conky or something similar, I added this to my .zshrc:

Code: Select all

precmd() {
    local BATT="$(~/bin/batt.sh)"
    RPROMPT="%{$fg_bold[green]%}Battery:%f ${BATT}%f%{$reset_color%}"
}
batt.sh is just an acpi wrapper that only gets the info I need, but real beauty is precmd that helps me keeping that battery status updated on my prompt.
Probably common knowledge, but I'm too happy with JWM to mess it up with Conky. :)

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 Feb 20, 2014 4:43 am

Nice! Maybe it's possible to make a kind of embedded status in the JWM bar -- much like xload is doing now. But this would rather belong to the .jwmrc config section :D
..gnutella..

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

Re: zsh hacking thread!!!

Unread post by GekkoP » Thu Feb 20, 2014 12:12 pm

Yes, I was trying to do that yesterday but then I moved to zsh and the solution came quicker. Of course, if anyone can work it out on the JWM bar please do let me know. :)

User avatar
wuxmedia
Grasshopper
Posts: 6445
Joined: Wed Oct 17, 2012 11:32 am
Location: Back in Blighty
Contact:

Re: zsh hacking thread!!!

Unread post by wuxmedia » Thu Feb 20, 2014 12:44 pm

wuxmedia wrote:I found a similar thing, in bash + tmux, but through idiotic bashing of the keys, double ctrl a (hit 'ctrl a' then 'ctrl a' again) would bring me to the start of the line...
Doesn't work with 'screen' though (installed on the work gateway) just tries to get through to nested sessions.
"Seek, and Ye shall find"
"Github | Chooons | Site"

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 May 26, 2014 4:55 pm

I really liked grml prompt colors. Especially the ones for user and root. Since I dropped grml zsh config (I was using only a tiny part of it) and I got my own zshrc with only the things I need, I use this to get the colors I want:

Code: Select all

# prompt colors
color="blue"
if [ "$USER" = "root" ]; then
    color="red"
fi;
prompt="%{$fg_bold[$color]%}%n%{$reset_color%}@%{$fg[white]%}%m%{$reset_color%}%u %B%~%b "
You may have already seen it in my scrots, but here's an example anyway.

Image

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

Re: zsh hacking thread!!!

Unread post by GekkoP » Wed Jun 04, 2014 5:42 pm

Another nice trick with zsh: multiple redirection.

Code: Select all

echo "test1" >file1
echo "test2" >file2
echo "test3" >>file1 >>file2 >file3
cat file[1-3]
test1
test3
test2
test3
test3

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

Re: zsh hacking thread!!!

Unread post by GekkoP » Thu Aug 14, 2014 2:02 pm

Fine-tuning moving/renaming files with zmv.

Add this to your .zshrc:

Code: Select all

autoload -Uz zmv
Source .zshrc and enjoy a better moving/renaming. For instance:

Code: Select all

manuel@bebop ~/tmp ls
test_1    test_2    test_3

manuel@bebop ~/tmp zmv 'test_(*)' '$1'
manuel@bebop ~/tmp ls
1    2    3

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

Re: zsh hacking thread!!!

Unread post by GekkoP » Fri Aug 14, 2015 2:47 pm

Adding a completion mechanism to zsh:

Code: Select all

# load function-based completion system
autoload -Uz compinit && compinit
zstyle ':completion:*' verbose yes
Image


Adding a pager (like less/more):

Code: Select all

# use a pager to scroll long ouput
zmodload zsh/complist
zstyle ':completion:*:default' list-prompt '%S%M matches%s'
# use 'q' to exit the pager
bindkey -M listscroll q send-break
Image


A custom chown with autocompletion:

Code: Select all

_chown() {
    local ret=1
    local -a suf

    if (( CURRENT == 2)); then
        if compset -P '*[.:]'; then
            _groups && ret=0
        else
            compset -S '[.:]*' || suf=( -S . )
            _users "$suf[@]" && ret=0
        fi
    else
        _files && ret=0
    fi

    return ret
}
compdef _chown chown
Image


This magic comes straight from this highly recommended book:

Image

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

Re: zsh hacking thread!!!

Unread post by GekkoP » Sun Aug 16, 2015 9:21 am

Handy functions to cd back up to the highest level of your git repository dir

Code: Select all

function cds {
    ORIGINAL_PWD=`pwd`
    while [ ! -d ".git" -a `pwd` != "/" ]
    do
        cd ..
    done
    if [ ! -d ".git" ]
    then
        cd $ORIGINAL_PWD
    fi
}

Post Reply