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 &"

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

  • Welcome to Vi and Vim! 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 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 at 13:38
  • just a .py file – 1900011604 Oct 17 at 17:33

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 %"
                    exec "AsyncRun -raw python3 %"
                    exec "copen"
                    exec "wincmd p"
| improve this answer | |
  • Once again, not that it matters, you can write AsyncRun ... and copen directly, no exec/quotes necessary – D. Ben Knoble Oct 17 at 17:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.