[TextMate] Custom Shell Variables question

Mats Persson mats at imediatec.co.uk
Sat Oct 16 12:56:19 UTC 2004


Thanks for your reply Allan. Very much appreciated. Answers below.


On Oct 15, 2004, at 20:44, Allan Odgaard wrote:
> On 15. Oct 2004, at 20:53, Mats Persson wrote:
>
>> I have a project in the following location:  [  
>> /Users/mats/Sites/projectName/  ]
>> In my template I use Custom Shell variables to auto-enter the 
>> relevant info.
> Template, as in file template?

Yes, as in a file template inside my own .tmbundle in AppSupport/TM/ 
dir. (Basically it's a copy of the "PHP Script" template in the default 
PHP.tmbundle in the TM app folder.


>> The "TM_FILEPATH" returns: [  
>> /Volumes/WorkDisk/Users/mats/Sites/projectName/index.php  ]
> So TM_PROJECT_DIRECTORY/FILEPATH would probably also have the 
> /Volumes/WorkDisk-prefix?

Yes, but TM_PROJECT points to the location where I have stored the TM 
.tmproj file, rather than to where the project - in my mind at least - 
is.


>> *but* I would prefer to have:  [  /projectName/index.php  ]
> I.e. _including_ the last path of the project location?

Yes, IF I understand your question correctly. What I want and assumed 
to be the case is that the top directory in the Project Drawer is the 
TM_PROJECT_DIRECTORY, but it's not really is it ?  Bit confused about 
what refers to what here and there.


>> in other words remove [ /Volumes/WorkDisk/Users/mats/Sites ] from the 
>> TM_FILEPATH  or alternatively create a new shell variable with this 
>> info only.
>
> You probably want to use 'sed' which can do single-line 
> regex-substitutions, e.g. if I enter and select these three lines in 
> TextMate:
>
> echo $TM_FILEPATH
> echo $TM_PROJECT_DIRECTORY
> echo $TM_FILEPATH | sed "s|^$TM_PROJECT_DIRECTORY/\(.*\)\$|\1|"
>
> And press control-r, they return:
>
> /tmp/myProject/untitled.txt
> /tmp/myProject
> untitled.txt
>
> So the first two are just the variables, the third is the TM_FILEPATH 
> but w/o the TM_PROJECT_DIRECTORY-prefix [1]


Using the above outputs the following:

echo $TM_FILEPATH
echo $TM_PROJECT_DIRECTORY
echo $TM_FILEPATH | sed "s|^$TM_PROJECT_DIRECTORY/\(.*\)\$|\1|"
/Volumes/WorkDisk/Users/mats/Sites/projectName/index.php
/Volumes/WorkDisk/Users/mats/TextMateProjects
/Volumes/WorkDisk/Users/mats/Sites/projectName/index.php

echo $TM_PROJECT_DIRECTORY
echo $TM_PROJECT_FILEPATH
/Volumes/WorkDisk/Users/mats/TextMateProjects
/Volumes/WorkDisk/Users/mats/TextMateProjects/projectName.tmproj


> But in your case, you'd preferred it to be 'myProject/untitled.txt' ?

Yes, however with the help of your suggestion and a bit of own brain 
activity - a rare but appreciated event - I created a work around for 
my system that works by using a custom shell variable that contains the 
path to the Sites directory.

echo $TM_SITES_DIRECTORY
/Volumes/WorkDisk/Users/mats/Sites

which then makes the following return exactly what I want

echo $TM_FILEPATH | sed "s|^$TM_SITES_DIRECTORY\(.*\)\$|\1|"
/projectName/index.php


So thanks to your help I have worked out/around the problem. THANK YOU 
!!


IMPORTANT:	Possible BUG ??

In the template I use ${TM_FILEPATH} and when I create a new template 
TM_FILEPATH takes the value of the currently selected Tab in the Tab 
bar. In other words. If I have "/tmp/index.php" as the active file in 
the main editor window, and then creates a new file that I name 
"newfile.php" the TM_FILEPATH output in the new file from the template 
is "/tmp/index.php" instead "/tmp/newfile.php" as it should be in my 
mind.



>> I guess that I would use some form of 'grep' and remove the bits that 
>> I don't want, but I can't work out how.
> sed is probably what you want, but feel free to ask any followup 
> questions (but provide as much context as possible).

I really have to learn all the hidden shell stuff that's available in 
OS X now, as it can do much of great interest.

Do you have any recommendations about good resources for learning that 
stuff, books or online ? How did you grasp it all ?



Kind regards,

Mats




More information about the textmate mailing list