Vim-plug: Vimtex regex function not available until after plug#end

Created on 22 Jul 2017  路  3Comments  路  Source: junegunn/vim-plug

I have vimtex and youcompleteme working together. A necessary chunk of code is:

if !exists('g:ycm_semantic_triggers')
   let g:ycm_semantic_triggers = {}
endif
let g:ycm_semantic_triggers.tex = g:vimtex#re#youcompleteme

This code only works if it is placed before plug#end. If I place it before, I get the following error:
```Error detected while processing /home/conner/.vimrc:
line 64:
E121: Undefined variable: g:vimtex#re#youcompleteme
E15: Invalid expression: g:vimtex#re#youcompleteme
Press ENTER or type command to continue

Previously, this was not an issue as I just had all of my plugins loaded, and then set the options separately. However, in an attempt to refactor my .vimrc I starting putting my plugin settings together with the plugin call itself so I could `{{{ }}}`  fold everything.

This isn't a huge deal, but a definite annoyance. Sorry if this is a vimtex issue, but since it is `plug#end` dependent I figured I would start here.
vim --version

VIM - Vi IMproved 8.0 (2016 Sep 12, compiled Jul 14 2017 16:17:03)
Included patches: 1-711
Compiled by Arch Linux
Huge version without GUI. Features included (+) or not (-):
+acl +file_in_path +mouse_sgr +tag_old_static
+arabic +find_in_path -mouse_sysmouse -tag_any_white
+autocmd +float +mouse_urxvt -tcl
-balloon_eval +folding +mouse_xterm +termguicolors
-browse -footer +multi_byte +terminfo
++builtin_terms +fork() +multi_lang +termresponse
+byte_offset +gettext -mzscheme +textobjects
+channel -hangul_input +netbeans_intg +timers
+cindent +iconv +num64 +title
+clientserver +insert_expand +packages -toolbar
+clipboard +job +path_extra +user_commands
+cmdline_compl +jumplist +perl/dyn +vertsplit
+cmdline_hist +keymap +persistent_undo +virtualedit
+cmdline_info +lambda +postscript +visual
+comments +langmap +printer +visualextra
+conceal +libcall +profile +viminfo
+cryptv +linebreak +python/dyn +vreplace
+cscope +lispindent +python3/dyn +wildignore
+cursorbind +listcmds +quickfix +wildmenu
+cursorshape +localmap +reltime +windows
+dialog_con -lua +rightleft +writebackup
+diff +menu -ruby +X11
+digraphs +mksession +scrollbind +xfontset
-dnd +modify_fname +signs -xim
-ebcdic +mouse +smartindent -xpm
+emacs_tags -mouseshape +startuptime +xsmp_interact
+eval +mouse_dec +statusline +xterm_clipboard
+ex_extra +mouse_gpm -sun_workshop -xterm_save
+extra_search -mouse_jsbterm +syntax
+farsi +mouse_netterm +tag_binary
system vimrc file: "/etc/vimrc"
user vimrc file: "$HOME/.vimrc"
2nd user vimrc file: "~/.vim/vimrc"
user exrc file: "$HOME/.exrc"
defaults file: "$VIMRUNTIME/defaults.vim"
fall-back for $VIM: "/usr/share/vim"
Compilation: gcc -c -I. -Iproto -DHAVE_CONFIG_H -D_FORTIFY_SOURCE=2 -march=x86-64 -mtune=generic -O2 -pipe -fstack-protector-strong -fno-plt -U_FORTIFY_SOURCE -D_FORTIFY_SOURCE=1
Linking: gcc -Wl,-E -Wl,-rpath,/usr/lib/perl5/core_perl/CORE -Wl,-O1,--sort-common,--as-needed,-z,relro,-z,now -L/usr/local/lib -Wl,--as-needed -o vim -lSM -lICE -lXpm -lXt -lX11 -lXdmcp -lSM -lICE -lm -lncurses -lelf -lnsl -lselinux -lacl -lattr -lgpm -ldl -Wl,-E -Wl,-rpath,/usr/lib/perl5/core_perl/CORE -Wl,-O1,--sort-common,--as-needed,-z,relro -fstack-protector-strong -L/usr/local/lib -L/usr/lib/perl5/core_perl/CORE -lperl -lpthread -lnsl -ldl -lm -lcrypt -lutil -lc
```

  • Type:

    • [X] Bug

    • [ ] Enhancement

    • [ ] Feature Request

    • [X] Question

  • OS:

    • [ ] All/Other

    • [X] Linux

    • [ ] OS X

    • [ ] Windows

  • Vim:

    • [X] Terminal Vim

    • [ ] GVim

    • [ ] Neovim

duplicate question

Most helpful comment

Is it because options are ultimately just variables

Yep. You can define variables of any name at any time, but to call a function, you need the source code.

if !exists('g:ycm_semantic_triggers')
  let g:ycm_semantic_triggers = {}
endif
au VimEnter * let g:ycm_semantic_triggers.tex = g:vimtex#re#youcompleteme

All 3 comments

It's how vim-plug works. The behavior is documented on the README page

call plug#end() to update &runtimepath and initialize plugin system

You can find a related discussion in #615. For now, you can hook it up to VimEnter event so that the code is executed after vimrc.

Okay, it just doesn't make sense to me that I can set options but not the functions. Is it because options are ultimately just variables? Sorry if it seems like a basic question, but vim internals still seem quite mysterious to me.

Anyways, for those that may end up here in the future, this was my solution using an -autocommand- as Junegunn suggested.

au VimEnter *.tex 
    \  if !exists('g:ycm_semantic_triggers')
    \ |  let g:ycm_semantic_triggers = {}
    \ |endif
    \ |let g:ycm_semantic_triggers.tex = g:vimtex#re#youcompleteme

Is it because options are ultimately just variables

Yep. You can define variables of any name at any time, but to call a function, you need the source code.

if !exists('g:ycm_semantic_triggers')
  let g:ycm_semantic_triggers = {}
endif
au VimEnter * let g:ycm_semantic_triggers.tex = g:vimtex#re#youcompleteme
Was this page helpful?
0 / 5 - 0 ratings