why "function!" instead of just "function"

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

why "function!" instead of just "function"

Bao Niu
I am learning Vimscript and I wonder if there is any difference between writing "function!" vs. "function"? What is that exclamation point for here?

--
--
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: why "function!" instead of just "function"

Xell Liu
On Tue, Jan 20, 2015 at 12:31 PM, Bao Niu <[hidden email]> wrote:
> What is that exclamation point for here?

Try :help before asking!

--
--
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: why "function!" instead of just "function"

Bao Niu
Hey I did try.
I got "E149: Sorry, no help for function!"



On Mon, Jan 19, 2015 at 8:35 PM, Xell Liu <[hidden email]> wrote:
On Tue, Jan 20, 2015 at 12:31 PM, Bao Niu <[hidden email]> wrote:
> What is that exclamation point for here?

Try :help before asking!

--
--
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 a topic in the Google Groups "vim_use" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/vim_use/71gztuCIKXI/unsubscribe.
To unsubscribe from this group and all its topics, send an email to [hidden email].
For more options, visit https://groups.google.com/d/optout.

--
--
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: why "function!" instead of just "function"

Xell Liu
On Tue, Jan 20, 2015 at 12:39 PM, Bao Niu <[hidden email]> wrote:
> Hey I did try.
> I got "E149: Sorry, no help for function!"

:help :function

It's a command, not a tag/keyword.

--
--
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: why "function!" instead of just "function"

Tim Chase
In reply to this post by Bao Niu
On 2015-01-19 20:31, Bao Niu wrote:
> I am learning Vimscript and I wonder if there is any difference
> between writing "function!" vs. "function"? What is that
> exclamation point for here?

As detailed at

  :help E123

If a function already exists with the same name as you want to
create, the "!" will tell it to overwrite the previously-existing
function.  Thus, you might have

  function Hello()
    echo 'Hello'
  endfunction

and then want to change it, so you'd use

  function! Hello()
    echoerr 'Hello'
  endfunction

to overwrite it.  Because of the behavior, I just use the "!" *every*
time that I define a function.  That way, I don't have to think about
whether it already exists or not.

-tim


--
--
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: why "function!" instead of just "function"

John Little-4
> On 2015-01-19 20:31, Bao Niu wrote:
> "function!" vs. "function"?

On Tuesday, January 20, 2015 at 5:49:55 PM UTC+13, Tim Chase replied:
> If a function already exists with the same name as you want to
> create, the "!" will tell it to overwrite the previously-existing
> function.

I'll add that many vim commands work like that, going back to the original vi, where f.ex.

    :w! foo.txt

overwrites any existing foo.txt.  However, for some commands an added ! indicates an alternate behaviour without any "overwrite any existing" or "really do it" connotation.  Blame the original vi, if you like, f.ex. :map! specifies mappings in insert mode.

Regards, John Little

--
--
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: why "function!" instead of just "function"

JohnBeckett
In reply to this post by Bao Niu
Bao Niu wrote:
> I am learning Vimscript and I wonder if there is any
> difference between writing "function!" vs. "function"?

Expanding on Tim's reply, suppose you have file h.vim which
contains:

  function! Hello()
    echoerr 'Hello'
  endfunction

In Vim, assuming the directory shown by :pwd is the directory
holding h.vim, you can do the following to "source" the script:

  :so h.vim

That executes h.vim which defines the function.

You could edit h.vim and source it again, and the edited
function would replace the original.

The idea is to help you to not accidentally overwrite functions.
If you do not have "!", the function will not be replaced if you
try to define it again.

John


--
--
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: why "function!" instead of just "function"

Steve Litt
In reply to this post by Xell Liu
On Tue, 20 Jan 2015 12:35:47 +0800
Xell Liu <[hidden email]> wrote:

> On Tue, Jan 20, 2015 at 12:31 PM, Bao Niu <[hidden email]> wrote:
> > What is that exclamation point for here?
>
> Try :help before asking!

Xell Liu,

If you have nothing to add to the discussion, why post at all?

SteveT

Steve Litt                *  http://www.troubleshooters.com/
Troubleshooting Training  *  Human Performance

--
--
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: why "function!" instead of just "function"

Steve Litt
In reply to this post by Tim Chase
On Mon, 19 Jan 2015 22:50:49 -0600
Tim Chase <[hidden email]> wrote:

> On 2015-01-19 20:31, Bao Niu wrote:
> > I am learning Vimscript and I wonder if there is any difference
> > between writing "function!" vs. "function"? What is that
> > exclamation point for here?
>
> As detailed at
>
>   :help E123
>
> If a function already exists with the same name as you want to
> create, the "!" will tell it to overwrite the previously-existing
> function.  Thus, you might have
>
>   function Hello()
>     echo 'Hello'
>   endfunction
>
> and then want to change it, so you'd use
>
>   function! Hello()
>     echoerr 'Hello'
>   endfunction
>
> to overwrite it.  Because of the behavior, I just use the "!" *every*
> time that I define a function.  That way, I don't have to think about
> whether it already exists or not.
>
> -tim

Tim,

Thanks for the complete explanation. I always wondered that myself.

SteveT

Steve Litt                *  http://www.troubleshooters.com/
Troubleshooting Training  *  Human Performance

--
--
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: why "function!" instead of just "function"

Salman Halim


On Jan 20, 2015 11:36 AM, "Steve Litt" <[hidden email]> wrote:
>
> On Mon, 19 Jan 2015 22:50:49 -0600
> Tim Chase <[hidden email]> wrote:
>
> > On 2015-01-19 20:31, Bao Niu wrote:
> > > I am learning Vimscript and I wonder if there is any difference
> > > between writing "function!" vs. "function"? What is that
> > > exclamation point for here?
> >
> > As detailed at
> >
> >   :help E123
> >
> > If a function already exists with the same name as you want to
> > create, the "!" will tell it to overwrite the previously-existing
> > function.  Thus, you might have
> >
> >   function Hello()
> >     echo 'Hello'
> >   endfunction
> >
> > and then want to change it, so you'd use
> >
> >   function! Hello()
> >     echoerr 'Hello'
> >   endfunction
> >
> > to overwrite it.  Because of the behavior, I just use the "!" *every*
> > time that I define a function.  That way, I don't have to think about
> > whether it already exists or not.
> >
> > -tim
>
> Tim,
>
> Thanks for the complete explanation. I always wondered that myself.
>
> SteveT

If you're writing a utility or plugin function that isn't specific to your script, I start with just 'function' as it helps me identify which function names already exist and I might clobber them. Once I know the name I've chosen is truly unique, I add the '!'.

Salman

--
--
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: why "function!" instead of just "function"

Nikolay Aleksandrovich Pavlov
In reply to this post by JohnBeckett


2015-01-20 13:48 GMT+03:00 John Beckett <[hidden email]>:
Bao Niu wrote:
> I am learning Vimscript and I wonder if there is any
> difference between writing "function!" vs. "function"?

Expanding on Tim's reply, suppose you have file h.vim which
contains:

  function! Hello()
    echoerr 'Hello'
  endfunction

In Vim, assuming the directory shown by :pwd is the directory
holding h.vim, you can do the following to "source" the script:

  :so h.vim

That executes h.vim which defines the function.

You could edit h.vim and source it again, and the edited
function would replace the original.

The idea is to help you to not accidentally overwrite functions.
If you do not have "!", the function will not be replaced if you
try to define it again.

This idea is only useful for small scripts for one-time job. It is bad to use it *always* because you cannot rely on the results of resourcing: there are loads of “state” things that persist while they should not: `:function!` overwrites existing function, but it is helpless if you removed or renamed a function in a script and resource it to check whether you have done everything right. It also will silently spoil user configuration if user or some plugin he installed has chosen the same function name.

Thus I suggest to *never* write :function! in any file in any directory in &runtimepath, but this is OK for testing answers for stackoverflow or vim-use, creating a script for a one-time job or something like this where you know and control such leftovers or just do not care. If you really do need to reload plugin without restarting Vim you can use same solution frawor uses: almost all s: functions are put into s:F dictionary (actually, in any dictionary: you just need to have them anonymous) so everything is needed to delete s: functions is :unlet s:F. All functions that were not put into s:F dictionary should be listed in s:_functions so frawor can delete them for you when you unload plugin (so function definition looks like

    function Foo()
    endfunction
    let s:_functions += ['Foo']

). Then frawor receives reference to s: dictionary, so it is able to do anything with it (including deleting s:F by unletting `F` key and iterating over s:_functions). Since all functions are global you can also add s:Foo function to a list (specifically frawor is also notified about plugin SID, so it can replace s: with actual function prefix if needed).

