Should I use function or function! in vim scripts? - vim

I think I understand the difference between function and function!: if a function with the same name already exists function! silently replaces it, but function yields an error.
I end up using function! always. Because if I use simple function sooner or later it returns and bites me with:
E122: Function my_lib#MyHandyFunction already exists, add ! to replace it
Are there any situations when one should use simple function without !?

In scripts, it doesn't hurt to use :function!, but you should use script-local (s:Foo) or autoload-scoped (myscript#Foo) functions to properly namespace them. So, the override error for :function is helpful to alert you to redefinitions of global functions, but in scripts, you shouldn't need this precaution.
You have to use :function! when you want to reload the script during development (instead of restarting the whole Vim). (And plugins like my ReloadScript plugin can deal with the include guards.)
Another empirical point: Most of the plugins I have use :function!, probably for the easy reload.
The same goes for :command! and :normal!, where (usually), the version with ! should be used.

You should normally use function. Doing such, you would at least recognize when there's a name collision.
When using function! by default, you don't have any feedback that you're about to override an existing function (i.e. change existing functionality)!
Just have a look at the error message you've posted:
E122: Function my_lib#MyHandyFunction already exists, add ! to replace it
This means: careful, dude! If you use function! now, the users of my_lib#MyHandyFunction will experience things they never expected!

Related

vim mapping a plugin and providing argument coming from an external script

I use ConqueGdb plugin on a fairly frequent basis for my debugging needs. I decided to set a mapping for it to make my life a little easier. Below is what my mapping looks like -
map gd :ConqueGdb ./binary_name !script_which_returns_pid_of_binary
OR
map gd: ConqueGdb ./binary_name str2nr(system('~/bin/which_pid.sh'))
I noticed that the script in this case is not getting evaluated but instead being pasted as text. Then I tried again by wrapping this script in a function which returns the pid -
map gd :ConqueGdb ./binary_name call GETPID()
Same issue persisted.
Finally, I created a function and within in, I added the
ConqueGdb ./binary_name pid_variable
But here too the same issue prevails (i.e. pid_variable gets passed as text rather than being evaluated to the value it holds).
What am I doing wrong and how can I get vim to use the value stored in the variable rather than assume it is plain text?
TIA.
It seems you're looking for :exe
I guess something like:
exe ':ConqueGdb ./binary_name'. str2nr(system('~/bin/which_pid.sh'))
Instead of ./binary_name you could also use a variable that you assign somewhere else (like a local vimrc that acts as a plugin that defines your project (preferences & more))

Remove a function defined in a plugin

I just installed ntpeters/vim-better-whitespace plugin and it defines a EnableWhitespace function which is really annoying since I'm used to typing :E to get the Explorer and now there is a conflict (Ambiguous use of user-defined command).
Is there a way to remove this function in my vim.rc file (i.e. some magical 'unbind_function EnableWhitespace') and leave the plugin code intact?
The plugin also provides a ToggleStripWhitespaceOnSave, so I will not miss any functionality.
Thanks in advance.
EnableWhitespace is a user command, not a function (there is a function with that name but that's not relevant). You can delete it with:
:delcommand EnableWhitespace

What does the sharp (#) sign mean in a vim script function definition?

So I was looking for ways to get some object oriented stuff in vimcript when I found this page
So for instance:
function gnat#Make () dict
...
return
endfunction gnat#Make
What does the '#' mean?
Does it have to do with ending the function explicitly like that?
(usually one just endfu[nction] without the function name)
Thanks!
The # is for autoload scripts. Try :h autoload for more info.
I don't think that "explicit" function ending is allowed, as written in the vim help:
:endf[unction] The end of a function definition. Must be on a line
by its own, without other commands.
But it appears that when you try to put something after :endf, even if it's not the name of the function, no error occurs.

<SID> with foldexpr

I am reading Learn Vim Script the Hard Way and hit something that confused me whilst doing the exercise to convert the folding functions to script local ones.
I tried to go this:
setlocal foldexpr=<SID>GetPotionFold(v:lnum)
and renamed all the functions to start with s:
To my surprise this didn't work and every line had a fold level of 0? It works if I put GetPotionFold into the global scope. Do you have to use a globally scoped function when assigning it to a option? Why?
The <SID> can be used in a mapping or menu, unfortunately not in an option. (This is a shortcoming in the implementation.)
You'd either have to translate it into the actual <SNR>NNN_ prefix (there's an s:SID() example function at :help <SID>), or use a different scope that is accessible from outside the script that defines the function. It's commendable that you want to avoid clobbering the global function namespace, as this is prone to name clashes.
A nice trick is using the autoload function prefix; it doesn't just work in autoload scripts, but can also be used elsewhere, e.g. in plugin scripts. Just prepend the script's name, and you'll have a function that can be invoked from anywhere, but scoped to the script's name:
:function! MyScriptName#GetPotionFold(lnum)
...
:setlocal foldexpr=MyScriptName#GetPotionFold(v:lnum)
Adding to the previous answer, you could define the function s:SID() to determine the script number as in the help documentation and then use execute to set the fold expression as following:
exe "setlocal foldexpr=<SNR>" . s:SID() . "_GetPotionFold(v:lnum)"

how to understand these vim scripts

I have two question about understand those vim script. please give some help,
Question 1:
I download a.vim plugin, and i try to read this plugin, how to understand the below variable definition? the first line I can understand, but the second line, I don't know exactly "g:alternateExtensions_{'aspx.cs'}" means.
" E.g. let g:alternateExtensions_CPP = "inc,h,H,HPP,hpp"
" let g:alternateExtensions_{'aspx.cs'} = "aspx"
Question 2:
how to understand "SID" before the function name, using like below function definition and function call.
function! <SID>AddAlternateExtensionMapping(extension, alternates)
//omit define body
call <SID>AddAlternateExtensionMapping('h',"c,cpp,cxx,cc,CC")
call <SID>AddAlternateExtensionMapping('H',"C,CPP,CXX,CC")
thanks for you kindly help.
let g:alternateExtensions_{'aspx.cs'} = "aspx"
That is an inline expansion of a Vimscript expression into a variable name, a rather obscure feature that is rarely used since Vim version 7. See :help curly-braces-names for details. It is usually used to interpolate a variable, not a string literal like here ('aspx.cs'). Furthermore, this here yields an error, because periods are forbidden in variable names. Newer plugins would use a List or Dictionary variable, but those data types weren't available when a.vim was written.
To avoid polluting the function namespace, plugin-internal functions should be script-local, i.e. have the prefix s:. To invoke these from a mapping, the special <SID> prefix has to be used instead of s:, because <SID> internally gets translated into something that keeps the script's ID, whereas the pure s:, when executed as part of the mapping, has lost its association to the script that defined it.
Some plugin authors don't fully understand this unfortunate and accidental complexity of Vim's scoping implementation either, and they put the <SID> prefix also in front of the function name (which works, too). Though it's slightly more correct and recommended to write it like this:
" Define and invoke script-local function.
function! s:AddAlternateExtensionMapping(extension, alternates)
...
call s:AddAlternateExtensionMapping('h',"c,cpp,cxx,cc,CC")
" Only in a mapping, the special <SID> prefix is actually necessary.
nmap <Leader>a :call <SID>AddAlternateExtensionMapping('h',"c,cpp,cxx,cc,CC")
<SID> is explained in :help <SID>:
When defining a function in a script, "s:" can be prepended to the name to
make it local to the script. But when a mapping is executed from outside of
the script, it doesn't know in which script the function was defined. To
avoid this problem, use "<SID>" instead of "s:". The same translation is done
as for mappings. This makes it possible to define a call to the function in
a mapping.
When a local function is executed, it runs in the context of the script it was
defined in. This means that new functions and mappings it defines can also
use "s:" or "<SID>" and it will use the same unique number as when the
function itself was defined. Also, the "s:var" local script variables can be
used.
That number is the one you see on the left when you do :scriptnames, IIRC.

Resources