I have written a function which capitalises the first letter of each word like this:
function! CamelCase() range let old = getline(a:firstline, a:lastline) for line in old let x = substitute(line,'\v<(.)(\w*)', '\u\1\L\2', 'g') echom x endfor endfunction command! -range=% CamelCase <line1>,<line2>call CamelCase() Currently :CamelCase()displays the output in :messsages (because of echom)
How do i modify the function so it modifies the current buffer directly instead ? I have tried removing echom x and let x= but I get a large number of errors.
setline(line, substitute(...)), for one.callwas implied in my comment but I should have been explicit.function()won't work without preceding it withcallor something that consumes its output (e.g.echoor variable assignment). Commands are different...they can be called straight/unadorned.func()(examples:let foo = func(),echo func(),call otherfunc(func())) then invocation offunc()is implied. If you only care about the side-effects (i.e. you want to ignore/discard the returned value) then usecallto invokefunc().