Good guide on VIM Scripting?

VimScripting

Vim Problem Overview


I've used VIM for 10+ years, but never really into VIM scripting (always subjectively beliving that this was one area where VIM was weaker than Emacs).

Recently, the realization "in a VIM script, I can execute normal mode commands" made my mind go "a-ha" and suddenly start realizing why parts of VIM script is the way it is.

normal ma10jd'a20kp

is much more elegant than

Marker m = currentLocation();
advanceLines(10);
Buffer b = delete(currentLocation(), m);
advanceLines(-20);
insertBuffer(b);

Now question is -- what's a good resource to learn vim scripting from?

Thanks!

Vim Solutions


Solution 1 - Vim

The best advice I can give is that you read:

:help usr_41.txt

which will give you a good overview of the basics. Then find some things that you want to do and create functions for them. While you're doing this, make heavy use of:

:help function-list

to get an idea of what all the built in functions are. Most of it is fairly similar to other scripting languages (albeit a harsher syntax), so any experience you have of text manipulation in python or whatever will be useful.

Also, look on the vim scripts page and Google to get examples of existing scripts to help expand your knowledge.

Finally (and probably most importantly), don't be afraid to ask on stackoverflow or the Vim mailing list and there'll be plenty of support for any problems you may have.

Solution 2 - Vim

Learn Vimscript the Hard Way is worth checking out too.

http://learnvimscriptthehardway.stevelosh.com/

Solution 3 - Vim

I'm in a similar situation. It's still on my "to read" list, but I just found Scripting the Vim editor today. The articles from IBM's DeveloperWorks are usually very good, so probably worth checking out.

Solution 4 - Vim

Al gave you a good answer. I'd also add vim.wikia that has a few tips related to your question.

On a side note.

I wouldn't say that ma10jd'a20kp is more elegant. I see the following problems:

  • it's quite difficult to maintain (what if the number of lines shall become a parameter?),
  • people like to override keys they never learned to use (this could be fixed with a banged :normal!),
  • some normal-mode commands have strange behaviour on side-case (like <esc> when the cursor is on the first column),
  • it messes the mark a, and the unnamed register -- other scripts, or even the user may expect their values to not be altered by your script.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionanonView Question on Stackoverflow
Solution 1 - VimDrAlView Answer on Stackoverflow
Solution 2 - VimEpeliView Answer on Stackoverflow
Solution 3 - VimWalterView Answer on Stackoverflow
Solution 4 - VimLuc HermitteView Answer on Stackoverflow