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?

3
  • 1
    So, no possibility of using an IDE? Commented Mar 20, 2018 at 15:15
  • See how to preserve cmd line history between sessions: 1, 2. Commented Mar 20, 2018 at 18:12
  • If you want an IDE the simplest solution is to use one. Commented Mar 21, 2018 at 6:49

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

Comments

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.