1

I want Vim to remember a defined Python variable when I run it again. Is there a way?

Example:

[n] : line n on vim.

[1] a = 2

[2] b = a + 1

[3] print(b)

If I write:

:1,2w !python

and then:

:3w !python

I'd like to get the answer:

3

Instead, I'm getting:

Traceback (most recent call last):
  File "< stdin >", line 1, in < module >
NameError: name 'b' is not defined

shell returned 1
3
  • 2
    What is the xy problem? Commented Nov 25, 2019 at 11:49
  • thanks, I will study more about getting help on stackoverflow. Commented Nov 25, 2019 at 11:59
  • 2
    Strictly speaking, Vim does remember the variable - all the source code is still loaded in the buffer; you just ask Vim to pass only parts of the buffer to Python. You rather want the Python interpreter to remember program state, something which is at odds with the :! command, which launches a fresh instance of the executable each time. Commented Nov 25, 2019 at 12:36

1 Answer 1

1

When you run !python you run an external Python binary. Every time you run !python you run a new non-initialized Python that doesn't remember anything from your previous session.

To remember your calculations you need to use the internal, built-in Python. Use :python or :python3.

Also see "Execute Python from within current file".

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

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.