Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

I have a VIM function in .vimrc that is called by a few abbreviations. I would like to move those abbreviations to PHP-specific, C-specific, and a few other file-type specific files, but they no longer see the functions in .vimrc as in scope. How can I increase the scope of functions in .vimrc?

EDIT: I've tried this:

 - bruno:~$ cat .vimrc
set nocompatible
filetype plugin on
function! Hello()
   return "hello"

 - bruno:~$ cat .vim/after/ftplugin/php.vim
abbr xyz <C-R>=Hello<CR>

However, I only get E121: Undefined variable: Hello when typing "xyz" in a .php file. I added the prefix g: to both the function declaration and to the function call, but that did not help either. Nor did it help to add the prefix only to the function declaration. This is on VIM 7.3, Kubuntu 12.10.

share|improve this question
Did you abuse s: by any chance? –  romainl May 27 '13 at 18:52
Thanks Romainl, in fact I did not abuse an s:! I did try with and without g: prefixes but that did not help. –  dotancohen May 28 '13 at 6:29
add comment

1 Answer

up vote 4 down vote accepted

You didn't give an example of a function and an abbreviation that don't work, so it's impossible to tell what you did wrong. Here is an example of a function in my ~/.vimrc,

function! Hello()
   return "hello"

and an abbreviation in ~/.vim/after/ftplugin/php.vim,

abbr xyz <C-R>=Hello()<CR>

that work as expected. Open a new php file,

$ vim foo.php


This is a test of xyx

and upon hitting the space bar, that becomes

This is a test of hello 
share|improve this answer
Thank you. I have started with a blank .vimrc and ftplugin file, and added your text and only set nocompatible and filetype plugin on to .vimrc. When opening somefile.php and typing the text xyz, I get the message E121: Undefined variable: Hello. This is VIM 7.3 on Kubuntu 12.10. –  dotancohen May 28 '13 at 6:22
I see the problem. I'm sorry, my fault. I mistyped the abbreviation and omitted the parentheses from the function call. "Hello<CR>" should have been "Hello()<CR>". I'll fix my answer. –  garyjohn May 28 '13 at 15:09
Actually, I had a whole mess of problems but I was able to use your working example and work backwards, Googling the error messages as I went. I actually started this quest two hours ago, seconds after you posted a working example! Thank you very, very much! –  dotancohen May 28 '13 at 17:26
add comment

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.