Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding a command to Vim

Tags:

vim

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?

like image 253
Chinmay Kanchi Avatar asked Jan 04 '10 18:01

Chinmay Kanchi


People also ask

How do you enter a terminal command in Vim?

You can run the shell commands from inside of Vim by just using :! before the command, : means you have to be in command mode. Just after being in command mode, the ! or bang operator will execute the command typed after it from the terminal(Linux/ macOS) or your default shell(Windows -> CMD/Powershell).

What does ctrl F do in Vim?

Ctrl+f will search within the file ctrl+shift+f will search in all the files in the folder tree.


2 Answers

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.

like image 35
Raoul Supercopter Avatar answered Sep 24 '22 01:09

Raoul Supercopter


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> 
like image 121
karoberts Avatar answered Sep 24 '22 01:09

karoberts