Skip to content

Instantly share code, notes, and snippets.

@sehe
Created December 11, 2012 16:06
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 sehe/4259744 to your computer and use it in GitHub Desktop.
Save sehe/4259744 to your computer and use it in GitHub Desktop.
import sublime
import sublime_plugin
import subprocess
class OpenWithVimCommand(sublime_plugin.WindowCommand):
def run(self):
view = self.window.active_view()
curfile = view.file_name()
cmd = ["C:\\Program Files (x86)\\Vim\\vim73\\gvim.exe", "--servername=GVIM", "--remote-wait-silent"]
# Ex commands don't mix well on vim launch
# Therefore, accumulate a larger sequence of normal mode commands:
normal_commands=""
def to_vim(st2_position):
correction = 0
if ("Unix"!=view.line_endings()):
correction = view.rowcol(st2_position)[0]
return 1 + st2_position + correction
def vbegin(region): return to_vim(region.begin())
def vend(region): return to_vim(region.end())
# comment the following loop if you don't want marks at alternative
# selections
mark = ord('a')
for r in view.sel():
normal_commands += "%igom%c" % (vbegin(r), mark)
mark += 1
if mark > ord('z'): # silently ignore >26 selections
break
# anchor top of vim window at start of ST2 visible region
normal_commands += "%igozt" % vbegin(view.visible_region())
# use the last selection as Visual selection
r = view.sel()[-1]
if (r.begin()!=r.end()):
normal_commands += "%igov%igo" % (vbegin(r), vend(r))
else:
normal_commands += "%igo" % vbegin(r)
cmd.append("+silent! exec \"norm %s\"" % normal_commands)
cmd.append(curfile)
# print cmd
subprocess.Popen(cmd)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment