
I typed the line, stuff, into the editor.
HOW TO MAKE A PYTHON TEXT EDITOR PORTABLE
# Reopen the file to read the edited dataĬlick is a great library for command line processing and it has some utilities, click.edit() is portable and uses the EDITOR environment variable. # Flush the I/O buffer to make sure the data is written to the file # Write the initial content to the file I/O buffer With tempfile.NamedTemporaryFile(suffix=".tmp", delete=False) as tf: # We want to reopen the file incase the editor uses a swap-file. # NOTE: Don't autodelete the file on close! # Open a temporary file to communicate through (`tempfile` should avoid any filename conflicts) # Set initial input with which to populate the buffer # Get the text editor from the shell, otherwise default to Vim Including passing input to the editor and reading output from the editor. I wrote a small module named callvim.py (betraying my preferred choice of text editor) which creates a temporary file, populates it with some text, opens it with a text editor (defaults to vim), and prints the modified text: #!/usr/bin/env pythonĭemonstrates calling a text-editor (e.g. Get the edited text back for use in the program don't leave anything lying around after the program exits Regain control after the text editor exits FeaturesĬall a text editor from within a program in order to edit a file or buffer emacs, vim, etc.) into command-line Python programs. I'd like to know how integrate the use of text editors (e.g.
