Created
April 9, 2012 18:19
-
-
Save jlegewie/2345210 to your computer and use it in GitHub Desktop.
Send selection from Sublime Text 2 to iTerm
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import sublime | |
import sublime_plugin | |
#import os | |
import subprocess | |
import string | |
import re | |
## send selection to iTerm | |
class SendSelectionIterm(sublime_plugin.TextCommand): | |
@staticmethod | |
def cleanString(str): | |
str = string.replace(str, '\\', '\\\\') | |
str = string.replace(str, '"', '\\"') | |
str = string.replace(str, '//', '*') | |
return str | |
def run(self, edit): | |
selection = "" | |
for region in self.view.sel(): | |
selection += self.view.substr(region) + "\n" | |
selection = (selection[::-1].replace('\n'[::-1], '', 1))[::-1] | |
# only proceed if selection is not empty | |
if(selection != ""): | |
# define list of arguments | |
args = ['osascript', '-e', 'tell app "iTerm" to activate'] | |
# split selection into lines | |
selection = self.cleanString(selection).split("\n") | |
# add code lines to list of arguments | |
for part in selection: | |
args.extend(['-e', 'tell app "iTerm" to tell current terminal to tell current session to write text "' + part.strip() + '"\n']) | |
# activate ST2 | |
args.extend(['-e', 'tell app "Sublime Text 2" to activate']) | |
# execute code | |
subprocess.Popen(args) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment