Last active
April 28, 2024 05:50
-
-
Save Konfekt/8a11d9a53a5e3697b0a2280ee8204e59 to your computer and use it in GitHub Desktop.
Use :Set! (instead of :set) to make setting persist in a modeline
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function! Set(args, isPersistent) abort | |
" remove whitespaces surrounding = | |
let cmd = 'set ' . substitute(a:args, '\s*=\s*', '=', 'g') | |
execute cmd | |
if !a:isPersistent | return | endif | |
" append modeline | |
let commentstring = empty(&l:commentstring) ? | |
\ (empty(&g:commentstring) ? '# %s' : &g:commentstring) : &l:commentstring | |
let modeline = substitute(commentstring,'%s',' vim: ' . cmd . ': ', '') | |
call append(line('$'), '') | |
call append(line('$'), trim(modeline)) | |
endfunction | |
command! -nargs=+ -bang -bar -complete=option Set call Set(<q-args>, <bang>0) | |
command! -nargs=1 -bang -bar SetSpelllang Set<bang> spelllang=<args> | |
command! -nargs=1 -bang -bar -complete=filetype SetFileType Set<bang> filetype=<args> | |
command! -nargs=1 -bang -bar -complete=customlist,s:complete_foldmethod SetFoldMethod Set<bang> foldmethod=<args> | |
function! s:complete_foldmethod(arglead, cmdline, cursorpos) | |
return filter(['marker', 'indent', 'syntax', 'expr', 'manual', 'diff'], 'v:val =~? "^" . a:arglead') | |
endfunction |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment