0

OK this may be pretty basic question but I just learned how to make custom key mappings.

I am trying to make vim automatically "end" two lines lower when typing "class" so I wrote following in .vimrc :

:imap class class<CR><CR>end

So that worked fine except now when I start typing "class" vim is over typing each letter of "class" until I finish typing it or what I'm typing no longer matches "class".

Is there a way to avoid this behavior, I want to be able to see what I am typing as I'm typing it even when it matches a mapped key sequence?

Not sure if I'm explaining it correctly.

Thanks for help

1

You're looking for

:inoreab class class<cr><cr>end

The same (nore) would have worked with mappings as well, but mappings are less fitted for such snippets.

BTW, the abbreviation should be declared specific to the filetype where you will use it (Ruby ?) ->

" in ~/.vim/ftplugin/ruby/my_snippets.vim
:inoreab <buffer> class class<cr><cr>end

Note, that you won't be able to type "class" from within a string or a comment without seeing it expanded. You'll either have to rely on functions that'll take care of the context, or snippets (which will be much easier to maintain)

For instance, with lh-bracket, you could define:

:Inoreab <buffer> class <c-r>=InsertSeq('class', 'class !cursorhere!\<cr\>!mark!\<cr\>end!mark!')<cr>

But I really recommend you to use a snippet engine instead (mu-template, utilsnip, snipmate, ... There are plenty)

2
  • I'm not infront of the right coputer but once I will be I'll take a look at the plugins you mentioned. I know I have heard of them before it just never came to my mind that they woudl be solution to my problem. – sgp667 May 27 '15 at 17:16
  • SnipMate basically solves this and more problems for me. thanks – sgp667 May 28 '15 at 13:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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