1

This is part of my vimrc--

function! Abbreviations()
    let a:code=input("Which Abbreviation: ")

    "gen
    if(a:code=="gen")
    source ~/.vim/Abbreviations/GeneralAbbreviations.vim
    endif

    "math
    if(a:code=="math")
    source ~/.vim/Abbreviations/MathAbbreviations.vim
    endif
endfunction


function! SourceAbbreviations()
    :execute ":normal! :source $MYVIMRC\<cr>:call Abbreviations()\<cr>gen\<cr>:call Abbreviations()\<cr>math\<cr>"
endfunction

nnoremap <buffer> <localleader>e :call SourceAbbreviations()<cr>

As you can see the function SourceAbbreviations() has to call other functions. When I do :call SourceAbbreviations() it shows an error. However, if I remove :source $MYVIMRC\<cr> from the function SourceAbbreviations(), it works. So, what's actually wrong in this case?

2

I would advise you to do the following:

First change the function Abbreviations() to accept an argument and maybe not use the interactive mode:

function! Abbreviations(code)
  if a:code != 'gen' && a:code != 'math'
    let l:code = input("Which Abbreviation: ")
  else
    let l:code = a:code
  endif
  if l:code == "gen"
    source ~/.vim/Abbreviations/GeneralAbbreviations.vim
  endif
  if l:code == "math"
    source ~/.vim/Abbreviations/MathAbbreviations.vim
  endif
endfunction

Now you can :call Abbreviations("gen") or :call Abbreviations("math") or :call Abbreviations("") to get the old behaviour.

Second simplify the function SourceAbbreviations() like so

function! SourceAbbreviations()
  execute "source" $MYVIMRC
  call Abbreviations("gen")
  call Abbreviations("math")
endfunction

But if you are really keen on using your complex :execute command you should take $MYVIMRC out of the string as this is vimscript and not shell. So write

:execute ":normal! :source " . $MYVIMRC . "\<cr>:call Abbreviations()\<cr>gen\<cr>:call Abbreviations()\<cr>math\<cr>"

In order to understand this compare these two

echo $MYVIMRC
echo "$MYVIMRC"
  • Hey, Thanks for helping me out. I made all the changes you suggested, still it showed an error; and it referred to the line endfunction in the function SourceAbbreviations(). I think the problem is that if I put source $MYVIMRC in this function, it creates a loop. I don't know for sure though. – Mahbub Alam Jul 5 '18 at 18:38
  • Please post the error message/number, that is always important. Do you call the function from your vimrc file? Why do you need to source the vimrc file from within the function? Maybe you can turn the function into an autoload function (see :h autoload). – Lucas Jul 6 '18 at 6:14
  • I did try adding autocmds for this, and it wasn't working; but today when I did it it worked! I don't know why it didn't work earlier, I did not change anything else that could affect it. Anyway thanks. – Mahbub Alam Jul 8 '18 at 10:46

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.