why "function!" instead of just "function"

44 views
Skip to first unread message

Bao Niu

unread,
Jan 19, 2015, 11:31:21 PM1/19/15
to vim...@googlegroups.com
I am learning Vimscript and I wonder if there is any difference between writing "function!" vs. "function"? What is that exclamation point for here?

Xell Liu

unread,
Jan 19, 2015, 11:36:35 PM1/19/15
to vim_use
On Tue, Jan 20, 2015 at 12:31 PM, Bao Niu <niub...@gmail.com> wrote:
> What is that exclamation point for here?

Try :help before asking!

Bao Niu

unread,
Jan 19, 2015, 11:39:16 PM1/19/15
to vim...@googlegroups.com
Hey I did try.
I got "E149: Sorry, no help for function!"




--
--
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 vim_use+u...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Xell Liu

unread,
Jan 19, 2015, 11:47:37 PM1/19/15
to vim_use
On Tue, Jan 20, 2015 at 12:39 PM, Bao Niu <niub...@gmail.com> wrote:
> Hey I did try.
> I got "E149: Sorry, no help for function!"

:help :function

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

Tim Chase

unread,
Jan 19, 2015, 11:49:55 PM1/19/15
to vim...@googlegroups.com, niub...@gmail.com
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


John Little

unread,
Jan 20, 2015, 4:53:08 AM1/20/15
to vim...@googlegroups.com, niub...@gmail.com
> 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

John Beckett

unread,
Jan 20, 2015, 5:48:38 AM1/20/15
to vim...@googlegroups.com
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


Steve Litt

unread,
Jan 20, 2015, 11:35:35 AM1/20/15
to vim...@googlegroups.com
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

Steve Litt

unread,
Jan 20, 2015, 11:36:14 AM1/20/15
to vim...@googlegroups.com
Tim,

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

Salman Halim

unread,
Jan 20, 2015, 11:42:22 AM1/20/15
to Vim Users

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

Nikolay Pavlov

unread,
Jan 20, 2015, 12:44:50 PM1/20/15
to vim...@googlegroups.com
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 vim_use+u...@googlegroups.com.

Nikolay Pavlov

unread,
Jan 20, 2015, 12:46:25 PM1/20/15
to vim...@googlegroups.com
Since you obviously do not test each and every plugin in world you cannot know that this name is truly unique.
 

Salman

Bao Niu

unread,
Jan 20, 2015, 5:04:19 PM1/20/15
to vim...@googlegroups.com
Thank you guys, I really appreciate all the useful tips here!

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 vim_use+u...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages