[Python] Open Vim with text in it, without saving the file

Discussão em 'Python' iniciado por Stack, Novembro 24, 2025.

  1. Stack

    Stack Membro Participativo

    I want to write a script which ends by calling my text editor (vim) on a specific file path. The file doesn't yet exist, and I want the editor window to already have some contents in it. In particular, I want that:


    1. If I save the file contents in my text editor :)w in vim), then the contents of the file are saved at the specified path.


    2. If I quit the text editor without saving the contents :)q! in vim), then the contents of the file are not saved.

    If I just do something like:

    with open(filename, "x") as file:
    file.write("something long and complicated")
    subprocess.call(["vim", filename])


    then point (1) works but point (2) doesn't — if I quit vim without saving, the file was still created. Essentially I want it to be like I called vim filename and then added a bunch of stuff in the editor window.

    What's the best way of achieving this?

    Continue reading...

Compartilhe esta Página