I was searching for the vim scripts to clear the registers in vim, I found this helpful script
right now i am inserting the code withing a function and putting it in my .vimrc
file
function ClearReg()
let regs='abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789/-"'
let i=0
while (i<strlen(regs))
exec 'let @'.regs[i].'=""'
let i=i+1
endwhile
unlet regs
endfunction
my question is
1. is the right way to create a function and put it in a .vimrc file?
2. If i would like to use this script as a plugin which path should i put this script?
Prefer to put functions in autoload plugins. They act as lazily loaded library plugins.
&rtp/autoload/lh
, and the functions have names like lh#foo#bar()
. Thanks to that, there is no risk of conflict with another plugin that defines a foo#bar()
function.foo#bar#titi()
is defined somewhere in &rtp/autoload/foo/bar.vim
.Thanks to autoload plugins I can also control the logging level of my functions, plugin (library) per (by?) plugin (library). For instance in the recent autoload/lh/async.vim
, I start the script with:
" credits....
let s:k_version = '3132'
" ## Misc Functions {{{1
" # Version {{{2
function! lh#async#version()
return s:k_version
endfunction
" # Debug {{{2
let s:verbose = get(s:, 'verbose', 0)
function! lh#async#verbose(...)
if a:0 > 0 | let s:verbose = a:1 | endif
return s:verbose
endfunction
function! s:Log(expr, ...)
call call('lh#log#this',[a:expr]+a:000)
endfunction
function! s:Verbose(expr, ...)
if s:verbose
call call('s:Log',[a:expr]+a:000)
endif
endfunction
function! lh#async#debug(expr) abort
return eval(a:expr)
endfunction
which permits me to fetch the internal value of any script variable (or function) with lh#aynch#debug('s:job_queue')
for instance, and also to trace what is done with:
function! s:start_next() dict abort " {{{3
...
call s:Verbose('Starting next job: %1', job)
Which will display me in the quickfix windows after a :call lh#async#verbose(1)
(and a :LHLogs qf
) (when I use this library to generate tags in the background):
autoload/lh/async.vim|100| Push or start job: {'txt': 'ctags OTB', 'cmd': 'cd /path/to/src/OTB && ctags --tag-relative=yes --c++-kinds=+pf --fields=+imaSftx{c++.properties} --extra=+q --languages=C,C++ -f ossimplugins.tags -R', 'callback': function('<SNR>256_callback', {'output': [], 'callback': function('<SNR>256_callback')}), 'close_cb': function('<SNR>256_TagGenerated', ['/path/to/src/OTB/ossimplugins.tags', '', {'output': [], 'callback': function('<SNR>256_callback')}]), 'before_start_cb': function('delete', ['/path/to/src/OTB/ossimplugins.tags']), 'args': {'callback': function('<SNR>256_callback', {'output': [], 'callback': function('<SNR>256_callback')}), 'close_cb': function('<SNR>256_TagGenerated', ['/path/to/src/OTB/ossimplugins.tags', '', {'output': [], 'callback': function('<SNR>256_callback')}])}} at 1-th position
autoload/lh/async.vim|116| Starting next job: {'txt': 'ctags OTB', 'cmd': 'cd /path/to/src/OTB && ctags --tag-relative=yes --c++-kinds=+pf --fields=+imaSftx{c++.properties} --extra=+q --languages=C,C++ -f ossimplugins.tags -R', 'callback': function('<SNR>256_callback', {'output': [], 'callback': function('<SNR>256_callback')}), 'close_cb': function('<SNR>256_TagGenerated', ['/path/to/src/OTB/ossimplugins.tags', '', {'output': [], 'callback': function('<SNR>256_callback')}]), 'before_start_cb': function('delete', ['/path/to/src/OTB/ossimplugins.tags']), 'args': {'callback': function('<SNR>256_callback', {'output': [], 'callback': function('<SNR>256_callback')}), 'close_cb': function('<SNR>256_TagGenerated', ['/path/to/srcsrc/OTB/ossimplugins.tags', '', {'output': [], 'callback': function('<SNR>256_callback')}])}}
autoload/lh/async.vim|126| job_start(['/bin/bash', '-c', 'cd /path/to/src/OTB && ctags --tag-relative=yes --c++-kinds=+pf --fields=+imaSftx{c++.properties} --extra=+q --languages=C,C++ -f ossimplugins.tags -R']) status: {'status': 'run', 'stoponexit': 'term', 'exitval': 0, 'exit_cb': 0, 'channel': channel 0 open, 'process': 22266}
autoload/lh/async.vim|148| Job finished process 22266 dead -- {'status': 'dead', 'stoponexit': 'term', 'exitval': 0, 'exit_cb': 0, 'channel': channel 0 closed, 'process': 22266}
I can require logs to be produced in lh-tags plugin also, or instead of async-library.
This is of course a little bit overkill for very simple scripts. But the more complex the code becomes, the more useful this approach is.
Regarding the where to put it. By default, you can store your autoload plugin (or any other file) into $HOME/.vim/autoload/
under *nix, or $HOME/vimfiles/autoload/
under Windows. However, if some day you wish to share it, or to put it into its own repository, the exact directory where to store the plugin will depend of the packages managers you use. First start with :help 'rtp'
, then read the documentation of your package manager.