The current mapping of my F5 key is:
imap :w\\|!python %
Now I want that if I\'m editing any python
There are problems with both given answer and original mapping. First of all, for buffer-local mappings there is *map
. Second, with
you don’t need to use BufEnter
events and can instead use Filetype
which are launched only once. Third, you have one error (2.), one potential problem (1.) and one place that can be optimized in original mappings:
imap
, it makes it very easy to accidentally break old mappings when adding new ones!python %
will break once file contain a special symbol (space, semicolon, quot, dollar, …):update
instead of :write
avoids useless writes in some casesMy variant:
autocmd Filetype c,cpp inoremap :updateexecute '!make '.shellescape(expand('%:r'), 1)
autocmd Filetype python inoremap :updateexecute '!python '.shellescape(@%, 1)
autocmd Filetype java inoremap :updateexecute '!javac '.shellescape(@%, 1)