I'm creating a custom syntax file for vim, and to make the code easier to read, I want to tell vim how to indent the code automatically. Right now, my usual re-indent gg=G''
in vim just gets rid of all the whitespace at the beginning of each line:
IF (@("X_17") = "X") THEN
*Make sure only one is selected
countX = 0;
IF (@("X_19") = "X") THEN
countX += 1;
END
IF (@("X_20") = "X") THEN
countX += 1;
END
IF (@("X_21") = "X") THEN
countX += 1;
END
...
I would like to set it up so that comments (beginning with *) are not indented at all (no whitespace at the beginning of the line), but the rest are indented based on IF and END. The same code, properly indented (assume even the first IF is within an even larger IF/END block):
IF (@("X_17") = "X") THEN
*Make sure only one is selected
countX = 0;
IF (@("X_19") = "X") THEN
countX += 1;
END
IF (@("X_20") = "X") THEN
countX += 1;
END
IF (@("X_21") = "X") THEN
countX += 1;
END
...
I have the syntax higlighting set up, and keywords and built-in functions already part of a dal.vim file. Here is the current dal.vim:
http://pastebin.com/PEcSvqM9
Is there a way to change what I have so that code will automatically indent within the IF/END blocks, while keeping all the comments with no whitespace at the beginning?
Bonus question: How can i set up code folding to follow those IF/END blocks? Right now I am doing zf2j
to fold this code.
You have to write an indentation plugin and put it into the indent
subdirectory. More is explained in
:h 'indentexpr'
, it is what your plugin should set. Feel free to get examples from a hundred of indentation scripts distributed with vim. I did not write such plugins by myself, so can’t say which one will be most useful as an example.
For code folding check out
:h :syn-fold
Ah, and don’t set indent in a syntax file. It is not impossible, but it is just not right place for this type of job.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With