How to toggle (Vimplug) plugins on and off on the fly?

Viewed 222

Using COC on multiple instances can really take a toll on my intel i3+4GB machine.
So I was wondering if I could toggle it on demand.
I have managed to turn it off by default and toggle it on using Vimplug's built in feature like the following.

Plug 'neoclide/coc.nvim', {'branch': 'release', 'on': 'CocToggle' }

However, I'm not sure how to toggle it off.
Any help would be highly appreciated!

P.S.: I have tried CocDisable and didn't notice any practical benefits that would minimize the resource usage like killing the gazillion node processes started by COC for each instances!

Solution

I ended up writing the following function for my use case.

Plug 'neoclide/coc.nvim', {'branch': 'release' }
let g:coc_start_at_startup = 0

let s:coc_enabled = 0
function! ToggleCoc()
   if s:coc_enabled == 0
      let s:coc_enabled = 1
      CocStart
      echo 'COC on'
   else
      let s:coc_enabled = 0
      echo 'COC off'
      call coc#rpc#stop()
   endif
endfunction
nnoremap <silent> <leader>h :call ToggleCoc()<cr>

1 Answers

My use case (I use vim-plug) can be different but it could help. Normally my nerdtrhee is disabled until I call it throught a mapping

Plug 'scrooloose/nerdtree', { 'on': 'NERDTreeToggle' }
nnoremap <F2> :NERDTreeToggle<cr>

And I have a function to load any plugin that is disabled on startup

if !exists('*LoadPluginFunction')
    fun! LoadPluginFunction(pluginname)
        let l:curpos = getcurpos()
        execute plug#load(a:pluginname)
        call cursor(l:curpos[1], l:curpos[2])
        return ''
    endfun
endif
command! -nargs=1 LoadPlugin :call LoadPluginFunction(<q-args>)

If I want to load it manyally I call:

:LoadPlugin nerdtree

Finally I have found a coc option that can help you:

let g:coc_start_at_startup = 0

You can start your vim and create a mapping to load the coc plugin manually

Related