Skip to content

Instantly share code, notes, and snippets.

@kkoomen
Last active July 30, 2023 05:47
Show Gist options
  • Star 14 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kkoomen/68319b08ab843ce67cf7b282b0b2fd24 to your computer and use it in GitHub Desktop.
Save kkoomen/68319b08ab843ce67cf7b282b0b2fd24 to your computer and use it in GitHub Desktop.
Vim-Plug: Run PlugUpdate every week automatically

This snippet will check every time you run Vim whether it updated all your Plug packages for you. It will do this once a week automatically for you.

Add the following to your .vimrc:

function! OnVimEnter() abort
  " Run PlugUpdate every week automatically when entering Vim.
  if exists('g:plug_home')
    let l:filename = printf('%s/.vim_plug_update', g:plug_home)
    if !filereadable(l:filename)
      call writefile([], l:filename)
    endif

    let l:this_week = strftime('%Y_%V')
    let l:contents = readfile(l:filename)
    if index(l:contents, l:this_week) < 0
      call execute('PlugUpdate')
      call writefile([l:this_week], l:filename, 'a')
    endif
  endif
endfunction

autocmd VimEnter * call OnVimEnter()
@RodgerOliver
Copy link

Thanks man 😄, this is very useful. Much better than execute do this by hand once in a while.

@kkoomen
Copy link
Author

kkoomen commented Aug 3, 2019

@RodgerOliver I've just updated the gist. Instead of writing a single file for every week, it's creating a single file and appending the strftime.

@orrpeles
Copy link

orrpeles commented Aug 3, 2019

Really nice, congrats on the initiative!
Do you have something similar that updated Ubuntu ?

@RodgerOliver
Copy link

@orrpeles you can use cron for this.

@orrpeles
Copy link

orrpeles commented Aug 3, 2019

Thanks, I use cron to run some tasks but never thought about this one) Still can u help me figure out a script that will look for update upon Ubuntu initializing?

@kkoomen
Copy link
Author

kkoomen commented Aug 4, 2019

@orrpeles You can run the snippet in a headless way by running vim +PlugUpdate +qa > /dev/null 2&>1. This won't show vim but will run vim with the given commands.

You should be able to bind this to cron in some way. Didn't get it to work myself for some reason, maybe this had to do that cron did run as root and root has no plugins on my system.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment