You can use the following mapping:
inoremap <CR> <Esc>:pyf ~/clang-format.py<CR>o
Which can be explained like this:
The mapping command:
inoremap creates a mapping in insert mode
The left hand part:
<CR> you want to map the 'enter' key and its code is <CR>
The right hand part:
<Esc> exit insert mode
:pyf ~/clang-format.py write the command to execute
<CR> press 'enter' to execute the command
o pressing 'o' in normal mode will add a new line and switch to insert mode
In the comments you asked why it doesn't work in paste mode. It is normal you can't create mapping in paste mode it would defeat the purpose of this mode. See this question on SO.
Finally a note on the efficiency of your workflow: I might be wrong but if I understand correctly you want to reformat all of your file each time you insert a new line. That doesn't seem ideal since Vim as some options to help you format your code as you right it. Now as I don't really know what your command does maybe it is the best solution.
EDIT To answer your comment, if you want to trigger the command only when you exit insert mode when editing a python file you can use autocommands:
autocmd! InsertLeave *.py pyf ~/clang-format.py
See :h :autocmd
.
inoremap <CR> <Esc>:pyf ~/clang-format.py<CR>o
this way pressing enter in insert mode will leave insert mode, call thepyf
command and add a new line et go back to insert mode thanks too
. (Note thenore
in the mapping instruction to avoid recursive mapping.CTRL-O ... <CR>
instead of<Esc>...<CR>o
, which leaves insert mode for just one command. SeeCRTL-O
and notes below.