Put some colour in your life!


This one-liner is a pretty harmless and easy one, yet very very useful (as you might be seeing in one of my next entries).

for i ({000..255}){[ $((i%20)) = 0 ] && printf "\n";printf "\x1b[38;5;${i}m${i} "}

This will actually just work in the Z-Shell, there’s a slightly longer version you’d have to use if you don’t use the Z-Shel (you masochist! 😉 ).

i=0;while [ $i -le 255 ];do [ $((i%20)) = 0 ] && printf "\n";printf "\x1b[38;5;${i}m${i} ";i=$(($i+1));done

What does it do? Well I’ll show you a more comfortable version of it:

i=0;
while [ $i -le 255 ]
do
    if [ $((i%20)) = 0 ]
    then
        printf "\n"
    fi
    printf "\x1b[38;5;${i}m${i} "
    i=$(($i+1));
done

If you ever worked with colors in the shell, you might right away see it: It’s the CSI codes for colors defined by the ANSI escape sequences. So the script goes through all possible 255 color codes that are available on your systems shell and prints the corresponding number in its color. By making use of every shells lazy evaluation the if <cond>; then; <expr>; fi becomes the neat and compact <cond> && <expr>. Making use of Z-Shells highly sophisticated brace expansion our i=0; while <$i-cond>; do; <expr>; <$i-expr>; done becomes for i in <brace>; do <expr>; done. The brace expression is then evaluated to a list from 000, 001,… to 099 and then further on to 255, so it is as if all those numbers were actually written out as list (which is why we can use for in this case). Using the short form of for-loops in zsh it then becomes for i (<brace>){<expr>}. Neat and sweet, wouldn’t you say?

Now you could even make it more compact, if you don’t care about how it is broken into lines (that’s why there’s the modulo part printing an printf “\n” after each 20 numbers (thus with space 4 chars * 20 = 80 chars per line), you could remove it and end up with printing one huge line, or even one huge column:

for i ({000..255}){ printf "\x1b[38;5;${i}m${i}\n"}

Why bother? Well let me lose some short thoughts on that:

Continue reading

Advertisements

Omit Second Word in Filename


Ever had a bunch of files, where you wanted to omit the second word in the file name?

Now, I believe there’s a much smaller way using zmv and it’s regular expression powers (and if there is and you know, please feel free to add it in the comments).

I for my part grew up with the bash and therefore feel much more comfortable with using sed and awk, though I haven’t used it in a while. But somehow – at least to my memory – I never needed to use substr, so this one was new to me. Here’s how I’ve done it:

for f in *; do; t=$(awk '{print $1 " " substr($0, index($0,$3))}' <<< $f); mv "$f" "$t"; done

My initial idea, which I found much more appealing was to just override the second variable:

for f in *; do; t=$(awk '{$2=""; print}'; mv "$f" "$t"; done

But that still treats $2 as a word, and therefore, before and after it there’s a space, leaving me with two spaces. Using substr() however, the space is removed, which is why i had to manually insert it.

Now if you want a more readable script file read on.

Continue reading