2

I want to be able to run a script to indent the line of code I have just written each time I press enter key in insert mode.

More concretely,currently I use following key binding:

map <C-K> :pyf ~/clang-format.py<cr>

So the sequence of events I have to make happen is:

  • write a line
  • come out of insert mode
  • auto format (with Ctrl+K)
  • go back to insert mode
  • press enter key and start editing next line

What I wish to achieve is:

  • write a line
  • press enter key in insert mode (and automatically trigger pyf ~/clang-format.py)
  • and get the line formatted without any hassle.
3
  • 1
    I dont think this workflow is really efficient but you could try something like this: inoremap <CR> <Esc>:pyf ~/clang-format.py<CR>o this way pressing enter in insert mode will leave insert mode, call the pyf command and add a new line et go back to insert mode thanks to o. (Note the nore in the mapping instruction to avoid recursive mapping.
    – statox
    Aug 23, 2016 at 11:34
  • In addition to what @statox said, you can use CTRL-O ... <CR> instead of <Esc>...<CR>o, which leaves insert mode for just one command. See CRTL-O and notes below.
    – Steve
    Aug 23, 2016 at 13:55
  • Thanks @statox, above worked just like I was expecting it to. Btw, I am curious to understand your thinking behind not considering it an efficient workflow. Also, surprisingly (for me) it does not work in 'insert (paste)' mode. Any idea about that? Aug 24, 2016 at 9:01

1 Answer 1

4

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.

2
  • Actually this command only auto formats the current line, not the whole file. Also, what if I wanted to trigger this each time I come out of insert mode? Is that possible? Aug 24, 2016 at 9:29
  • @SaurabhKumar see my edit :-)
    – statox
    Aug 24, 2016 at 9:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.