'Launching vim from python in a new terminal and waiting for it to close
I am trying to create a python script that
- Launches a new terminal window (in my case terminator)
- Open vim on a temporary or designated file
- Waits until vim/terminator is closed
- Assigns the contents of the file to a variable
Here is what I have so far:
import os, subprocess
def print_tmp(i):
with open(str(os.path.dirname(os.path.abspath(__file__))+'/tmp.tex'), 'r') as g:
return print('flag ' + str(i) + ': ' + str(g.read()))
tmpfile=str(os.path.dirname(os.path.abspath(__file__))+'/tmp.tex')
f = open(tmpfile,'w+')
f.write('$$')
f.close()
vimcmd = str("/usr/bin/terminator -g ~/.config/terminator/config -p nord --geometry 60x5+0-0 -T popup-bottom-center -e \'vim " + str(tmpfile) +"\'")
print_tmp(1)
subprocess.Popen(vimcmd,shell=True).wait()
print_tmp(2)
contents=""
with open(tmpfile, 'r') as g:
contents = g.read().strip()
print('contents = '+str(contents))
This opens vim on the file that I wanted just as I need. However, after calling my vimcmd
, the script does not wait like I need it to. The variable contents
stays as '$$'and finishes the script. How do I make python wait for the terminator window to close? By the way, I've already tried subprocess.run()
and subprocess.call()
but neither works. Thanks in advance!
Solution 1:[1]
#!/usr/bin/python3
import os
from pathlib import Path
file = Path('/tmp/myfile.txt')
os.system( 'terminator --command="vim {}"'.format( str(file.absolute()) ) )
myvar = None
with file.open('r') as f:
myvar = f.readlines()
print(myvar)
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Suuuehgi |