[TxMt] Copy and Duplicate word snippet

Charilaos Skiadas skiadas at hanover.edu
Wed Jan 24 22:12:39 UTC 2007


On Jan 24, 2007, at 12:08 PM, Charilaos Skiadas wrote:

> On Jan 24, 2007, at 11:48 AM, Helmut Granda wrote:
>> and this works great if there is only one line of text but lets  
>> say I need to select a certain word in the middle of a pharragraph  
>> then i get the following
>>
>> before snippet:
>> first word theText second word
>>
>> after snippet:
>> first word theText theText = theText second word
>>
>> as you can tell the snippet inserts the comman din the middle of  
>> the sentence, is there a way to avoid that?
>
> You would need to use a command, that has as input the entire  
> document and output probably insert as snippet. Then you would have  
> to locate the line the caret is at, and then add a line right after  
> it with what you want. You then need to snippet-escape all the  
> regular text, and add a $0 at the correct location where you want  
> the caret to end up.
>
> Sorry, short on time here, but there are ruby libraries that make  
> most of the above very easy (for instance the escaping part). The  
> location can be found via the [dynamic environment variables] 
> [http://macromates.com/textmate/manual/ 
> environment_variables#environment_variables]. There are a number of  
> commands that do similar things in most bundles, certainly in  
> GTDAlt and LaTeX bundles. It should be a short 5-6 line ruby  
> script, if the text manipulation you want to do is simple like  
> above. I'll try to write an example when I get a chance.
>
>> TIA
>> Helmut
>

I spoke too soon here. Actually there is no way to do what I  
described exactly. The gist of it is that you need to set the output  
of the command to "insert as snippet', so as to be able to decide  
where you want the caret to end. However, in order for the snippet to  
actually overwrite the entire text, you must make it so that TM  
thinks that there is a selection, and hence it should replace the  
selection when inserting the snippet. So the input should be  
selection with fallback document.

But in your case you already have the selection there, so the whole  
document will never end up being entered as input.

So I think as Dr. Drang suggested a macro is closer to what you want  
to do. Depending on whether this "theText" part is the same or  
different in each case, you would want to use a snippet or command or  
neither WITHIN your macro. If all you want is to solve the exact  
problem you described in the first message, then this is probably  
what you want (slightly different that Dr. Drang's suggestion):

select the text you want.
Start macro recording (Bundles -> Macros ...)
Copy
use cmd + right arrow to move to end of line
Press Return
Type "theText = "
Paste
Stop macro recording (Bundles -> Macros ...)
Save scratch macro
Test scratch macro

> Haris
>


Haris





More information about the textmate mailing list