Skip to content

Instantly share code, notes, and snippets.

@jlegewie
Created April 9, 2012 18:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jlegewie/2345210 to your computer and use it in GitHub Desktop.
Save jlegewie/2345210 to your computer and use it in GitHub Desktop.
Send selection from Sublime Text 2 to iTerm
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