How do I invoke a text editor from the command line like git?
Some commands git
, git commit
for example, invoke a command line-based text editor (for example, vim
or nano
or other), pre-filled with some values, and after being saved by the user and there is something to do with the saved file.
How do I go about adding this functionality to a similar Python command line program on Linux?
Please don't hesitate to give an answer, if it doesn't use Python, I'll be satisfied with a general abstract answer, or an answer as code in another language.
source to share
The decision will depend on which editor you have, which environment variable the editor can find, and if the editor accepts any command line options.
This is a simple solution that works on Windows without any environment variables or command line arguments to the editor. Modify as needed.
import subprocess
import os.path
def start_editor(editor,file_name):
if not os.path.isfile(file_name): # If file doesn't exist, create it
with open(file_name,'w'):
pass
command_line=editor+' '+file_name # Add any desired command line args
p = subprocess.Popen(command_line)
p.wait()
file_name='test.txt' # Probably known from elsewhere
editor='notepad.exe' # Read from environment variable if desired
start_editor(editor,file_name)
with open(file_name,'r') as f: # Do something with the file, just an example here
for line in f:
print line
source to share