4

I have set F2 prompt key with map <f2> :w<cr>:! D:\Python34\python %<cr>,when i open an python file in vim and press F2,the python file will be executed .For a simple example, here is my python file and opened in gvim .

enter image description here enter image description here

Now i can't input other python lines ,only thing i can do is to see the result and hit any key to close this window. What i want is : when i press F2, (the python file was opened in gvim) ,the python console pop up,and all the files in the python file were copied into the python console automatically,and i can go no to input some lines such as Obj().hello in the python console or go on to edit in gvim ,i am a lazy man ,the gvim and python console all opened waiting to serve me , can i write a vim scripts to achieve the target? The command :!D:\Python34\python -i % works fine ,i got the ouput enter image description here There is still a problem remain, 1)when command :!D:\Python34\python -i % works ,the gvim window will be frozen , i can't drag my mouse to see codes in vim. 2)there is no any python codes in the python console wiondow

So if the program is full of many lines ,and i can't remember the previous content ,worse still, the gvim window frozen ,how can i get the codes? enter image description here

6
  • Try: :!D:\Python34\python -i % Commented Aug 22, 2014 at 2:14
  • a problem remain.1)when command :!D:\Python34\python -i % works ,the gvim window will be frozen , i can't drag my mouse to see codes in vim. 2)there is no any python codes in the python console wiondow.So if the program is full of many lines ,and i can't remember the previous content ,worse still, the gvim window frozen ,how can i get the codes? Commented Aug 22, 2014 at 2:32
  • Does adding a & to the end of the command work? (launch it as a background process?) Commented Aug 22, 2014 at 3:02
  • The gvim is frozen too,i can not drag mouse in the gvim to watch my codes. Commented Aug 22, 2014 at 5:12
  • @FDinoff, I don't think & works in Windows. Commented Aug 30, 2014 at 3:24

3 Answers 3

1

Avoid blocking

To make the call asynchonous (to avoid that GVIM is blocked during the Python session), use the Windows-specific :!start command:

nnoremap <f2> :w<cr>:!start D:\Python34\python -i %<cr>

List teh codez

I don't know whether it is possible to list the passed source code from the interactive Python debugger. But you can print the file contents before starting it:

nnoremap <f2> :w<cr>:!start cmd /c type % && D:\Python34\python -i %<cr>

Additional tips

  • You should use :noremap; it makes the mapping immune to remapping and recursion.
  • As your mapping only works correctly from normal mode, use :nnoremap (or extend it to support visual-mode selections, too).
Sign up to request clarification or add additional context in comments.

Comments

0

Maybe Vim plugin Conque will solve your problem:

  1. Installation instrucions are here https://code.google.com/p/conque/
  2. To use just type :ConqueTermVSplit python -i test.py (VSplit is for vertical split - you may use horizontal)
  3. There is no blocking of your window with python code - you may escape interactive mode and switch to your window with Ctrl+W twice

Comments

0

You could approach the problem from the Python angle (2.7).

  1. Keep the file where it is (or save it with some unique name to a temporary directory) and have python load the file directly.
  2. Go to that location in your shell and run python interactively (or have vim spin off an interpreter for you)
  3. Import your file import demo
  4. Experiment with what you have implemented demo.SomeModule().meth()
  5. Make some changes in vim
  6. Reload your python module reload(demo)
  7. Experiment with your code again demo.SomeModule().differentMeth()

You can also have vim create a file with shortcut functions for loading/reloading the file you are working on. When vim kicks off the interpreter, you can have it set this file to the PYTHONSTARTUP environment variable, which is a file the interpreter will automatically load when it starts up. For example, you could have a function called r() to automatically reload the file you are working on.

It's also worth mentioning that reloading modules can be a little weird. If you instantiate some modules then reload the file, only new modules will use the new code; the old modules will run with the old code.

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.