Question summary

Currently I'm writing a pull request for this vim plugin which should set commas and semicolons automatically.
At the moment it looks if it should set a comma/semicolon/double point or not everytime the user presses any key . This makes the plugin a little bit heavy (in my opinion) so I want to call a function only when the user presses the return key and not for every key.

What I have now at this moment

This is currently in the plugin:

autocmd BufEnter * call cosco_autocmds#RefreshAutocmds()

This cosco_autocmd#RefreshAutocmds() enables the auto-setting for the current buffer:

autocmd TextChangedI <buffer> call cosco#AdaptCode()

Now I've tried this:

inoremap <CR> <C-O>:call cosco#AdaptCode()<CR>

But it looks like that the mapping doesn't call the function (you can try to add a echom "Test" in the cosco#AdaptCode() function). Do you have an idea how I can fix this?

Test it on your own

If you want to test it on your own, add my pull request to your plugin manager. For vim-plug it should be:

Plug 'TornaxO7/cosco.vim'

This is the line where I want to put the inoremap. If you need more information, how this plugin works in general, take a look into its CONTRIBUTING.md file.

  • What do you mean by "But it doesn't work"? I do get this error Error detected while processing TextChangedI Autocommands for "<buffer=1>"..function cosco#AdaptCode[41]..cosco_eval#ShouldNotSkip: line 56: E121: Undefined variable: g:cosco_ignore_comment_lines but if I start with :let g:cosco_ignore_comment_lines = 0 then it seems to work for me... I do seem to have the TextChangedI autocmd set, so it seems your fork isn't really removing that, is it?
    – filbranden
    Feb 4, 2021 at 23:08
  • One thing is you need your <CR> mapping to also produce an actual <CR>, so inoremap <CR> <C-O>:call cosco#AdaptCode()<CR><CR> is a start. It doesn't really work very well though, because it only adds the semi-colon for the preceding lines, so autoindent thinks the new line is a continuation line...
    – filbranden
    Feb 4, 2021 at 23:12
  • @filbranden first of all, thank you for the "bug report" I forgot to change the variable name. It should work now. 2. My fork wants to change this autocmd with the mapping. I'll edit my question. Thank you for the hints!
    – TornaxO7
    Feb 4, 2021 at 23:25
  • @filbranden It's my intention to add the semicolon/comma in the previous line, since this gives the opportunity to press the return key once, instead of leaving insertmode, going one line down and entering the insert mode again.
    – TornaxO7
    Feb 4, 2021 at 23:28


Your Answer

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

Browse other questions tagged or ask your own question.