[TxMt] Re: How to extract part of a line of code and make it a variable?

Jérémy Lecour jeremy.lecour at gmail.com
Thu Aug 30 15:52:44 UTC 2012

Thank you Alan and Hans for your answer.

> On Aug 30, 2012, at 11:11 AM, Hans-Jörg Bibiko <bibiko at eva.mpg.de> wrote:
>> select: "something a_method(with, arguments)"
>> and invoke that snippet
>> ${1:variable} = `echo -ne "$TM_SELECTED_TEXT" | perl -pe 's/^\s*\w+\s*//'`
>> `echo -ne "$TM_SELECTED_TEXT" | perl -pe 's/^\s*(\w+)\s*.*/\1/'` ${1/\s*//}
> Neat approach but no need to call out to shell / perl:
>    ${1:variable} = ${TM_SELECTED_TEXT/\A\s*\w+\s*//}
>    ${TM_SELECTED_TEXT/\A\s*(\w+).*/$1/} ${1}

It is working with the exact (very simple) example that I've written previously.
But it is not working at all with a slightly more complicated real example like that :

	@articles = @article.category_id.present? ? Article.where('category_id = ?', @article.category_id) : []

I'd like to obtain this :

	variable = Article.where('category_id = ?', @article.category_id)
	@articles = @article.category_id.present? ? variable : []

I guess your solution doesn't work in this example because of the whitespaces, …

In Vim, it seems that it can be done with a prompt of the desired variable name, then replacing the selected text with it, and finally inserting the assignment on a new line above.

Does this seems feasible in TextMate?

> And here’s a tip regarding selection: in 2.0 using a “unit selection”, i.e. the stuff under Edit → Select, using shift + movement will always extend selection, so e.g. with something like:
>    something a_method(with, arguments), something_else
> Place caret inside parenthesis, use ⇧⌘B (select to enclosing pairs) and then one can use ⌥⇧← to extend the selection to the method name (with 1.x unit selections would produce a regular “anchored” selection, so using left/right movement would shrink/expand respectively).

I should have stated that I'm using TextMate Version 1.5.11 (1634)

I'm not a big fan of alpha software for my day to day use ;-)

Thank you so much for your help, it's really appreciated.

Jérémy Lecour
Conception et développement d'applications web
http://jeremy.wordpress.com - http://twitter.com/jlecour

More information about the textmate mailing list