42

I finally decided to try out Vim, as I am getting increasingly frustrated by GUI editors. So far, I'm loving it, but I can't find any help for a issue I'm having...

I am trying to map the command :Pyrun to :!python % in Vim using cmap. The mapping shows up fine if I type :cmap. However, on typing :Pyrun, I get this error message:

Not an editor command: Pyrun.

Here is what I'm trying in .vimrc:

:autocmd FileType python :cmap Pyrun<cr> !python %<cr>
:autocmd FileType python :cmap Intpyrun<cr> !python -i %<cr>

What can I do to solve this problem?

1

4 Answers 4

45

I would try something like this in your .vimrc or your ftplugin/python_ft.vim

command Pyrun execute "!python %"
command Intpyrun execute "!python -i %"

Then :Pyrun and :Intpyrun should work

You could then map a function key to each

map <F5> :Pyrun<CR>
map <F6> :Intpyrun<CR>
Sign up to request clarification or add additional context in comments.

1 Comment

to add the buffer-local command, use :command -buffer...:command -b does not work
30

I personally prefer another approach. First create a function receiving the command arguments and then create a command to call the function:

fun! DoSomething( arg ) "{{{
    echo a:arg
    " Do something with your arg here
endfunction "}}}

command! -nargs=* Meh call DoSomething( '<args>' )

So it would be like

fun! Pyrun( arg ) "{{{
    execute '!python ' . expand( '%' )
endfunction "}}}

command! -nargs=* Pyrun call Pyrun( '<args>' )

But, there's a better way to do it in Vim. Use makeprg:

makeprg=python\ %

Just type :make to run your current Python file. Use :copen to show error list.

2 Comments

Sounds like a more scalable approach. I will probably do something like this if I need more than one command executed.
Is there a benefit to defining the function Pyrun and the command Pyrun to take arguments when these are not actually used? I'd expect either to pass arg along to the python script, or have none and use -nargs=0 (which is also the default) in the command! line...
12

G'day,

Similar to karoberts answer, I prefer the more direct:

:map <F9> :!python %<CR>

If my script is creating some output I also like to capture it in a temp file and then autoread that files content into another buffer, e.g.

:map <F9> :!python % 2>&1 \| tee /tmp/results

I then set autoread by entering :set autoread and opening the results file in another buffer

:split /tmp/results<CR>

Then I can easily see the results of the run in the buffer that auto refreshes when the results file is updated by running the script under development.

HTH

cheers,

Comments

0

With new LUA api:

vim.api.nvim_create_user_command('Hello', 'echo "Hello World!"', {})
vim.api.nvim_create_user_command('HelloLua', function ()
    print('Hello LUA!')
end, {})

NeoVIM API reference

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.