Skip to content

Instantly share code, notes, and snippets.

@dezza
Last active June 21, 2026 08:14
Show Gist options
  • Select an option

  • Save dezza/33d20d2ca2c16df556c264d8d26e5821 to your computer and use it in GitHub Desktop.

Select an option

Save dezza/33d20d2ca2c16df556c264d8d26e5821 to your computer and use it in GitHub Desktop.
vim9script
# buf.vim
# Depends:
# {{{ get
# Returns: listed buffers
export def GetRegularBuffers(): list<dict<any>>
return getbufinfo()->filter((k, v) => (v.listed == 1))
enddef
# ...
# }}}
vim9script
# file.vim - various file utils
# Depends:
export def ChangedSinceVimStart(file: string): bool
if !has_key(g:, 'vim_started')
throw 'g:vim_started not set'
endif
var file_mtime = getftime(file)
if !file_mtime
return false
endif
var now = localtime()
var vim_uptime = g:vim_started[1]
var file_age = now - file_mtime
return file_age >= vim_uptime
enddef
# ...
defcompile
vim9script
# reload.vim - reload vimrc,plugins,filetype(,syntax)
# Depends: ui.vim, file.vim, buf.vim, g:vim_started
# Calls:
# augroup VimrcInitialize
# autocmd!
# autocmd VimEnter * call <SID>Initialize()
# augroup END
# Depends: file#ChangedSinceVimStart
export def Reload(): bool
var changed = false
var autoload = glob($MYVIMDIR .. "autoload/**/*", 1, 1)
->filter((_, p) => !isdirectory(p))
->filter((_, file) => fnamemodify(file, ':t') !=# 'reload.vim')
for f in autoload
if file#ChangedSinceVimStart(f)
changed = true
# this can fail and prevent vimrc load
execute printf('source %s', fnameescape(f))
endif
endfor
if file#ChangedSinceVimStart($MYVIMRC)
changed = true
source $MYVIMRC
# Reload 'FileType' autocmd
for b in buf#GetRegularBuffers()
var ft = getbufvar(b.bufnr, '&filetype')
if empty(ft)
continue
endif
# Reset filetype to reload changes
setbufvar(b.bufnr, '&filetype', '')
setbufvar(b.bufnr, '&filetype', ft)
endfor
doautocmd VimrcInitialize VimEnter
endif
ui#StatusPopup($'Reload {$MYVIMRC}, /autoload -- changed:{changed}')
return true
enddef
defcompile
" hybrid vimrc using `:def! s:` in legacy best of both worlds
if has('vim_starting')
let g:vim_started = reltime()
endif
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment