handling error in function

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

handling error in function

tekion
All,
I have
 function TrimTab()
   %s/^tab//g
endfunction

map <S-F11> :call  TrimTab()<CR>

It works when their are tabs in my file to remove, but display error
when there are no tabs. Is there a way to catch and handle the error
so I could display a friendly message?  Thanks.

--
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
Reply | Threaded
Open this post in threaded view
|

Re: handling error in function

Nikolay Aleksandrovich Pavlov
Reply to message «handling error in function»,
sent 18:29:25 17 October 2010, Sunday
by tekion:

> It works when their are tabs in my file to remove, but display error
> when there are no tabs. Is there a way to catch and handle the error
> so I could display a friendly message?  Thanks.
Use :try ... :catch:
    function TrimTab()
        try
            %s/^tab//g
        catch /^Vim(substitute):E486:/
            echo v:exception
        endtry
    endfunction

signature.asc (205 bytes) Download Attachment
Reply | Threaded
Open this post in threaded view
|

Re: handling error in function

Gary Johnson-4
In reply to this post by tekion
On 2010-10-17, tekion wrote:

> All,
> I have
>  function TrimTab()
>    %s/^tab//g
> endfunction
>
> map <S-F11> :call  TrimTab()<CR>
>
> It works when their are tabs in my file to remove, but display error
> when there are no tabs. Is there a way to catch and handle the error
> so I could display a friendly message?  Thanks.

If you want to just ignore the error and not display a different
message, just add the 'e' flag:


    %s/^tab//ge

See

    :help :s_flags

Regards,
Gary

--
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