0

I currently use vim and tmux to edit my code. During debugging, I often have to:

  1. Save my changes in vim
  2. Head over to the terminal pane
  3. Run python -i script.py
  4. Do some testing (e.g. print some variables, check out error messages, etc), then head back to my vim pane.

However this becomes cumbersome when after several edits, as every time, I would switch over to the terminal pane, quit the python -i session, and rerun python. Worst of all, I lose all my python history when I restart the session!

I thought of binding something like :!python -i <current file> in my .vimrc, but that would not solve the problem as I can't edit the script while testing it at the same time (as one would do with an IDE, and also the reason I got tmux). Running python -i seems to crash vim anyway.

What should I do?

Wet Feet
  • 4,435
  • 10
  • 28
  • 41

2 Answers2

0

You could add a python code to run the vim editor in such a way that editor automatically execute the python code itself.

import sys, tempfile, os
from subprocess import call

EDITOR = os.environ.get('EDITOR','vim') #that easy!

initial_message = "" # if you want to set up the file somehow

with tempfile.NamedTemporaryFile(suffix=".tmp") as tf:
     tf.write(initial_message)
     tf.flush()
     call([EDITOR, tf.name])

    # do the parsing with `tf` using regular File operations.
    # for instance:
    tf.seek(0)
edited_message = tf.read()

In "initial_message" you could give "the command to run a python code in a editor".
hope this would work fine.

0

This might not be exactly the answer to your question, but I would advise you to use IPython which might simplify your life.

This would resolve your history problem as you can turn logging on via %logstart and then keep your history between sessions.

You could also use the %run command to reload the script.

There is also a plugin linking vim and Ipython: https://github.com/ivanov/vim-ipython but I've never used it, it might be worth a test.

Tic
  • 421
  • 1
  • 5
  • 14