TextMate + iTerm = Love for everyone

TextMate has been my text editor of choice for a while. Even if its interface is as simple as it can be (it does not even have a toolbar!) it is very powerful behind the scene, in part due to numerous “bundles”. One of them brings love, communication and share between TextMate and iTerm, a terminal application with tabs, bookmarks and many other nifty features. The iTerm bundle is quite sparse in itself but it has a little gem in it, called Paste Selection. The command grabs the text currently selected in TextMate, sends it -through an AppleScript- to the last used iTerm window/tab and paste it at the prompt there.

The beauty of this lies in the fact that one can run many things in a terminal window, from a simple prompt to R or MATLAB or SciLab or Python or… OK you probably get it by now! This commands extends TextMate love to all terminal based programs. And if you are so inclined you could also experiment with behavior of doubtful utility such as sending text from TextMate to vim.

I modified the base command a bit to escape backslashes and suppress the tabs in the text TextMate sends (they triggers auto-completion in terminal programs that support it, which is not always welcome) and to give back the focus to TextMate after it has sent the text (I typically send code one line at a time and it reduces significantly the number of back-to-TextMate-clicks or ⌘⇥). The code for the modified command is this one (with ¬ being a line continuation):

PASTE=$(echo "$TM_SELECTED_TEXT" | sed 's/\\/\\\\/g' | ¬
        sed 's/\"/\\\"/g'  | sed 's/    //g')
osascript << END
tell application "iTerm"
        activate
        tell current terminal
                tell current session
                        write text "$PASTE"
                end tell
        end tell
end tell
tell application "TextMate"
        activate
end tell
END

which I usually associate with the key equivalent ⌘↩ in the general scope “source”. It overrides a potentially useful built-in shortcut which allows you to insert an switch to a new line from any cursor position in the current one. It would probably be wiser to use ⌥↩ instead… but ⌘↩ is R.app’s default and I got used to it ;).

‘hope you’ll find this useful!

EDIT: There is even a better way to use TextMate’s power. Set Input to “Selected Text or Line” and use this command:

PASTE="$(cat | sed 's/\\/\\\\/g' | sed 's/\"/\\\"/g'  | sed 's/    //g')"
osascript << END
tell application "iTerm"
        tell current terminal
                tell current session
                        write text "$PASTE"
                end tell
        end tell
end tell
tell application "TextMate"
        activate
end tell
END

This allows you to send current line when nothing is selected. Much faster!

EDIT 2: I added a small screencast showing how I personally use this feature, as requested by Vincent.

1 Responses to “TextMate + iTerm = Love for everyone”


  • R or MATLAB or SciLab or Python or…

    … or Octave? ;-)

    I am a fan of both Texmate and Octave. I was unaware of the ability for Textmat to interact with iTerm. Very cool.

Leave a Reply