Why do I get E127 from this vimscript?

Inside your function, you're loading another buffer (e.g. vim.command("b " + altBufName1)). When that buffer has the same filetype, the current ftplugin script is sourced again as part of the filetype plugin handling, but the original function hasn't returned yet, so you get the E127.

Solution

I recommend putting the function itself into an autoload script, e.g. in ~/.vim/autoload/ft/cppalter.vim:

function! ft#cppalter#CppAlter()
    ...

Your ftplugin script becomes much smaller and efficient, as the function is only sourced once:

nnoremap <leader>vc :call ft#cppalter#CppAlter()<cr>
...

(You should probably use :nnoremap <buffer> here to limit the mapping's scope.)

Alternative

If you don't want to break this up, move the function definition(s) to the bottom and add a guard, like:

nnoremap <leader>vc :...

if exists('*CppAlter')
    finish
endif
function! CppAlter()
    ...

Tags:

Python

Vim