1

In ftplugin directory I created pascal.vim with the following mapping:

imap // <ESC>hc2l{  }<ESC>hi

The aim of this mapping is to replace two forward slashes with braces and leave the cursor between them.

It works. But for unknown reason it also deletes two previous characters.

Furthermore, it does not work in columns 1 and 2 - probably because it wants to delete those two chars but it can't.

Can someone explain this mystery to me?

2
  • 2
    If you want mappings only for certain filetypes, you should make sure you use buffer-local mappings. See :h :map-local Apr 3 at 6:25
  • 3
    Another best practice (which both answers neglected) is to use non-recursive mappings unless you explicitly need them. Prefer :inoremap to :imap.
    – Friedrich
    Apr 3 at 9:16

3 Answers 3

4

The Mapping

Shamelessly stealing from Vivian's and Maxim's answers as well as from Christian's and my own comments, the command to put in your ~/.vim/after/ftplugin/pascal.vim file would be either

:inoremap <buffer> // {}<Left>

or

:inoremap <buffer> // {}<C-G>U<Left>

The Mystery

There's no need to remove the slashes because mappings change the meanings of keys. Whenever you type // it's as if you'd typed {}<Left> instead. The slashes never go into the buffer, hence you don't need to delete them.

The Miscellany

The mapping above has the following advantages:

  • does not overrule the defaults for Pascal because it's in after. See :help 'runtimepath'.
  • uses non-recursive mapping to prevent mappings on the right hand side from triggering (some people like to map opening braces to {} or disable arrow keys). See :help :inoremap
  • constrained to the current buffer to be in effect only in Pascal files. See :help map-<buffer>.
  • the version with <C-G>U can be reverted with a single u. See :help i_CTRL-G_U (thanks to Luc Hermitte for the excellent suggestion)
  • clean and minimal mapping without mode switching
5
  • 2
    "There's no need to remove the slashes because mappings change the meanings of keys. Whenever you type // it's as if you'd typed {}<Left> instead" - that's it. For whatever reason I thought that map command meant: when I write this, VIM will do that. But it actually replaces what I have written. :-)
    – Pontiac_CZ
    Apr 8 at 20:55
  • 2
    I would even recommand {}<c-g>U<left> (:h i_CTRL-G_U) Apr 9 at 10:54
  • 1
    @LucHermitte thank you for your suggestion. Edited.
    – Friedrich
    Apr 9 at 12:48
  • 1
    That's a new information for me (have just tested it), that using arrow keys within a line in insert mode starts a new undo block. It really does (there's probably a reason behind that) and <C-g>U prevents it. Learning everyday something new in VIM! :-)
    – Pontiac_CZ
    Apr 9 at 17:29
  • Didn't know about that either but it was handed to me on a silver plate as punishment for stealing from all the other answers. Life is not fair :-D
    – Friedrich
    Apr 9 at 19:03
2

Can someone explain this mystery to me?

You have commands in your mapping that do it

imap // <ESC>hc2l{  }<ESC>hi

When you press // your mapping is in effect and // is not inserted into buffer. Instead mapped commands do the job, where you have c2l which deletes 2 chars.

I would go with

imap // {}<left>

or

imap // {  }<left><left>
2

I would do:

inoremap <buffer> // {  }<C-o>h

You don't need to delete the // if the mapping is on //.

I prefer to use <C-o> over <Esc>hi to avoid switching mode in the mapping.

The <C-o> switch Vim to Normal mode for exactly one keyboard command (in this case h)

More information with: :help i_CTRL-O

It, indeed, moves Vim in a variant of the Normal mode (code niI).

More information with: :help mode()

4
  • Thanks for the feedback :-) I have adapted the solution. Apr 3 at 16:08
  • 1
    This works, but I actually don't understand the <C-o> combination. The -- INSERT -- in status bar changes to -- (insert) --. Another type of an insert mode? :help <C-o> returns something about inserting registers but it's quite confusing.
    – Pontiac_CZ
    Apr 8 at 21:07
  • 1
    Thanks for the feedback. I have enriched the answer to clarify the <C-o> part :-) Apr 9 at 5:17
  • @Friedrich, probably because I'm clumsy :-) Apr 9 at 5:18

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.