Help noob understand vimrc file conditional scripting

271 views
Skip to first unread message

Xtian Simon

unread,
Mar 17, 2012, 4:28:41 PM3/17/12
to vim...@googlegroups.com
This is a basic experiment in vimrc scripting. Its a conditional load of an option based on the existence of a global variable set in a sourced vim config file which is loaded by filetype autocommand.

These are in my .vimrc=
"Adds another vimrc file with other settings...
:autocmd FileType html source $HOME/.vim/html_vimrc.vim
"
"Test if var set in html_vimrc exists...
:if exists("g:prog")
: let g:ok_prog=1
:endif

In .vim/html_vimrc.vim is this line:
:let g:prog=1

This has two test cases. Using :let command to check for the existence of the named g:vars: test.vim, test.html shows that the variable is not available to test.vim. It is available to test.html; however, in the resulting let display, it is listed before, above, the g:prog var. Evidently its not available to the conditional test.

The question is this true? Does the position of the g:var displayed with :let command represent the processing order. And if yes, how can I change this order to and still keep the files separate?

PS. I tried this first my conditional test with :number option.

Taylor Hedberg

unread,
Mar 17, 2012, 5:16:58 PM3/17/12
to vim...@googlegroups.com
Your problem is that .vimrc is sourced first, before the autocommand is
fired. So when the if statement is reached, the variable doesn't exist
yet.

How best to accomplish what you're shooting for depends on what it is
that you want to do. Your message doesn't really make that clear, but if
you provide some more details about your ultimate goal, people can
probably offer suggestions.

Also, just as minor comments on your code:

You don't need leading colons on the commands in a Vim script.

The idiomatic way to source a particular script based on file type is to
save it as an ftplugin. See `:help ftplugin` and, more specifically,
`:help ftplugin-name` for details on the different ways to accomplish
this.

Xtian Simon

unread,
Mar 18, 2012, 11:38:34 AM3/18/12
to vim...@googlegroups.com
On Saturday, March 17, 2012 5:16:58 PM UTC-4, Taylor Hedberg wrote:
> The idiomatic way to source a particular script based on file type is to
> save it as an ftplugin. See `:help ftplugin` and, more specifically,
> `:help ftplugin-name` for details on the different ways to accomplish
> this.

Thanks for that.

Do you know of any documentation about the order of execution in the VIMDOCS [1] or another source?

-----
[1]: http://vimdoc.sourceforge.net/htmldoc/usr_toc.html

Marcin Szamotulski

unread,
Mar 18, 2012, 12:41:40 PM3/18/12
to vim...@googlegroups.com
> --
> You received this message from the "vim_use" maillist.
> Do not top-post! Type your reply below the text you are replying to.
> For more information, visit http://www.vim.org/maillist.php

You can read ":help startup".

Best,
Marcin

Taylor Hedberg

unread,
Mar 18, 2012, 1:22:06 PM3/18/12
to vim...@googlegroups.com
Xtian Simon, Sun 2012-03-18 @ 08:38:34-0700:

> Do you know of any documentation about the order of execution in the
> VIMDOCS [1] or another source?

In addition to Marcin's recommendation, you can use the :scriptnames
command to see a list of all scripts that have been sourced in the order
in which they were executed.

Reply all
Reply to author
Forward
0 new messages