0

I am using Manjaro/Arch Linux and I use vim from sudo pacman -S vim instead of build it from source

I want to run my .py files in vim by F5 but when I added these to ~/.vimrc:

map <F5> :call CompileRunGcc()<CR>
    func! CompileRunGcc()
        exec "w"
if &filetype == 'c'
            exec "!g++ % -o %<"
            exec "!time ./%<"
elseif &filetype == 'cpp'
            exec "!g++ % -o %<"
            exec "!time ./%<"
elseif &filetype == 'java'
            exec "!javac %"
            exec "!time java %<"
elseif &filetype == 'sh'
            :!time bash %
elseif &filetype == 'python'
            exec "!time python %"
elseif &filetype == 'html'
            exec "!firefox % &"
elseif &filetype == 'go'
    "        exec "!go build %<"
            exec "!time go run %"
elseif &filetype == 'mkd'
            exec "!~/.vim/markdown.pl % > %.html &"
            exec "!firefox %.html &"
endif
    endfunc

it will show (originally in Chinese)

function CompileRunGcc --> error
In line 1:
E382: cannot write in,has been set as 'buftype'
when running .py files

I am using anaconda as my default python environment, and I installed them at ~/.anaconda

  • 1
    Welcome to [vi.se]! I don’t think it’s related to your issue, but you don’t need all the exec’s; those are generally used when you need to build commands from strings. So you can simply use write, !g++ % -o %<, etc. And you may be interested in :help :compiler and :help :make. – D. Ben Knoble Oct 17 '20 at 13:37
  • As for your question, the error indicates that vim cannot write the file: can you give an example filename where this error happens? Have you checked permissions in that directory/for that file? – D. Ben Knoble Oct 17 '20 at 13:38
  • just a .py file – Firestar-Reimu Oct 17 '20 at 17:33

2 Answers2

0

Now this work but it will throw EOF Error when meeting input()

map <F5> :call RunPython()<CR>
function! RunPython()
    exec "w"
    if &filetype == 'python'
            if search("@profile")
                    exec "AsyncRun kernprof -l -v %"
                    exec "copen"
                    exec "wincmd p"
             elseif search("set_trace()")
                     exec "!python3 %"
             else
                    exec "AsyncRun -raw python3 %"
                    exec "copen"
                    exec "wincmd p"
            endif
    endif
endfunc
0
filetype plugin on
map <F5> :call RunPython()<CR>
function! RunPython()
    exec "w"
    if &filetype == 'python'
        if search("@profile")
            exec "AsyncRun kernprof -l -v %"
        elseif search("set_trace()")
            exec "!~/.anaconda/bin/python %"
        else
            exec "AsyncRun -mode=term -pos=bottom ~/.anaconda/bin/python %"
        endif
    endif
endfunc

Now it works, Thanks to @D.Ben Knoble