Top-rated recipes tagged "editing"http://code.activestate.com/recipes/tags/editing/top/2014-09-01T18:26:51-07:00ActiveState Code RecipesCall out to an external editor (Python)
2014-09-01T18:26:51-07:00Steven D'Apranohttp://code.activestate.com/recipes/users/4172944/http://code.activestate.com/recipes/578926-call-out-to-an-external-editor/
<p style="color: grey">
Python
recipe 578926
by <a href="/recipes/users/4172944/">Steven D'Aprano</a>
(<a href="/recipes/tags/editing/">editing</a>, <a href="/recipes/tags/editor/">editor</a>, <a href="/recipes/tags/external/">external</a>, <a href="/recipes/tags/text/">text</a>).
Revision 2.
</p>
<p>Here's a function that lets you use Python to wrap calls to an external editor. The editor can be an command line editor, like venerable old "ed", or something more powerful like nano, vim or emacs, and even GUI editors. After the editor quits, the text you typed in the editor is returned by the function.</p>
<p>A simple example, using the (rather cryptic) 'ed' editor on Linux. For the benefit of those unfamiliar with 'ed', I have annotated the editor session with comments.</p>
<pre class="prettyprint"><code>>>> status, text = edit('ed')
0 ## ed prints the initial number of lines
a ## start "append" mode
Hello World!
Goodbye now
. ## stop appending
w ## write the file to disk
25 ## ed prints the number of bytes written
q ## quit ed and return to Python
>>> status
0
>>> print text
Hello World!
Goodbye now
</code></pre>