0

How to convert the output of the type() function, which is a number, into a verbal format, e.g. as described in help v:t_...?

To write my own mappings seems to be too cumbersome.

Presumably there exists a ready solution, but by googling I cannot find any.

Thanks, Regards Anton Wessel

=== addendum, Wessel, 23.11.2022

Thank you very much for your hint at the function typename(). I have overlooked it.

NOTES:

When writing such vimscripts, one should bear in mind:

The returned value of vim commands can be of type integer, string, and other. This is described in the documentation vim help.

This must be remembered by the programmer, but to be sure, the programmer should have displayed the typename.

When such a returned value has to be further processed as input parameter to command system(), the programmer has to use string() to convert a number type to a string type if necessary.

Since if to system() as input parameter a type integer is passed, vim gives an error message like

cannot open file <encrypted filename> 

The meaning of this is not obvious initially. Since the programmer had not caused a file to be opened.

This is the file where system() automatically writes the argument into, as help says.

Regards

Anton Wessel

2 Answers 2

1

There is the typename() function for this:

:echo typename('str')
string

You need Vim 8.2.2339 or newer for this.

It does display "Vim 9" type information:

:echo typename([])
list<unknown>

:echo typename([1])
list<number>

:echo typename(['', 1])
list<any>

Which may or may not be desired depending on what you want to do; you can strip off the <...> for collection types.


I did not know this either! I found it by just typing :echo type<Tab> on the Vim command line and seeing if that might complete to anything that looks useful, and turns out it did. Another very useful page for this kind of thing is :help function-list, which shows an overview of all functions.

0

In the momemt I use following code, only to have a possibility to continue my work:

let WE_curr_PID__as_string = string(WE_curr_PID)

let WE_type_yields__as_number = type(WE_curr_PID__as_string)

echo "WE_type_yields__as_number: ~" . WE_type_yields__as_number . "~\n"

let WE_type_yields__as_verbally = system( "read -r WE_arg; echo \"sys: WE_arg: ~$WE_arg~\" >&2; grep '|v:t_' /usr/share/vim/vim82/doc/eval.txt | awk -v WEawk_arg=$WE_arg '$2 == WEawk_arg {print $0}' | sed 's/^[[:space:]]*//' ", string(WE_type_yields__as_number) )

echo "WE_type_yields__as_verbally: ~" .  WE_type_yields__as_verbally . "~\n"

I hope that there exists a handy solution, like e.g. an option to type(), to choose if output only number, or only verbally, or both.

Regards Anton Wessel

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.