Chong
Chong

Reputation: 963

customizing vimrc for functions and commands defined in plugins

Sometimes, customisation of vim utilising plugins' functions and commands increases the efficiency of using vim largely. And I was told it can be done by using conditional settings. For example, if I want to mapping some keys only when the plugin tabular is installed, I can write something like

if exists(":Tabularize")
    vnoremap ......
endif

into my ~/.vimrc file.

But because the starting sequence of vim is to load ~/.vimrc before plugins. These settings will only turn out not loaded. Is there a work around for it? Or how should I validly make these conditional settings?

Upvotes: 0

Views: 534

Answers (3)

Chong
Chong

Reputation: 963

Just realised another way, maybe more neat, as I can put all the script in .vimrc like rest of the configuration using this method(I want to make this configuration file as portable as possible). The idea behind it is simple, with autocmd for the event VimEnter. We can load some scripts after all plugins are loaded.

First define a function in the file .vimrc, and put all plugin specific behaviour in it.

function! g:LoadPluginScript()
"Tabular
    if exists(":Tabularize")
        vnoremap foo
        bar
    endif
"plugins A
    if exists("some_function_or_variable_defined_in_plugin_A")
        foo
        bar
    endif
endfunction

then, add an autocmd somewhere after this function to call it on event of VimEnter

augroup plugin_specific_script
    autocmd!
    autocmd VimEnter * call LoadPluginScript()
    foo bar
augroup END

And that's it. All plugin specific scripts will be successfully loaded after enter Vim, as long as the responsible plugins are available.

Upvotes: 0

Derek Redfern
Derek Redfern

Reputation: 1009

Create a new file called ~/.vim/after/tabularize.vim and put your script in it. Any files ending in .vim in the ~/.vim/after directory are loaded after the plugins are loaded, but before vim actually starts.

Upvotes: 3

Luc Hermitte
Luc Hermitte

Reputation: 32976

You can try

runtime plugin/tabthingy.vim " I don't know the plugin name

If your plugin is loaded by a plugins manager, you'll have to add this line (and the following test) after the configuration of your plugins manager.

Otherwise, IIRC, I've seen plugins managagers (or was it google's maktaba framework ?) that can execute things after the correct loading of plugins. But I guess the first solution should be enough.

Upvotes: 2

Related Questions