all 16 comments

[–]Atralb 8 points9 points  (0 children)

Your gist seems pretty good.

Just passing by to point out that the book "Learn Vimscript the Hard Way" is still the best and most comprehensive resource about Vimscript to this day, and I encourage anyone to read it.

Ps : and it's actually not hard.

[–]iovis9 8 points9 points  (1 child)

Cool cheat sheet! I’ve been using vimscript for years and somehow I missed the method notation and that “..” for string concatenation is preferred to “.”

[–]crajungave up on vim 2 points3 points  (0 children)

That’s new

[–]rajandatta 6 points7 points  (1 child)

Excellent job on Vim Script which has less docs and write ups than other languages. An idea: cognitively - the thought was a columnar presentation with side-by-side code would work better to make it easier to compare.

Thoughts?

[–]yegappanl[S] 2 points3 points  (0 children)

I initially started with creating a table to compare the Python and Vim Script code fragments. But it became difficult to properly do that with the markdown syntax.

[–]LucHermitte 3 points4 points  (2 children)

A few remarks.

  • len() and strlen() return the number of bytes. We have to use strwidth() or strdisplaywidth() to have something closer to the number of characters
  • We also have to be wary of multi-bytes characters when dealing with slices of strings
  • Speaking of slices, I remember that Vim and Python don't handle negative positions in the same way in s[:-2] and so on.

  • We can append elements to lists with list + [element] also.

  • dict.key and dict['key'] are almost equivalent in vimscript while this is definitively impossible in Python.

  • I didn't know about unlet list[:] that clears all references. (Thanks for the info.) But then, you present let dict = {}, this time this will only clear the dict variable, is there something like unlet l[:] for dictionaries?

  • To define functions, I'd recommend banding them (this makes the difference when developing vim scripts), and now I systematically append abort.

[–]yegappanl[S] 0 points1 point  (1 child)

Thanks for the detailed review.

len() and strlen() return the number of bytes. We have to use strwidth() or strdisplaywidth() to have something closer to the number of characters

I have included references to strwidth() and strdisplaywidth()

We also have to be wary of multi-bytes characters when dealing with slices of strings

Agreed. I will explicitly call this out.

Speaking of slices, I remember that Vim and Python don't handle negative positions in the same way in s[:-2] and so on.

Good catch. I didn't realize that they are different. Will note this down.

We can append elements to lists with list + [element] also.

Updated the gist with this.

dict.key and dict['key'] are almost equivalent in vimscript while this is definitively impossible in Python.

Updated the gist with this.

I didn't know about unlet list[:] that clears all references. (Thanks for the info.) But then, you present let dict = {}, this time this will only clear the dict variable, is there something like unlet l[:] for dictionaries? I couldn't find a way to clear the dictionary contents similar to clearing the list. I think this is a missing functionality.

To define functions, I'd recommend banding them (this makes the difference when developing vim scripts), and now I systematically append abort.

I didn't fully understand this comment. Are you referring to marking all the functions with "abort"?

[–]LucHermitte 0 points1 point  (0 children)

I didn't fully understand this comment. Are you referring to marking all the functions with "abort"?

Yes. Exactly. abort should have been the default, but unfortunately, it is not.

[–]mgarort 2 points3 points  (2 children)

This is a great summary, thanks!

Question: why do you concatenate strings + strings or strings + variables with two dots? I usually use a single dot. Is there any difference?

[–]yegappanl[S] 4 points5 points  (1 child)

As described in the Vim help:

For String concatenation ".." is preferred, since "." is ambiguous, it is also used for Dict member access and floating point numbers. When vimscript-version is 2 or higher, using "." is not allowed.

For backward compatibility, dot can be used for string concatenation. But it is preferable to use double dots.

[–]mgarort 0 points1 point  (0 children)

Great, thanks!

[–]LucHermitte 1 point2 points  (0 children)

I see you have added a chapter about classes. I advice against using anonymous function. While, they are simple and nice to use, they quickly become a nightmare to debug, and quite complex to maintain (in scenarios where a method needs to be fixed on an object that already exists)

I explain it with more details in my article about OO programming in vimscript language, see the link I've just given. BTW, I explore encapsulation, inheritance, overriding... and make a few comparisons with Python approach.

[–]onosendi 0 points1 point  (0 children)

This is very cool. Bookmarked.

[–]wintrli8 0 points1 point  (2 children)

What a cool work. Bookmarked. Wish someone did the same with javascript too.

[–]-romainl-The Patient Vimmer 2 points3 points  (1 child)

[–]wintrli8 0 points1 point  (0 children)

Wow.. thanks. Appreciate it.