sandbox = Sandbox.create()
# Install vim if needed
sandbox.commands.run("apt-get update && apt-get install -y vim", user="root")
# Create a file to edit
sandbox.files.write("/home/user/test.py", "print('Hello')")
# Open vim in PTY
handle = sandbox.pty.start(
"vim /home/user/test.py",
cols=120,
rows=40,
on_data=lambda data: print(data.decode(), end="")
)
# Send vim commands
handle.send_input("i") # Insert mode
handle.send_input("# Modified by PTY\n")
handle.send_input("\x1b") # Escape
handle.send_input(":wq\n") # Save and quit
handle.wait()