Question

I would like to map a certain key mapping to make the Python interpreter run the current edited buffer in VIM.

What I'm doing now (manually):

:!python <FILENAME>

But I'd like the variable to actually be a variable, so that a mapping could be in my .vimrc and work for any file I open in VIM.

I'd also like to know how to chain several commands, so that instead of doing:

:w
:!python <FILENAME>

I could map both of them to a single command. Something like

:w ; :!python <FILENAME>

EDIT: Thanks for the answers. The final solution was to do:

:cmap <KEY> :w<CR>:!python %<CR>
Was it helpful?

Solution

% in vim refers to the current filename.

You can do a mapping like nnoremap <leader>r :!./%<CR> to execute the current script if the shebang line is set.

OTHER TIPS

The percent sign (%) will be replaced by the current filename

For the key macro:

 map yourkey :w^V^J!python %^V^J

where ^V is Ctrl-V and ^J is Ctrl-J (linefeed)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top