Skip to content

Instantly share code, notes, and snippets.

@nrjones8
Last active August 29, 2015 13:56
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 nrjones8/8835815 to your computer and use it in GitHub Desktop.
Save nrjones8/8835815 to your computer and use it in GitHub Desktop.
Simple Sublime Text 2 plugin to sync local files with remote ones via SCP. A simple workaround for lack of a free SFTP library that could be extended/made more robust.
import sublime, sublime_plugin
import sys
# Sublime has its own embedded Python interpreter -- we need to tell it explicitly
# to find installed 3rd party libraries (like paramiko)
PYTHON_PACKAGES_PATH = '/Library/Python/2.7/site-packages'
sys.path.append(PYTHON_PACKAGES_PATH)
from paramiko import SSHClient
from scp import SCPClient
HOST = 'remotehost.com'
USERNAME = 'yourusername'
SRC_PATH_START = 'local_path_of_files_you_want_to_upload'
DEST_PATH_START = 'remote_path_of_files_you_want_to_upload'
ssh = SSHClient()
ssh.load_system_host_keys()
ssh.connect(HOST, username=USERNAME, look_for_keys=False)
# SCPCLient takes a paramiko transport as its only argument
scp = SCPClient(ssh.get_transport())
class UploadCommand(sublime_plugin.EventListener):
def on_post_save(self, view):
'''
Upload saved file to destination after formatting path correctly
'''
src = view.file_name()
# Put a different condition here to only upload under certain conditions
if True:
dest = self.format_path_for_remote(src)
scp.put(src, dest)
print 'Put %s to\n %s' % (src, dest)
else:
print 'Not trying to SCP %s' % src
def format_path_for_remote(self, full_path):
return full_path.replace(SRC_PATH_START, DEST_PATH_START)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment