0

I just set up IDE env for Python 3. I was wondering how I can run the file being currently edited in vim. I remembered that the command was ":python %", but it did not work for Python 3.

Thank you very much.

dda
  • 6,030
  • 2
  • 25
  • 34
user1067671
  • 231
  • 1
  • 5
  • 10
  • How exactly did it 'not work'? Was there an error message? It's easier to debug things with full information about what happened. – weronika Jul 02 '12 at 05:37

2 Answers2

3

For Python 3, just simply execute :!python3 %


Furthermore, you might also want to map it to a hotkey in your settings, like what I did:

noremap <D-r> <esc>:w<CR>:!python3 %<CR>

So that you can just press Command+r to execute the current code with Python 3 anytime (it will be saved automatically.

Timothy
  • 4,467
  • 5
  • 28
  • 51
  • There is `nnoremap` for this kind of things: I bet you don’t know whether or not OP has, for example, done `noremap : ;|noremap ; :`. – ZyX Jul 02 '12 at 20:47
  • @ZyX My advice is for the general case. If the OP had done `noremap : ;`, then I would bet that he does know that how to take my advice in right way. – Timothy Jul 03 '12 at 06:15
  • Using `*noremap`, `normal!`, `feedkeys(, "n")` for *anything* you distribute and for *anything* you write in the vimrc unless you do know and do want at least part of the rhs to be remapped *is* a general advice. Yours is not: can you say what is expected to be remapped here? – ZyX Jul 03 '12 at 15:41
  • Point taken. I think you are right. I will update it and thank you:-) – Timothy Jul 03 '12 at 16:53
2

you could use

:!python %

and it will surely work. Now your python should be pointing to python3 in this case, if not just write :!python3 % instead

Hassek
  • 8,715
  • 6
  • 47
  • 59
  • 1
    Unless python points to python2 rather than python3. Also, any reason for using the filename instead of `%`? Shouldn't that work also? – weronika Jul 02 '12 at 05:51
  • when using `:!` in vim you are executing a bash command directly so if your `python` points to `python2` you can use `:!python3 your_file.py`. Also you can do any bash command using the `:!`approach. Just tested `:!python %`and it does work, so I'll edit my answer – Hassek Jul 02 '12 at 14:46