0

Been through several guides to set up some simple Vim abbreviations. I'm almost there, I just can't seem to find how to end IN insert mode.

What I have:

iab cl console.log()<Esc>F%s<c-o>:call getchar()<CR><Left><Left>

What happens:

cl expands to console.log() and the cursor is placed over )

What I want to happen:

Same thing except I want to enter insert mode.

What I've tried:

After <Left> I've tried:  <Insert>, i, and <Ins> with no luck.
diplosaurus
  • 2,538
  • 5
  • 25
  • 53

2 Answers2

1

The problem is that the abbreviation is encountering an error and stops executing.

This part <Esc>F%s<c-o>:call getchar()<CR> doesn't make sense to me and looks like it would be causing the error. Multiple parts of it could be the problem. But most likely the F% because it can't find a percent sign going backwards.

I think the fixed mapping should be something along the lines of this (however I don't think this does what you want) It does end in insert mode since the abbreviation completed successfully and the abbreviation doesn't do anything to exit insert mode.

iab cl console.log()<Left>

Take a look at this to https://stackoverflow.com/a/11865489/1890567 remove the trailing whitespace. from the abbreviation.

Community
  • 1
  • 1
FDinoff
  • 30,689
  • 5
  • 75
  • 96
  • `F%s:call getchar()` removes the previous space and is taken from: http://vim.wikia.com/wiki/Use_abbreviations_for_frequently-used_words. As for what you posted, while it does end in insert mode it puts a space before the cursor. – diplosaurus Jun 01 '14 at 04:24
  • Your link provided the answer to the problem, thank you! – diplosaurus Jun 01 '14 at 04:32
  • @diplosaurus yeah that part was taken out of context. It doesn't work unless you put a `%` in the abbreviation. – FDinoff Jun 01 '14 at 06:36
0

The final solution (using info from the thread FDinoff provided) was:

func Eatchar(pat)
  let c = nr2char(getchar(0))
  return (c =~ a:pat) ? '' : c
endfunc

iabbr cl console.log();<Left><Left><C-R>=Eatchar('\s')<CR>
diplosaurus
  • 2,538
  • 5
  • 25
  • 53