Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Vimscript: what's the point of 'abort' in a function definition?

Tags:

In vimscript, function definitions can take an abort argument. To quote the docs,

When the [abort] argument is added, the function will abort as soon as an error is detected 

This leads me to seriously question what exactly functions normally do when they encounter errors. Stumble blindly forth into the darkness?

What does abort actually do? Does it break all of the try...endtry blocks? When do you want to use it, and when do you want to avoid it?

like image 332
So8res Avatar asked Nov 16 '12 22:11

So8res


People also ask

What is let G in Vimrc?

l: local to a function. g: global. :help internal-variables. Follow this answer to receive notifications.

How do you call a function in Vim?

Calling A Function Vim has a :call command to call a function. The call command does not output the return value. Let's call it with echo . To clear any confusion, you have just used two different call commands: the :call command-line command and the call() function.

What is echo in Vim?

Plain old :echo will print output, but it will often disappear by the time your script is done. Using :echom will save the output and let you run :messages to view it later.


1 Answers

As glts mentioned, all the complex details are documented at :help except-compat, and the answer basically boils down to backwards compatibility and the inherent flexibility of Vimscript.

There's a natural progression from recorded macros to mappings to custom functions. With that in mind, it may make sense that when a command in a function causes an error (e.g. a %s/foo/bar/ that is not matching and missing the e flag), processing should continue.

On the other hand, when you write "industrial-grade" mappings, you'll almost always use a try..catch block inside your function call hierarchy, anyway (to avoid any multiline-errors Error detected while processing function: ..., and instead show a nice error message to the user).

So in practice, most published plugins do not use abort, but try..catch, and for quick, off-the-cuff stuff, you typically don't care too much about error handling, anyway.

like image 126
Ingo Karkat Avatar answered Oct 09 '22 11:10

Ingo Karkat