Problem with function

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

Problem with function

Cesar Romani-2
I'm using vim 8.1.682 on Windows.
I'd like to know what is wrong with following function?:

function! Finalize()
   :'<,'>join
   :s/ /,/g
endfunction
vmap ,f :call Finalize()<cr>

If I visually select the following text:

2.233 2.204
2.077 2.066 1.987
6.381 6.372 6.427 6.437
6.152 6.115

It correctly executes, but I get the error:
E16: Invalid range

But if I execute separately each line inside the function, i.e.
:'<,'>join
:s/ /,/g

I don't get any error

Thanks in advance,

--
Cesar

--
--
You received this message from the "vim_use" maillist.
Do not top-post! Type your reply below the text you are replying to.
For more information, visit http://www.vim.org/maillist.php

---
You received this message because you are subscribed to the Google Groups "vim_use" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: Problem with function

Tony Mechelynck
On Thu, Jan 3, 2019 at 10:09 PM Cesar Romani <[hidden email]> wrote:

>
> I'm using vim 8.1.682 on Windows.
> I'd like to know what is wrong with following function?:
>
> function! Finalize()
>    :'<,'>join
>    :s/ /,/g
> endfunction
> vmap ,f :call Finalize()<cr>
>
> If I visually select the following text:
>
> 2.233 2.204
> 2.077 2.066 1.987
> 6.381 6.372 6.427 6.437
> 6.152 6.115
>
> It correctly executes, but I get the error:
> E16: Invalid range
>
> But if I execute separately each line inside the function, i.e.
> :'<,'>join
> :s/ /,/g
>
> I don't get any error
>
> Thanks in advance,
>
> --
> Cesar

It's unusual to explicitly use :'<,'> in a function.

Try the following instead:

function Finalize() range
        execute a:firstline . ',' . a:lastline 'join'
        s/ /,/g
endfunction

The :join command inside the function should then get the same range
that was passed to the :call command calling the function. Without the
"range" flag, the function is called once for every line in the range
of the :call command.

see
    :help E124
    :help :func-range
    :help function-range-example
    :help :execute


Best regards,
Tony.

--
--
You received this message from the "vim_use" maillist.
Do not top-post! Type your reply below the text you are replying to.
For more information, visit http://www.vim.org/maillist.php

---
You received this message because you are subscribed to the Google Groups "vim_use" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
For more options, visit https://groups.google.com/d/optout.