简体   繁体   中英

VIM: Referring to current buffer in key mappings

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>

% 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.

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM