Share a Zsh tip weekly!

Self-explanatory title really. I’ll start with a simple but useful one:

ls -l *(Lk-3) | ls *(.m0) 

finds all files in current directory that are less than 3Kb (Lk-3) in size and have been modified today (m0). Good for quickly spotting changes in config and settings files.

PS: For it to work, extended globbing must be turned on (setopt extended_glob)

1 Like

new week, new tip:

grep -iw '@pid' **/*.{erb,rb}~(lib|vendor|test)/*

will search for occurrence of a variable (in this case) in template and Ruby files, excluding certain directories. But this isn’t the coolest part. The coolest part is that, if we keep using the same search pattern regularly we can create a function-style parameterized alias for it, like so:

finder() {grep -iw "$*" **/*.{js,rb}~(lib|vendor|test)/*}

So now we can simply type

finder @pid

I have many such appropriately named aliases that I apply for different patterns of searches in my codebase, e.g. html_find, tests_only, style_find, ruby_wholeword_nocase, etc,

1 Like

I’m not a zsh user. But it’s a command line and you can do command line things here so I recommend following @climagic on Twitter for awesome command line magic :smile:

1 Like

@climagic is general cli advice, mainly for the bash shell. This thread is for the ZShell specifically. Bonus points for tips that facilitate Ruby development. :wink:

Awe… you bashed my bash tip. You know it still works on zsh

what bash tip? it was just a reference to a twitter account :stuck_out_tongue_winking_eye:

This week’s tip especially for @danielpclark :smile:

In Zsh you can edit an environment variable inline:

2 Likes

Isn’t vared just an inline editor program? Also I think your path descriptor is too verbose. The world is only 80 characters wide afterall :wink:

2 Likes

well, you can have a vared equivalent in bash. All you have to do is run this function:

function vared { [[ $# -eq 1 ]] || { echo "usage: vared varname" >&2 ; return 1 ; }
                              tmpfile=$( command mktemp -t vared.********** ) echo "${!1}" > "$tmpfile" 
                             command vim "$tmpfile"   && export $1="$(< "$tmpfile" )" command rm "$tmpfile"}

Simple.

Not. :042:

Oh, and my shell theme is gorgeous, thank you for noticing :smile: