I use vim to develop with Python and it works fine. However I want to improve the behavior for documenting my code. For instance I wrote this in vimrc :

:iabbrev """ """ """<Esc>3hi

It works fine but I would like to differentiate the behavior depending on which key is used after """, either <Space> or <CR>. The result should be like that :

  • """ blabla should give """ blabla """

  • """<CR>blabla should give :


For the first form, the command :iabbrev """<Space> """ """<Esc>3hi give E474: Invalid argument. As written in help I escaped <Space> with CTRL+V but even if there is no error there is no behavior.

For the second one, the command :iabbrev """<CR> """<ESC>yyPA is accepted but nothing happens when I enter """<CR> else the normal behavior.


1 Answer 1


You'll probably have less troubles using mappings than abbreviations. Also using the non recursive version of the abbreviations commands (noreabbrev) and mappings (noremap) is usually a better idea unless you know why you are using the recursive version.

If you go with the mapping commands you'll see that when you start typing """ the characters will not be inserted in the buffer immediately, I think that is an acceptable trade off in this case.

For your first case you could go this way:

inoremap """<Space> """ """<Esc>F i

The left hand side of the mapping is """<Space> you don't need to escape anything as <Space> is a keycode recognized by inoremap. On the right hand side we use F to go back to the last inserted space.

For your second case you could go this way:

inoremap """<CR> """<Esc>YpO

Again no need to escape <CR> and I reduce yy to Y, p pastes the line under the current one and O inserts a new line over the pasted one and start insert mode.

Now these commands should work but to do what you want, maybe yous should start exploring the different snippet plugins which exist (this question can be a starting place) they will give you more control over what you are doing.

  • 1
    Still feels weird to see no diamond ...
    – guntbert
    Commented Feb 16, 2021 at 20:31
  • 1
    @gruntbert It's a good opportunity to see new diamonds, it will be good for the community :)
    – statox
    Commented Feb 17, 2021 at 15:00
  • @gruntbert Thanks for helping. It is exactly what I wanted to do. On the same way I added autocompletion for parenthesis and simple quotes. Thanks a lot.
    – Nimu Nobdy
    Commented Feb 19, 2021 at 8:22

Your Answer

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

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