[TxMt] Executing command on all files in a project AND using TM variables in a search and replace

Allan Odgaard throw-away-1 at macromates.com
Fri Oct 20 05:42:15 UTC 2006


On 19. Oct 2006, at 22:12, J Fishwick wrote:

> Command:
> eval arr=("$TM_SELECTED_FILES")
> for (( i = 0; i < ${#arr[@]}; i++ )); do
> perl -pe 'while ($string =~ m/mc\d+(.*\n.*True)/g) {$string =~
> s/mc\d+/tf$TM_FILENAME/g;}';
> done
>
> Input: Entire Doc
> Output: Replace selected text

You need to set input/output to none/discard (or show tool tip, or  
maybe show as HTML and make your command output some progress info).

Then your actual command needs to load the file itself, it already  
iterators over the selected files, it just doesn’t tell perl to load/ 
overwrite each of them.

So a simple example would instead be:

     eval arr=("$TM_SELECTED_FILES")
     for f in "${arr[@]}"; do
         perl -i.bak -pe 's/bar/bar/' "$f"
     done
     rescan_project # do this after you make changes to files, so TM  
will rescan

This will replace foo with bar in all selected files (and create  
a .bak file with the old contents).

Two other things: 1) inside single quoted strings, shell variables  
are not expanded, so if you want to use $TM_… then you need to use  
double quotes, and 2) TM_FILENAME is the filename of the file open  
when you called the file, it won’t update inside the loop. Here you  
would instead use the current file given by the loop (in my example $f).






More information about the textmate mailing list