But this reloading facility is rather complicated and if you are not basing on frawor for some other reason it is easier and more robust to just reload Vim and never use :function! or :command! in plugins.
 

John


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

--
--
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: why "function!" instead of just "function"

Nikolay Aleksandrovich Pavlov
In reply to this post by Salman Halim


2015-01-20 19:42 GMT+03:00 Salman Halim <[hidden email]>:


On Jan 20, 2015 11:36 AM, "Steve Litt" <[hidden email]> wrote:
>
> On Mon, 19 Jan 2015 22:50:49 -0600
> Tim Chase <[hidden email]> wrote:
>
> > On 2015-01-19 20:31, Bao Niu wrote:
> > > I am learning Vimscript and I wonder if there is any difference
> > > between writing "function!" vs. "function"? What is that
> > > exclamation point for here?
> >
> > As detailed at
> >
> >   :help E123
> >
> > If a function already exists with the same name as you want to
> > create, the "!" will tell it to overwrite the previously-existing
> > function.  Thus, you might have
> >
> >   function Hello()
> >     echo 'Hello'
> >   endfunction
> >
> > and then want to change it, so you'd use
> >
> >   function! Hello()
> >     echoerr 'Hello'
> >   endfunction
> >
> > to overwrite it.  Because of the behavior, I just use the "!" *every*
> > time that I define a function.  That way, I don't have to think about
> > whether it already exists or not.
> >
> > -tim
>
> Tim,
>
> Thanks for the complete explanation. I always wondered that myself.
>
> SteveT

If you're writing a utility or plugin function that isn't specific to your script, I start with just 'function' as it helps me identify which function names already exist and I might clobber them. Once I know the name I've chosen is truly unique, I add the '!'.


Since you obviously do not test each and every plugin in world you cannot know that this name is truly unique.
 

Salman

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

--
--
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: why "function!" instead of just "function"

Bao Niu
Thank you guys, I really appreciate all the useful tips here!

On Tue, Jan 20, 2015 at 9:46 AM, Nikolay Pavlov <[hidden email]> wrote:


2015-01-20 19:42 GMT+03:00 Salman Halim <[hidden email]>:


On Jan 20, 2015 11:36 AM, "Steve Litt" <[hidden email]> wrote:
>
> On Mon, 19 Jan 2015 22:50:49 -0600
> Tim Chase <[hidden email]> wrote:
>
> > On 2015-01-19 20:31, Bao Niu wrote:
> > > I am learning Vimscript and I wonder if there is any difference
> > > between writing "function!" vs. "function"? What is that
> > > exclamation point for here?
> >
> > As detailed at
> >
> >   :help E123
> >
> > If a function already exists with the same name as you want to
> > create, the "!" will tell it to overwrite the previously-existing
> > function.  Thus, you might have
> >
> >   function Hello()
> >     echo 'Hello'
> >   endfunction
> >
> > and then want to change it, so you'd use
> >
> >   function! Hello()
> >     echoerr 'Hello'
> >   endfunction
> >
> > to overwrite it.  Because of the behavior, I just use the "!" *every*
> > time that I define a function.  That way, I don't have to think about
> > whether it already exists or not.
> >
> > -tim
>
> Tim,
>
> Thanks for the complete explanation. I always wondered that myself.
>
> SteveT

If you're writing a utility or plugin function that isn't specific to your script, I start with just 'function' as it helps me identify which function names already exist and I might clobber them. Once I know the name I've chosen is truly unique, I add the '!'.


Since you obviously do not test each and every plugin in world you cannot know that this name is truly unique.
 

Salman

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

--
--
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 a topic in the Google Groups "vim_use" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/vim_use/71gztuCIKXI/unsubscribe.
To unsubscribe from this group and all its topics, send an email to [hidden email].
For more options, visit https://groups.google.com/d/optout.

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