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.

In my .vimrc I am trying to use ftplugin and obviously use some commands relating to that under the assumption that it was loaded successfully. However, I have now encountered a few old machines that don't have the plugin installed. Can I somehow make loading of this plugin conditional and add filetype on and similar directives into the same conditional block?

I have seen there are conditions for color schemes and the Vim version, but I haven't seen an example that would check for the plugin (or didn't recognize it).

NB: Be gentle, I am a VimScript beginner.

share|improve this question
    
Note that plugins are loaded after your ~/.vimrc, so you won't be able to test for the effects of a plugin within your ~/.vimrc unless you test for the existence of the plugin file or defer the test until after plugins have been loaded with an autocommand such as VimEnter. –  garyjohn Feb 15 '13 at 1:16
    
@garyjohn: aha, that's interesting. Because this kind of contradicts the existing answer. Could you write it up as an answer? –  0xC0000022L Feb 15 '13 at 1:51
    
I've edited my answer to somewhat address that issue. –  qqx Feb 15 '13 at 2:10
    
My comment didn't contradict qqx's answer; it was meant to draw attention to a point that could have been missed if one didn't read qqx's answer carefully or made incorrect inferences from it. The answer was good to start with and is even clearer now. –  garyjohn Feb 15 '13 at 3:08
add comment

2 Answers

up vote 2 down vote accepted

You can wrap that block in a conditional which uses the exists() function to check if a variable, command or function defined by the plugin is known to vim.

Here are a couple bits that I have in files under ~/.vim:

" after/plugin/speeddating.vim
if exists(':SpeedDatingFormat')
    SpeedDatingFormat %-d %B %Y
endif

" ftplugin/ruby.vim
if exists('g:loaded_surround') && !exists('b:surround_'.char2nr(':'))
  let b:surround_{char2nr(':')} = ":\r"
endif

Note that the above bits are in files that get evaluated after normal plugins, here an ftplugin, and a file in the after/plugin directory.

Another option would be to use try/catch blocks, although this requires at least vim 7.0:

if v:version >= 700
    try
        runtime bundle/pathogen/autoload/pathogen.vim
        call pathogen#infect()
    catch
    endtry
endif

Once something in the try section of that block fails it will skip to the catch section. Since the catch section is empty, it will just continue on with the remainder of the initialization file after the endtry statement.

Since this is manually loading code rather than relying on a plugin being already loaded, this can be done in the .vimrc file itself.

share|improve this answer
    
Could you add the version requirements for the try construct? Would the old Vim understand this? I.e. when was it introduced. Thanks and +1 for now. –  0xC0000022L Feb 15 '13 at 0:37
1  
I've added information about the version required. –  qqx Feb 15 '13 at 2:09
    
I think the last solution with the version check wrapping the try should work. Thanks a lot. Let's see whether another answer will still arrive. Otherwise I'll of course accept yours. –  0xC0000022L Feb 15 '13 at 2:18
    
ah, of course this disables the plugin on 6.x versions. Hmmm ... need to find something better, but this will work in the meantime. Thanks. –  0xC0000022L Feb 15 '13 at 2:29
    
Yet another alternative is using :silent! {cmd}, which suppresses the error when {cmd} does not exist. This even works in Vim 6. –  Ingo Karkat Feb 15 '13 at 8:02
show 1 more comment

Yet another alternative is using :silent! {cmd}, which suppresses the error when {cmd} does not exist. The main benefit is that it's a short single command. This even works in Vim 6, and is great for optional stuff.

For example, it is used by plugins that use Tim Pope's repeat.vim to make mappings repeatable.

share|improve this answer
    
Would something like !silent runtime ftplugin/man.vim | filetype on | filetype plugin on | filetype indent on work to shut up all the commands following the !silent or is that always specific to the next command? –  0xC0000022L Feb 15 '13 at 13:58
    
It's :silent!, not !silent, and it applies to all contained commands, except for when :unsilent is used somewhere inside. (But that's rare.) –  Ingo Karkat Feb 15 '13 at 14:08
    
oops, hard to fix now in the comment. But got it. Thanks. –  0xC0000022L Feb 15 '13 at 14:12
add comment

Your Answer

 
discard

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.