1

I am writing python 3 code from within vim.

There is a function within my file like this:

def seconds_to_text(seconds):
    return str(datetime.timedelta(seconds=seconds))

Is there a way for me to see what it outputs for certain input values from within vim?

How can I see what seconds_to_text(30) would output without leaving vim?


My plan would be to use the ShiftV selection mode, grab the relevant lines, and pass them into python through the command line.

However, this has several issues: doesn't import necessary modules, won't capture a function return value unless it is also printed, and probably some other things I haven't yet spotted.

8
  • 2
    :w | !python -c "import pyfilename; print pyfilename.seconds_to_text(30)" Commented Dec 25, 2016 at 9:01
  • Downvoted because...? Commented Dec 25, 2016 at 9:01
  • I think most questions get downvoted when the OP doesn't show what they have tried to solve the issue. Commented Dec 25, 2016 at 9:32
  • @Gribouillis well, some 50k rep user posted an answer saying "it's impossible." In the comments of that, I posted what I already did do, which was very tedious but worked, and then they deleted their answer, and probably downvoted this because salt. Commented Dec 25, 2016 at 9:35
  • I'll upvote to counterweight the downvote, because I think it's a good question, although I don't use vim. How can so many people still use vim ? Commented Dec 25, 2016 at 9:44

2 Answers 2

2

How about using Python with the interactive inspection?

:!python -i %

After the entire script was run, you are left in interactive inspection mode where you can then test your functions as you wish.

If you have ipython installed you can of course also use ipython. ipython has the advantage that it saves a history of commands and you can use the arrow keys to scroll through them:

:!ipython -i %

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

1 Comment

Nice! This is a really good answer, I think I'll work on creating a function to do this for me, with all the test cases I want.
1
  1. Open vim
  2. set your cursor to visual (v) and highlight the lines of code you want to translate. -- in this case:

    import datetime
    def seconds_to_text(seconds):
        return str(datetime.timedelta(seconds=seconds))
    
    print(seconds_to_text(30))
    

without print nothing will be returned

  1. While the lines are highlighted, execute the following command: '<,'>!python

Note that those lines will change to: "0:00:30"; and that u undoes your last change(s).

Other options, I would suggest is to look at pycharm, and this stackoverflow question

Comments

Your Answer

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