Skip to content

Instantly share code, notes, and snippets.

@whitmo
Created April 8, 2010 21:42
Show Gist options
  • Select an option

  • Save whitmo/360579 to your computer and use it in GitHub Desktop.

Select an option

Save whitmo/360579 to your computer and use it in GitHub Desktop.
import tempfile, sys, os
def what_editor():
editor = os.getenv('VISUAL') or os.getenv('EDITOR')
if not editor:
editor = 'vi'
return editor
def get_edited_text(starting_text=''):
temp_fd, temp_fn = tempfile.mkstemp(text=True)
os.write(temp_fd, starting_text)
os.close(temp_fd)
editor = what_editor()
proc = os.spawnlp(os.P_WAIT, editor, editor, temp_fn)
if proc:
raise RuntimeError, "Can't run %s %s" %(editor, temp_fn)
result = open(temp_fn).read()
os.unlink(temp_fn)
return result
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment