OK, I might be opening my mouth and removing all doubts about my stupidity here, but I can't think of a solution to this problem, so here goes..
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.
The "TM_FILEPATH" returns: [ /Volumes/WorkDisk/Users/mats/Sites/projectName/index.php ]
*but* I would prefer to have: [ /projectName/index.php ]
in other words remove [ /Volumes/WorkDisk/Users/mats/Sites ] from the TM_FILEPATH or alternatively create a new shell variable with this info only.
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.
Any help would be greatly appreciated.
Kind regards,
Mats
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?
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?
*but* I would prefer to have: [ /projectName/index.php ]
I.e. _including_ the last path of the project location?
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]
But in your case, you'd preferred it to be 'myProject/untitled.txt' ?
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).
Kind regards Allan
[1] before beta 5 I didn't run the project file/directory through stringByStandardizingPath, so e.g. /tmp would be /private/tmp etc.
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
On 16. Oct 2004, at 14:56, Mats Persson wrote:
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
Okay, so basically the TM_PROJECT_DIRECTORY is useless in this context, since the “logical” project directory is not where the actual tmproj file is kept.
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
Ah, yes -- I think that's really the only "general" solution, seeing how there is no way we can extract the “logical” project directory from the existing variables.
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.
You should use TM_NEW_FILE instead :)
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.
It's _really_ a time saver! ;)
Do you have any recommendations about good resources for learning that stuff, books or online ? How did you grasp it all ?
uh... I think I learned it by years and years of reading man files ;)
I'd recommend starting by getting familiar with the GNU textutils, unfortunately not all are available on OS X, and the OS X versions are not always as flexible, but it's a good overview of what you'd probably need 90% of the time: http://www.gnu.org/software/textutils/textutils.html (you can actually get the GNU versions for OS X btw, though I personally haven't).
Additionally there is sed and awk, which are also _very_ helpful for manipulating text.
And for actual shell programming, I guess the bash man file has some info, but maybe someone else can give a better reference? since man files can be rather intimidating at first, especially when they have the length and layout of the bash man file ;)
Also remember that often you can pipe text through a perl or ruby script giving on the command line, which is what I do to expand the variables in the default HTML templates (though let me add, I expect to soon make the "output option" for templates be "insert as snippet", so we can have placeholders, embedded shell commands a.s.o.).
Other than that, it's probably like learning any other programming language, a bit at a time, reading through all the "public" sources which are available (at least when they do something "impressive") and ask questions whenever you have some!
Personally I don't mind unix questions sent to this list, since TextMate is so tied to shell stuff.
Kind regards Allan
On 16 Oct 2004, at 14:30, Allan Odgaard wrote:
And for actual shell programming, I guess the bash man file has some info, but maybe someone else can give a better reference? since man files can be rather intimidating at first, especially when they have the length and layout of the bash man file ;)
Depends on which shell you favour of course ..
For bash, the "Bash Reference Manual" is friendlier, and provides a bit more background, than the 'man' pages do. Exists as HTML or PDF in numerous locations. Latest version is Edition 3.0 (27 July 2004).
As far as tutorials go, the "Advanced Bash-Scripting Guide", by Mendell Cooper, is _very_ comprehensive. You can download the latest SGML/HTML from the author's site here,
http://personal.riverusers.com/~thegrendel/abs-guide-3.0.tar.bz2
or view it online / download it in various other formats, at the LDP, here:
(There are other good bash guides and HowTo's on the LDP page too ..)
Cheers, -- Andre
On Oct 16, 2004, at 14:30, Allan Odgaard wrote:
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
Ah, yes -- I think that's really the only "general" solution, seeing how there is no way we can extract the “logical” project directory from the existing variables.
(With an embarrassed face) Hmm, was a bit too cocky there thinking I understood this all. Which I didn't of course.
Now having spent too much of the weekend trying to decipher the incomprehensible 'english' of the man files, online tutorials etc etc. I am still stuck and confused.
I am trying to create my own shell variable that is embedded in a new file when it's created like this:
Creating a new file "/Users/mats/Sites/projectName/index.php" should embed the variable TM_PROJ_FILEPATH which should be "/projectName/index.php" into the newly created file.
So I am trying this inside a text doc in TM with no success. What's wrong ??
TM_PROJ_FILEPATH=`$TM_FILEPATH | sed "s|^$TM_SITES_DIRECTORY(.*)$|\1|"` echo $TM_PROJ_FILEPATH /bin/sh: line 1: /Volumes/WorkDisk/Users/mats/Sites/projectName/index.php: Permission denied
( I have tried the above and any possible variation I could think of with no success)
The template file command is: [ -f "$TM_NEW_FILE" ] || TM_DATE=`date +%Y-%m-%d` TM_YEAR=`date +%Y` TM_USERNAME=`niutil -readprop / /users/$USER realname` perl -pe 's/${([^}]*)}/$ENV{$1}/g' <index.php >"$TM_NEW_FILE"
and in that command I would like to add the new variable value TM_PROJ_FILEPATH.
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.
You should use TM_NEW_FILE instead :)
Yeah, I told you I was embarrassed. : )
Do you have any recommendations about good resources for learning that stuff, books or online ? How did you grasp it all ?
<SNIP> Personally I don't mind unix questions sent to this list, since TextMate is so tied to shell stuff.
Be careful with what you don' t mind. Someone might take you up on it. : )
Attn: Andre. Thanks for those links and recommendations.
Perhaps I could suggest that you guys who know and understand this stuff could create a ShellScriptCorner on the wiki where you could translate the incomprehensible stuff into examples and ENGLISH for us non-geek's. Because the writers of man files and online UNIX guides sure as hell hasn't.
Apologies, but I *HATE IT* when I can't work something out myself. AND the only reason that I can't do it is that the people who have created the problem assumed that I - or anyone else - knows as much as they do, and therefore see logic when there is none.
Kind regards,
Mats
On 18. Oct 2004, at 15:35, Mats Persson wrote:
So I am trying this inside a text doc in TM with no success. What's wrong ??
TM_PROJ_FILEPATH=`$TM_FILEPATH | sed "s|^$TM_SITES_DIRECTORY(.*)$|\1|"` echo $TM_PROJ_FILEPATH /bin/sh: line 1: /Volumes/WorkDisk/Users/mats/Sites/projectName/index.php: Permission denied
You need to put 'echo' before '$TM_FILEPATH'. So it should be: TM_PROJ_FILEPATH=`echo $TM_FILEPATH | sed "s|^$TM_SITES_DIRECTORY(.*)$|\1|"` echo $TM_PROJ_FILEPATH
Stuff in `...` is executed as a command. $TM_FILEPATH expands to the file path, so doing `$TM_FILEPATH` alone would try to execute the file. Doing `echo $TM_FILEPATH` instead will echo (print) it. Of course we add `echo $TM_FILEPATH | sed ...` to run sed on the result, to replace the path prefix. The | char means take output from the command on the left side and use as input for the command on the right side.
Not sure how much shell stuff you know!?!
Perhaps I could suggest that you guys who know and understand this stuff could create a ShellScriptCorner on the wiki where you could translate the incomprehensible stuff into examples and ENGLISH for us non-geek's. Because the writers of man files and online UNIX guides sure as hell hasn't.
It's a good idea, though personally I'm probably better at answering concrete questions than writing general tutorials/documentation.
Kind regards Allan
On Oct 18, 2004, at 5:47 PM, Allan Odgaard wrote:
Perhaps I could suggest that you guys who know and understand this stuff could create a ShellScriptCorner on the wiki where you could translate the incomprehensible stuff into examples and ENGLISH for us non-geek's. Because the writers of man files and online UNIX guides sure as hell hasn't.
It's a good idea, though personally I'm probably better at answering concrete questions than writing general tutorials/documentation.
On Oct 18, 2004, at 5:47 PM, Allan Odgaard wrote:
Perhaps I could suggest that you guys who know and understand this stuff could create a ShellScriptCorner on the wiki where you could translate the incomprehensible stuff into examples and ENGLISH for us non-geek's. Because the writers of man files and online UNIX guides sure as hell hasn't.
It's a good idea, though personally I'm probably better at answering concrete questions than writing general tutorials/documentation.
My recommendation would be to buy a copy of the Ruby Pickaxe http://www.pragmaticprogrammer.com/titles/ruby/ and write all of your shell scripts that will be executed inline in TextMate thusly:
ruby <<END #...place ruby code here... puts "$TM_FILEPATH" # for example END
This might be easier if one could directly specify the interpreter to be used (like /usr/bin/ruby instead of sh) for a given script/scriptlet, in the manner of Xcode's shell script build phases.
Chris
This might be easier if one could directly specify the interpreter to be used (like /usr/bin/ruby instead of sh) for a given script/scriptlet, in the manner of Xcode's shell script build phases.
Made a *quick* mock up on this, the small textarea, is insufficient for some commands, so how about using the editor itself to write it! Question is then, Should the script be stored in a standalone file, or the plist like today.
hp.
On Oct 19, 2004, at 01:47, Allan Odgaard wrote in reply to Mats' numerous questions:
Not sure how much shell stuff you know!?!
Hmm, well not enough it seems. : ) Have written a number of conditional shell scripts that create back-ups of MySQL DBs; httpd.conf files and so on which in my world is pretty advanced. My problem has more to do with understanding the logic behind it all. Sort of why do I do this rather than that ??? Never been good at grammar, so I am slow in understanding the underlying structure of the shell world, and I end up being confused.
Hence I did not realise that I just needed the 'echo' in there, which is obvious to me now. Thanks for taking the time to point that out for me. Both you & Sune. Much appreciated.
Perhaps I could suggest that you guys who know and understand this stuff could create a ShellScriptCorner on the wiki where you could translate the incomprehensible stuff into examples and ENGLISH for us non-geek's. Because the writers of man files and online UNIX guides sure as hell hasn't.
It's a good idea, though personally I'm probably better at answering concrete questions than writing general tutorials/documentation.
I created such a page on the wiki, and will try to get the above problem up there as an example in the next few days.
Kind regards,
Mats
On 18. okt 2004, at 15:35, Mats Persson wrote:
So I am trying this inside a text doc in TM with no success. What's wrong ??
TM_PROJ_FILEPATH=`$TM_FILEPATH | sed "s|^$TM_SITES_DIRECTORY(.*)$|\1|"` echo $TM_PROJ_FILEPATH /bin/sh: line 1: /Volumes/WorkDisk/Users/mats/Sites/projectName/index.php: Permission denied
You want: TM_PROJ_FILEPATH=`echo $TM_FILEPATH | sed "s|^$TM_SITES_DIRECTORY(.*)$|\1|"` Instead I think.
Forgot this bit of info: I'm using 1.0.1b4.
Just in case it matters or you would wonder.
Kind regards,
Mats
On 15 Oct 2004, at 19:53, Mats Persson wrote:
*but* I would prefer to have: [ /projectName/index.php ]
in other words remove [ /Volumes/WorkDisk/Users/mats/Sites ] from the TM_FILEPATH or alternatively create a new shell variable with this info only.
This any good? :
echo -n \; basename `dirname $TM_FILEPATH`\`basename $TM_FILEPATH`
-- Andre