Missing :endfunction error for vim function -
in vimrc file trying write function take in 2 lines numbers arguments , comments out lines in range. using substitute start of line '^' , replacing '#' works fine when call outside function. function have far:
function comment(line1, line2) a:line1,a:line2s/^/#/g endfunction
this not working though , getting error when try start vim saying 'missing :endfunction'
in command:
a:line1,a:line2s/^/#/g
some parts variables (a:line1
, a:line2
). need evaluated. so, can wrap every part of command inside string, except variables, concatenate parts dot operator, , pass result command :execute
execute regular ex command:
execute a:line1.','.a:line2.'s/^/#/g'
if need command call it, try one:
function! comment(line1, line2) execute a:line1.','.a:line2.'s/^/#/' endfunction command! -range=% comment call comment(<line1>,<line2>)
it defines :comment
command calls function same name. accepts range, because it's defined -range
attribute. if don't provide range command, use whole buffer, because %
(=1,$
) defined default range. see :h user-commands
more info.
to use command, visually select lines, execute:
:'<,'>comment
to use function, assuming wanted comment lines address between 10 , 20, execute:
:call comment(10,20)
it's not linked issue, if don't add bang keyword function
, every time source script, you'll have error:
e122: function comment exists, add ! replace
same thing command, want add bang after keyword command
, otherwise you'll have error:
e174: command exists: add ! replace
Comments
Post a Comment