The execute command is used to evaluate a string as if it were a Vimscript . Responsible Coding. So far we’ve covered a bunch of Vim commands that let. Functional Programming. We’re going to take a short break now to talk about a.
|Published (Last):||27 September 2014|
|PDF File Size:||9.61 Mb|
|ePub File Size:||20.65 Mb|
|Price:||Free* [*Free Regsitration Required]|
So far we’ve covered a bunch of Vim commands that let you customize Vim quickly. In the next part of the book we’re going to dive into Vimscript as a real programming language, but before we do that I want to talk a bit about how to stay sane while writing large amounts of Vimscript. Vimscript is extremely powerful, but has grown organically over the years into a twisty maze ready to ensnare unwary programmers who enter it.
Options and commands are hars terse and hard to read, and working ths compatibility issues can increase the complexity of your code.
Writing a plugin and allowing for user customization introduces another entire layer above that! Be defensive when writing anything that takes more than a few lines of Vimscript. Add a comment explaining what it does, and if there is vimscriph relevant help topic, mention it in the comment!
Unfortunately this also makes it easy for it to grow out of control and become hard to navigate.
One way to combat this is to use Vim’s code folding capabilities and group lines into sections. If you’ve never used Vim’s folding wag should look into it as soon as you can. Some people myself included find it indispensable in our day to day coding.
First we need to set up folding for Vimscript files. Go ahead and run: Sourcing the file won’t work, because Vim has already set the FileType for this file and the autocommand only fires when that happens.
In the future you won’t need to do it manually.
Return to normal mode, put your cursor on any of those lines and type vimscriph. Typing za again will unfold the lines. You may think that adding explicit comments to source code that describe folding is ugly at first. I thought the same way when I first saw it.
For most files I still think it’s wrong.
Not everyone uses the same editor, so littering your code with folding comments is just noisy to anyone else looking at the code in something other than Vim. Vimscript files are special case, though.
Responsible Coding / Learn Vimscript the Hard Way
It’s highly unlikely that someone who doesn’t use Vim will be reading your code, and it’s especially important to group things explicitly and thoughtfully when writing Vimscript so you don’t go crazy.
Vim allows you to use abbreviated names for most commands and options. For example, both wya these commands do exactly the same thing:. Vimscript is terse and cryptic enough to begin with; shortening things further is only going to make it even harder to read. Even if you know what a certain short command means, someone else reading your code might not.
With that said, the abbreviated forms haed great for running commands manually in the middle of coding. No one will ever see them again after you press return, so there’s no reason to press more keys than you have to.
Some places to start might be: Add folding markers with headings to each section. Find out how to make Vim fold everything automatically the first time you open the file. Commit the repository you just made and put it on Bitbucket or GitHub so other people can see it and get ideas for their own. Be sure to commit and push the repository fairly often so your changes are recorded. If you tge Vim on more than one machine, clone down that repository and symlink the file there as well.
This will make it simple and easy to use the exact same Vim configuration on all machines you work with. Responsible Coding So far we’ve covered a bunch of Vim commands that let you customize Vim quickly.
Commenting Vimscript is extremely powerful, but has grown organically over the years into a twisty maze ready to ensnare unwary programmers who enter it. Now add lines before and after that autocommand group so that it looks like this: Try these explicit folds out for a while.
You might grow to love them. Short Names Vim allows you to use abbreviated names for most commands and options. For example, both of these commands do exactly the same thing: