Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python module for editing text in CLI

Is there some python module or commands that would allow me to make my python program enter a CLI text editor, populate the editor with some text, and when it exits, get out the text into some variable?

At the moment I have users enter stuff in using raw_input(), but I would like something a bit more powerful than that, and have it displayed on the CLI.

like image 355
Andrew Avatar asked Dec 22 '22 11:12

Andrew


1 Answers

Well, you can launch the user's $EDITOR with subprocess, editing a temporary file:

import tempfile
import subprocess
import os

t = tempfile.NamedTemporaryFile(delete=False)
try:
    editor = os.environ['EDITOR']
except KeyError:
    editor = 'nano'
subprocess.call([editor, t.name])
like image 162
nosklo Avatar answered Dec 25 '22 00:12

nosklo