Vim: function to create comment and new subroutine using <cword>
I'm trying to write a function in my vimrc that would allow me to "hover" the in vim cursor (not the mouse cursor) over the word in question and and create a doc comment (with the word already in the comment) along with the new subroutine. So I would start with just:
newSubName
and then hover over and call the sub, it would replace that with:
#------------------------------------------------------------------------------
# Subroutine: newSubName
# Function :
# Parms :
# Returns :
# Assumes :
#------------------------------------------------------------------------------
sub newSubName() {
}
I have the code for creating the sub "declaration" at the bottom of the file (got from internet), but I can't really decipher how to turn it into what I want. The code that I am using is:
function! Newsub()
let word = "sub " . expand("<cword>") . "()" . " {" . "}"
let ln = search("__.*__", 'nW')
if ln == 0
call append('$', word)
else
call append(ln-1, word)
开发者_如何转开发 endif
endfunction
Any and all help is appreciated.
Thanks, RzITex
Any templates/snippets expander should simplify your life. For instance, with mu-template v2.4.0 (only available through svn/vim-addon-manager for the moment):
VimL: " {rtp}/after/template/{filetype}/func.template
#------------------------------------------------------------------------------
# Subroutine: <+g:func+>
# Function : <++>
# Parms : <++>
# Returns : <++>
# Assumes : <++>
# Date : <+strftime('%c')+>
# Author : <+g:myname_that_I_need_to_define_in_my_vimrc+>
#------------------------------------------------------------------------------
sub <+g:func+>() {
<++>
}<++>
and the ftplugin {rtp}/ftplugin/{filetype}.vim
" here the anti-inclusion guards, ...
" and the untested mapping
nnoremap <buffer> <c-x>f :let g:func=expand('<cword>')<cr>"_diw:exe "Mutemplate ".&ft."/func"<cr>:unlet g:func<cr>
Note: this mapping contain everything that you need:
- obtain the current word (there are many other methods)
- delete that word with
"_diw
(:h "_
,:h diw
) - apply something that will do the expansion. If you prefer to work manually with a function that calls
:put
, name its:FuncExpand(name)
for instance, and call it from the mapping withdiw:call <sid>FuncExpand(@")<cr>
(here I use another way to cut the function name under the cursor).
精彩评论