0

I need to set up a function in zsh that would edit a different file based on some input at the command line.

I want to simplify my aliases so I don't have multiple aliases to do the same thing but with a slight variation. I am specifically trying to set up a function to open in my editor different versions of the php.ini file (for php 5.4, 5.5, 5.6 and 7.0)

Right now, I have defined the following aliases:

alias editphpini54="subl /usr/local/etc/php/5.4/php.ini"
alias editphpini55="subl /usr/local/etc/php/5.5/php.ini"
alias editphpini56="subl /usr/local/etc/php/5.6/php.ini"
alias editphpini70="subl /usr/local/etc/php/7.0/php.ini"

What I would like to do is set up a function called editphpini and then input a variable (version number) and have it intelligently open up the right file. This could then be future-proof as well, as long as the basic path remains the same.

So, what I want to do be able to type in

editphpini 54

And have the function parse that command and load the php.ini file located in /usr/local/etc/php/5.4/php.ini

Using the above example, I would then be able to substitute 55 56 or 70 at the end of the command and that would issue the relevant command.

In my thoughts, the function would take the XX version number, insert it into the command subl /usr/local/etc/php/X.X/php.ini using the XX to define the version number.

Honestly, I have tried multiple things, and nothing seems to work, so rather than just list all my failed attempts, I'd appreciate if someone could point me in the right direction.

Thank you,

Ali

1
function editphpini() {
    local version=$( echo $1 | sed 's/^\(.\)/\1./' )

    subl /usr/local/etc/php/${version}/php.ini
}

usage:

% editphpini 54
  • OK...that seems simple enough...just one question...I want to input the version number without the dot-notation as in 55 but the path has the version with a dot as in 5.5 – Ali Samii May 6 '16 at 12:15
  • I would add that function to your ~/.zshrc now and just get this simple version working first. then - if that works - start looking at parameter expansion to handle the dot. – the_velour_fog May 6 '16 at 12:17
  • That works...as per the usage X.X. Now, how to expand the parameter to exclude the . – Ali Samii May 6 '16 at 12:20
  • I will mark the answer correct (when it lets me in 4 minutes), and will vote it up when the . is gone :) – Ali Samii May 6 '16 at 12:21
  • That's perfect. Thank you very much @the_velour_fog – Ali Samii May 6 '16 at 12:41
3
editphpini() subl /usr/local/etc/php/${1[1]}.${1[2,-1]}/php.ini

${1[1]}.${1[2,-1]} is one of the many ways to transform xyz into x.yz. Other approaches could be: ${1[1]}.${1#?}, or ${1/(#m)?/$MATCH.} or ${(j:.:)${(s::)1}} (for x.y.z)

  • nice, I wondered why you didn't mention ${1:0:1}.${1:1:1} which also seems to work with zsh , I'm guessing because you prefer the array subscript notation? – the_velour_fog May 6 '16 at 12:46
  • @the_velour_fog, yes, that syntax was only added recently for compatibility with ksh93/bash. It clashes with history modifiers and the ${var:-1} syntax so I don't like it much (not to mention the out of character 0-based index). – Stéphane Chazelas May 6 '16 at 12:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.