1*8c35d5eeSXin Li<?xml version = '1.0'?> 2*8c35d5eeSXin Li<?xml-stylesheet type="text/xsl" href="styleguide.xsl"?> 3*8c35d5eeSXin Li<GUIDE title="Google Vimscript Guide"> 4*8c35d5eeSXin Li <p class="revision"> 5*8c35d5eeSXin Li 6*8c35d5eeSXin Li Revision 1.1 7*8c35d5eeSXin Li </p> 8*8c35d5eeSXin Li 9*8c35d5eeSXin Li 10*8c35d5eeSXin Li <address> 11*8c35d5eeSXin Li Nate Soares<br/> 12*8c35d5eeSXin Li Joshua Hoak<br/> 13*8c35d5eeSXin Li David Barnett<br/> 14*8c35d5eeSXin Li </address> 15*8c35d5eeSXin Li 16*8c35d5eeSXin Li <OVERVIEW> 17*8c35d5eeSXin Li <CATEGORY title="Background"> 18*8c35d5eeSXin Li <p> 19*8c35d5eeSXin Li This is the in-depth vimscript guide. If you're just a casual user 20*8c35d5eeSXin Li looking to write a plugin, the 21*8c35d5eeSXin Li <a href="vimscriptguide.html">abbreviated style guide</a> is for you. 22*8c35d5eeSXin Li </p> 23*8c35d5eeSXin Li <p> 24*8c35d5eeSXin Li This rather rotund guide dives into justifications and clarifications. 25*8c35d5eeSXin Li It provides an idealized set of rules that are rather too draconian to 26*8c35d5eeSXin Li push on casual scripters. 27*8c35d5eeSXin Li </p> 28*8c35d5eeSXin Li 29*8c35d5eeSXin Li <p> 30*8c35d5eeSXin Li It's for users who want to know why certain decisions were made in the 31*8c35d5eeSXin Li abbreviated guide and who want to learn a thing or two about using 32*8c35d5eeSXin Li vimscript safely. 33*8c35d5eeSXin Li </p> 34*8c35d5eeSXin Li <p> 35*8c35d5eeSXin Li Fair warning: Vimscript is a maddening abyss. When you gaze into it, it 36*8c35d5eeSXin Li gazes also into you. Proceed with caution. 37*8c35d5eeSXin Li </p> 38*8c35d5eeSXin Li </CATEGORY> 39*8c35d5eeSXin Li </OVERVIEW> 40*8c35d5eeSXin Li 41*8c35d5eeSXin Li <CATEGORY title="Portability"> 42*8c35d5eeSXin Li <p> 43*8c35d5eeSXin Li Vim is highly configurable. Users can change many of the default 44*8c35d5eeSXin Li settings, including the case sensitivity, the regular expression rules, 45*8c35d5eeSXin Li the substitution rules, and more. In order for your vimscript to work 46*8c35d5eeSXin Li for all users, follow these guidelines: 47*8c35d5eeSXin Li </p> 48*8c35d5eeSXin Li <ul> 49*8c35d5eeSXin Li <li> 50*8c35d5eeSXin Li Always prefix regular expressions with one of <code>\m</code>, 51*8c35d5eeSXin Li <code>\v</code>, <code>\M</code>, or <code>\V</code> (prefer 52*8c35d5eeSXin Li tersity) 53*8c35d5eeSXin Li <ul> 54*8c35d5eeSXin Li <li> 55*8c35d5eeSXin Li Users can change the global "magic level" of regular expressions. 56*8c35d5eeSXin Li This changes how atoms are parsed in regular expressions, 57*8c35d5eeSXin Li including <code>.</code>, <code>*</code>, and <code>{</code>. 58*8c35d5eeSXin Li </li> 59*8c35d5eeSXin Li <li> 60*8c35d5eeSXin Li Even if your regular expression does not contain characters which 61*8c35d5eeSXin Li are affected by the <code>magic</code> setting you must prefix it 62*8c35d5eeSXin Li with one of the magic control atoms. This future-proofs your 63*8c35d5eeSXin Li regular expression against other devs modifying it and forgetting 64*8c35d5eeSXin Li to add the control atom. 65*8c35d5eeSXin Li </li> 66*8c35d5eeSXin Li <li> 67*8c35d5eeSXin Li If you have no opinion about what type of regular expression to 68*8c35d5eeSXin Li use, prefer the one which makes your regular expression most 69*8c35d5eeSXin Li concise. 70*8c35d5eeSXin Li </li> 71*8c35d5eeSXin Li </ul> 72*8c35d5eeSXin Li </li> 73*8c35d5eeSXin Li <li> 74*8c35d5eeSXin Li Avoid using <code>:s[ubstitute]</code> in scripts. 75*8c35d5eeSXin Li <ul> 76*8c35d5eeSXin Li <li> 77*8c35d5eeSXin Li <code>:substitute</code> moves the cursor. 78*8c35d5eeSXin Li </li> 79*8c35d5eeSXin Li <li> 80*8c35d5eeSXin Li <code>:substitute</code> outputs an error message when the match 81*8c35d5eeSXin Li does not exist. 82*8c35d5eeSXin Li </li> 83*8c35d5eeSXin Li <li> 84*8c35d5eeSXin Li The meaning of the <code>g</code> flag depends upon the 85*8c35d5eeSXin Li <code>gdefault</code> setting. If you do use 86*8c35d5eeSXin Li <code>:substitute</code> you must save <code>gdefault</code>, set 87*8c35d5eeSXin Li it to <code>0</code> or <code>1</code>, perform the substitution, 88*8c35d5eeSXin Li and then restore it. 89*8c35d5eeSXin Li </li> 90*8c35d5eeSXin Li <li> 91*8c35d5eeSXin Li Script authors who want a safe way to replace text in the buffer 92*8c35d5eeSXin Li are encouraged to use <code>maktaba#buffer#Replace</code>. 93*8c35d5eeSXin Li </li> 94*8c35d5eeSXin Li </ul> 95*8c35d5eeSXin Li </li> 96*8c35d5eeSXin Li <li> 97*8c35d5eeSXin Li Always use case-explicit operators for strings (<code>=~#</code> and 98*8c35d5eeSXin Li <code>=~?</code>, never <code>=~</code>). 99*8c35d5eeSXin Li <ul> 100*8c35d5eeSXin Li <li> 101*8c35d5eeSXin Li This also applies to <code>!~ == != > >= <</code> and 102*8c35d5eeSXin Li <code><=</code> 103*8c35d5eeSXin Li </li> 104*8c35d5eeSXin Li <li> 105*8c35d5eeSXin Li This only applies for strings. <code>==</code> and 106*8c35d5eeSXin Li <code>>=</code> are fine for numbers, but <code>==#</code> and 107*8c35d5eeSXin Li <code>>=#</code> must be used for strings. 108*8c35d5eeSXin Li </li> 109*8c35d5eeSXin Li <li> 110*8c35d5eeSXin Li The behavior of <code>=~</code> and friends is dependant upon the 111*8c35d5eeSXin Li <code>ignorecase</code> setting. 112*8c35d5eeSXin Li </li> 113*8c35d5eeSXin Li <li> 114*8c35d5eeSXin Li You may break this rule when you explicitly want to obey the 115*8c35d5eeSXin Li user's <code>ignorecase</code> setting. Be prepared to justify 116*8c35d5eeSXin Li your reasoning. 117*8c35d5eeSXin Li </li> 118*8c35d5eeSXin Li </ul> 119*8c35d5eeSXin Li </li> 120*8c35d5eeSXin Li <li> 121*8c35d5eeSXin Li When using regular expressions as arguments to functions, prepend them 122*8c35d5eeSXin Li with <code>\c</code> or <code>\C</code>. 123*8c35d5eeSXin Li <ul> 124*8c35d5eeSXin Li <li> 125*8c35d5eeSXin Li This forces case to be either explicitly matched or ignored. 126*8c35d5eeSXin Li </li> 127*8c35d5eeSXin Li <li> 128*8c35d5eeSXin Li This is recommended, but not required, when comparing regexes with 129*8c35d5eeSXin Li operators that specify case sensitivity (<code>=~#</code>, etc.). 130*8c35d5eeSXin Li </li> 131*8c35d5eeSXin Li <li> 132*8c35d5eeSXin Li This rule applies when your regexes are matching syntax, external 133*8c35d5eeSXin Li APIs, external messages, and most other cases. 134*8c35d5eeSXin Li </li> 135*8c35d5eeSXin Li <li> 136*8c35d5eeSXin Li It does not apply when matching text in the buffer. When matching 137*8c35d5eeSXin Li text in the buffer you should honor the <code>ignorecase</code> 138*8c35d5eeSXin Li setting. 139*8c35d5eeSXin Li </li> 140*8c35d5eeSXin Li <li> 141*8c35d5eeSXin Li You may also ignore this rule any time that you explicitly want to 142*8c35d5eeSXin Li honor the <code>ignorecase</code> setting. Be prepared to justify 143*8c35d5eeSXin Li your reasoning. 144*8c35d5eeSXin Li </li> 145*8c35d5eeSXin Li </ul> 146*8c35d5eeSXin Li </li> 147*8c35d5eeSXin Li <li> 148*8c35d5eeSXin Li Always use <code>normal!</code> instead of <code>normal</code>. 149*8c35d5eeSXin Li <ul> 150*8c35d5eeSXin Li <li> 151*8c35d5eeSXin Li If you forgo the <code>!</code> the command will use the user's 152*8c35d5eeSXin Li key mappings and you have literally no idea what your macro will 153*8c35d5eeSXin Li do. 154*8c35d5eeSXin Li </li> 155*8c35d5eeSXin Li </ul> 156*8c35d5eeSXin Li </li> 157*8c35d5eeSXin Li <li> 158*8c35d5eeSXin Li Always use the <code>noremap</code> family of commands. 159*8c35d5eeSXin Li <ul> 160*8c35d5eeSXin Li <li> 161*8c35d5eeSXin Li Your plugins generally shouldn't introduce mappings, but if they 162*8c35d5eeSXin Li do, the <code>map</code> command respects the users existing 163*8c35d5eeSXin Li mappings and could do anything. 164*8c35d5eeSXin Li </li> 165*8c35d5eeSXin Li </ul> 166*8c35d5eeSXin Li </li> 167*8c35d5eeSXin Li <li> 168*8c35d5eeSXin Li When using <code>catch</code>, match the error code rather than the 169*8c35d5eeSXin Li error text. 170*8c35d5eeSXin Li <ul> 171*8c35d5eeSXin Li <li> 172*8c35d5eeSXin Li The error text may be locale-dependant. 173*8c35d5eeSXin Li </li> 174*8c35d5eeSXin Li <li> 175*8c35d5eeSXin Li See <code>:help error-messages</code>. 176*8c35d5eeSXin Li </li> 177*8c35d5eeSXin Li </ul> 178*8c35d5eeSXin Li </li> 179*8c35d5eeSXin Li </ul> 180*8c35d5eeSXin Li <p> 181*8c35d5eeSXin Li In general, guard all commands and functions against user settings. 182*8c35d5eeSXin Li </p> 183*8c35d5eeSXin Li 184*8c35d5eeSXin Li </CATEGORY> 185*8c35d5eeSXin Li <CATEGORY title="Language Guide"> 186*8c35d5eeSXin Li <ul> 187*8c35d5eeSXin Li 188*8c35d5eeSXin Li 189*8c35d5eeSXin Li <li> 190*8c35d5eeSXin Li Line continuations: <strong>Yes</strong> 191*8c35d5eeSXin Li <ul> 192*8c35d5eeSXin Li 193*8c35d5eeSXin Li <li> 194*8c35d5eeSXin Li Plugins that support vi compatibility mode must save and restore 195*8c35d5eeSXin Li compatibility options as described in the 196*8c35d5eeSXin Li <strong>Errata section</strong> so line continuations work properly. 197*8c35d5eeSXin Li </li> 198*8c35d5eeSXin Li </ul> 199*8c35d5eeSXin Li </li> 200*8c35d5eeSXin Li <li> 201*8c35d5eeSXin Li Exceptions: <strong>Yes, with caution</strong> 202*8c35d5eeSXin Li <ul> 203*8c35d5eeSXin Li <li> 204*8c35d5eeSXin Li Always use an error code in thrown exception messages. 205*8c35d5eeSXin Li </li> 206*8c35d5eeSXin Li <li> 207*8c35d5eeSXin Li Prefer the <code>maktaba#error</code> codes found in 208*8c35d5eeSXin Li <code>maktaba</code>. 209*8c35d5eeSXin Li </li> 210*8c35d5eeSXin Li <li> 211*8c35d5eeSXin Li Fall back to the vim error codes. See 212*8c35d5eeSXin Li <code>:help error-messages</code>. 213*8c35d5eeSXin Li </li> 214*8c35d5eeSXin Li <li> 215*8c35d5eeSXin Li Generate custom error messages using 216*8c35d5eeSXin Li <code>maktaba#error#Message</code>. 217*8c35d5eeSXin Li </li> 218*8c35d5eeSXin Li </ul> 219*8c35d5eeSXin Li </li> 220*8c35d5eeSXin Li <li> 221*8c35d5eeSXin Li Global Variables: <strong>As configuration only</strong> 222*8c35d5eeSXin Li <ul> 223*8c35d5eeSXin Li <li> 224*8c35d5eeSXin Li See the plugin guide. 225*8c35d5eeSXin Li </li> 226*8c35d5eeSXin Li </ul> 227*8c35d5eeSXin Li </li> 228*8c35d5eeSXin Li <li> 229*8c35d5eeSXin Li Messaging: <strong>As little as possible.</strong> 230*8c35d5eeSXin Li <ul> 231*8c35d5eeSXin Li <li> 232*8c35d5eeSXin Li Loud scripts are annoying. 233*8c35d5eeSXin Li </li> 234*8c35d5eeSXin Li <li> 235*8c35d5eeSXin Li Message the user when an error has occured. 236*8c35d5eeSXin Li </li> 237*8c35d5eeSXin Li <li> 238*8c35d5eeSXin Li Message the user when an operation which takes a long time has 239*8c35d5eeSXin Li begun work. 240*8c35d5eeSXin Li </li> 241*8c35d5eeSXin Li <li> 242*8c35d5eeSXin Li Avoid messaging otherwise. 243*8c35d5eeSXin Li </li> 244*8c35d5eeSXin Li </ul> 245*8c35d5eeSXin Li </li> 246*8c35d5eeSXin Li <li> 247*8c35d5eeSXin Li Type checking: 248*8c35d5eeSXin Li <strong>Use strict and explicit checks where possible.</strong> 249*8c35d5eeSXin Li <ul> 250*8c35d5eeSXin Li <li> 251*8c35d5eeSXin Li Vimscript has unsafe, unintuitive behavior when dealing with some 252*8c35d5eeSXin Li types. For instance, <code>0 == 'foo'</code> evaluates to true. 253*8c35d5eeSXin Li </li> 254*8c35d5eeSXin Li <li> 255*8c35d5eeSXin Li Use strict comparison operators where possible. When comparing 256*8c35d5eeSXin Li against a string literal, use the <code>is#</code> operator. 257*8c35d5eeSXin Li Otherwise, prefer <code>maktaba#value#IsEqual</code> or check 258*8c35d5eeSXin Li <code>type()</code> explicitly. 259*8c35d5eeSXin Li </li> 260*8c35d5eeSXin Li <li> 261*8c35d5eeSXin Li Check variable types explicitly before using them. Use functions 262*8c35d5eeSXin Li from <code>maktaba#ensure</code>, or check 263*8c35d5eeSXin Li <code>maktaba#value</code> or <code>type()</code> and throw your own 264*8c35d5eeSXin Li errors. 265*8c35d5eeSXin Li </li> 266*8c35d5eeSXin Li <li> 267*8c35d5eeSXin Li Use <code>:unlet</code> for variables that may change types, 268*8c35d5eeSXin Li particularly those assigned inside loops. 269*8c35d5eeSXin Li </li> 270*8c35d5eeSXin Li </ul> 271*8c35d5eeSXin Li </li> 272*8c35d5eeSXin Li <li> 273*8c35d5eeSXin Li FuncRefs: <strong>No in most cases.</strong> 274*8c35d5eeSXin Li <ul> 275*8c35d5eeSXin Li <li> 276*8c35d5eeSXin Li FuncRefs have inconsistently enforced naming restrictions. 277*8c35d5eeSXin Li (Functions can have names that FuncRefs can not.) 278*8c35d5eeSXin Li </li> 279*8c35d5eeSXin Li <li> 280*8c35d5eeSXin Li FuncRefs have inconsistent ability to be reassigned (in Vim 281*8c35d5eeSXin Li 7.2 and before you must unlet a FuncRef before assigning it). 282*8c35d5eeSXin Li </li> 283*8c35d5eeSXin Li <li> 284*8c35d5eeSXin Li In most instances where a FuncRef is needed a string works 285*8c35d5eeSXin Li just as well: just pass the string that you would use to make 286*8c35d5eeSXin Li the FuncRef. 287*8c35d5eeSXin Li </li> 288*8c35d5eeSXin Li <li> 289*8c35d5eeSXin Li Consider using <code>maktaba#function</code> instead to create and 290*8c35d5eeSXin Li manipulate handles to functions. 291*8c35d5eeSXin Li </li> 292*8c35d5eeSXin Li </ul> 293*8c35d5eeSXin Li </li> 294*8c35d5eeSXin Li <li> 295*8c35d5eeSXin Li Python: <strong>Sparingly</strong> 296*8c35d5eeSXin Li <ul> 297*8c35d5eeSXin Li 298*8c35d5eeSXin Li <li> 299*8c35d5eeSXin Li Hurts code reuse since python code embedded in python plugins is 300*8c35d5eeSXin Li awkward to share between plugins. 301*8c35d5eeSXin Li </li> 302*8c35d5eeSXin Li <li> 303*8c35d5eeSXin Li Using python introduces python language version dependencies, which 304*8c35d5eeSXin Li are likely to get stale. 305*8c35d5eeSXin Li </li> 306*8c35d5eeSXin Li <li> 307*8c35d5eeSXin Li Exception: It's reasonable to use python for plugin functionality 308*8c35d5eeSXin Li that needs to do work in the background, as vimscript can not do 309*8c35d5eeSXin Li this. 310*8c35d5eeSXin Li </li> 311*8c35d5eeSXin Li </ul> 312*8c35d5eeSXin Li </li> 313*8c35d5eeSXin Li <li> 314*8c35d5eeSXin Li Ruby: <strong>No</strong> 315*8c35d5eeSXin Li <ul> 316*8c35d5eeSXin Li <li> 317*8c35d5eeSXin Li We can not assume ruby interoperability. 318*8c35d5eeSXin Li </li> 319*8c35d5eeSXin Li <li> 320*8c35d5eeSXin Li You shouldn't depend upon the version of the ruby language that the 321*8c35d5eeSXin Li user has installed. 322*8c35d5eeSXin Li </li> 323*8c35d5eeSXin Li </ul> 324*8c35d5eeSXin Li </li> 325*8c35d5eeSXin Li <li> 326*8c35d5eeSXin Li Lua: <strong>No</strong> 327*8c35d5eeSXin Li <ul> 328*8c35d5eeSXin Li <li> 329*8c35d5eeSXin Li For the same reasons an Ruby. 330*8c35d5eeSXin Li </li> 331*8c35d5eeSXin Li </ul> 332*8c35d5eeSXin Li </li> 333*8c35d5eeSXin Li <li> 334*8c35d5eeSXin Li Dict Functions: <strong>Encouraged</strong> 335*8c35d5eeSXin Li <ul> 336*8c35d5eeSXin Li <li> 337*8c35d5eeSXin Li Vimscript can attach functions to dictionaries. Such functions 338*8c35d5eeSXin Li have access to the <code>self</code> parameter which access 339*8c35d5eeSXin Li the dict state. 340*8c35d5eeSXin Li </li> 341*8c35d5eeSXin Li <li> 342*8c35d5eeSXin Li Use these where you would use a class in python. 343*8c35d5eeSXin Li </li> 344*8c35d5eeSXin Li <li> 345*8c35d5eeSXin Li Do not over-use this feature; it is not necessary for helper 346*8c35d5eeSXin Li functions or API functions, only for encapsulated objects. 347*8c35d5eeSXin Li </li> 348*8c35d5eeSXin Li </ul> 349*8c35d5eeSXin Li </li> 350*8c35d5eeSXin Li </ul> 351*8c35d5eeSXin Li <p> 352*8c35d5eeSXin Li All other language features are fair game. 353*8c35d5eeSXin Li </p> 354*8c35d5eeSXin Li </CATEGORY> 355*8c35d5eeSXin Li <CATEGORY title="Structure"> 356*8c35d5eeSXin Li <ul> 357*8c35d5eeSXin Li <li> 358*8c35d5eeSXin Li Provided functionality should be packed into modular plugins. 359*8c35d5eeSXin Li <ul> 360*8c35d5eeSXin Li <li> 361*8c35d5eeSXin Li Every function in your plugin should be specific to your 362*8c35d5eeSXin Li plugin. 363*8c35d5eeSXin Li </li> 364*8c35d5eeSXin Li <li> 365*8c35d5eeSXin Li General utility functions should be abstracted into library plugins. 366*8c35d5eeSXin Li </li> 367*8c35d5eeSXin Li <li> 368*8c35d5eeSXin Li Manage dependencies with <code>maktaba</code>. 369*8c35d5eeSXin Li </li> 370*8c35d5eeSXin Li </ul> 371*8c35d5eeSXin Li </li> 372*8c35d5eeSXin Li <li> 373*8c35d5eeSXin Li <code>plugin-names-like-this</code> 374*8c35d5eeSXin Li <ul> 375*8c35d5eeSXin Li <li> 376*8c35d5eeSXin Li Plugin names should be descriptive and concise. 377*8c35d5eeSXin Li </li> 378*8c35d5eeSXin Li 379*8c35d5eeSXin Li 380*8c35d5eeSXin Li </ul> 381*8c35d5eeSXin Li </li> 382*8c35d5eeSXin Li <li> 383*8c35d5eeSXin Li Each plugin must consist of one directory (or code repository), sharing 384*8c35d5eeSXin Li a name with the plugin (with a "vim-" prefix or ".vim" suffix if 385*8c35d5eeSXin Li desired). 386*8c35d5eeSXin Li </li> 387*8c35d5eeSXin Li <li> 388*8c35d5eeSXin Li Plugin metadata should be declared in the addon-info.json format (see 389*8c35d5eeSXin Li the <a href="https://github.com/MarcWeber/vim-addon-manager/blob/master/doc/vim-addon-manager-additional-documentation.txt">VAM documentation</a> for details). 390*8c35d5eeSXin Li </li> 391*8c35d5eeSXin Li <li> 392*8c35d5eeSXin Li Functions should go in the <code>autoload/</code> subdirectory of 393*8c35d5eeSXin Li your plugin. 394*8c35d5eeSXin Li <ul> 395*8c35d5eeSXin Li <li> 396*8c35d5eeSXin Li This allows them to be late-loaded, which speeds up startup 397*8c35d5eeSXin Li time. 398*8c35d5eeSXin Li </li> 399*8c35d5eeSXin Li <li> 400*8c35d5eeSXin Li This helps vim enforce namespacing conventions. 401*8c35d5eeSXin Li </li> 402*8c35d5eeSXin Li </ul> 403*8c35d5eeSXin Li </li> 404*8c35d5eeSXin Li <li> 405*8c35d5eeSXin Li Each file in the <code>plugin/</code> or <code>instant/</code> directory 406*8c35d5eeSXin Li should begin with the boilerplate 407*8c35d5eeSXin Li <CODE_SNIPPET> 408*8c35d5eeSXin Li let [s:plugin, s:enter] = maktaba#plugin#Enter(expand('<sfile>:p')) 409*8c35d5eeSXin Li if !s:enter 410*8c35d5eeSXin Li finish 411*8c35d5eeSXin Li endif 412*8c35d5eeSXin Li </CODE_SNIPPET> 413*8c35d5eeSXin Li (This prevents re-entry and allows users to selectively disable 414*8c35d5eeSXin Li functionality.) 415*8c35d5eeSXin Li </li> 416*8c35d5eeSXin Li <li> 417*8c35d5eeSXin Li User configuration should be via plugin flags defined in 418*8c35d5eeSXin Li <code>instant/flags.vim</code>. 419*8c35d5eeSXin Li <ul> 420*8c35d5eeSXin Li <li> 421*8c35d5eeSXin Li Define flags with 422*8c35d5eeSXin Li <code>call s:plugin.Flag('FLAGNAME', DEFAULT_VALUE)</code>. 423*8c35d5eeSXin Li </li> 424*8c35d5eeSXin Li <li> 425*8c35d5eeSXin Li Users can configure these flags using the <code>:Glaive</code> 426*8c35d5eeSXin Li command (see <a href="https://github.com/google/glaive">glaive</a>). 427*8c35d5eeSXin Li </li> 428*8c35d5eeSXin Li </ul> 429*8c35d5eeSXin Li </li> 430*8c35d5eeSXin Li <li> 431*8c35d5eeSXin Li Commands, autocommands, mappings, and settings changes should 432*8c35d5eeSXin Li occur either in the <code>plugin/</code> or the 433*8c35d5eeSXin Li <code>ftplugin/</code> subdirectories. 434*8c35d5eeSXin Li <ul> 435*8c35d5eeSXin Li <li> 436*8c35d5eeSXin Li All commands should be defined in <code>plugin/commands.vim</code> 437*8c35d5eeSXin Li or <code>ftplugin/</code> files. 438*8c35d5eeSXin Li </li> 439*8c35d5eeSXin Li <li> 440*8c35d5eeSXin Li Autocommands should be defined in <code>plugin/autocmds.vim</code>, 441*8c35d5eeSXin Li inside an augroup. 442*8c35d5eeSXin Li </li> 443*8c35d5eeSXin Li <li> 444*8c35d5eeSXin Li Mappings should be defined in <code>plugin/mappings.vim</code> and 445*8c35d5eeSXin Li will be disabled unless explicitly enabled by users. 446*8c35d5eeSXin Li </li> 447*8c35d5eeSXin Li <li> 448*8c35d5eeSXin Li If the plugin configures any standard vim settings, those should be 449*8c35d5eeSXin Li configured in <code>plugin/settings.vim</code> or 450*8c35d5eeSXin Li <code>instant/settings.vim</code>. 451*8c35d5eeSXin Li </li> 452*8c35d5eeSXin Li </ul> 453*8c35d5eeSXin Li </li> 454*8c35d5eeSXin Li <li> 455*8c35d5eeSXin Li Avoid using the <code>after/</code> subdirectory. 456*8c35d5eeSXin Li <ul> 457*8c35d5eeSXin Li <li> 458*8c35d5eeSXin Li <code>after/</code> should be reserved for the user. 459*8c35d5eeSXin Li </li> 460*8c35d5eeSXin Li <li> 461*8c35d5eeSXin Li It is difficult for the user to add their own overrides when 462*8c35d5eeSXin Li plugins use <code>after/</code>. 463*8c35d5eeSXin Li </li> 464*8c35d5eeSXin Li </ul> 465*8c35d5eeSXin Li </li> 466*8c35d5eeSXin Li </ul> 467*8c35d5eeSXin Li 468*8c35d5eeSXin Li <STYLEPOINT title="Libraries vs. Functionality"> 469*8c35d5eeSXin Li <SUMMARY> 470*8c35d5eeSXin Li Separate library-providing plugins from command-providing plugins. 471*8c35d5eeSXin Li </SUMMARY> 472*8c35d5eeSXin Li <BODY> 473*8c35d5eeSXin Li <p> 474*8c35d5eeSXin Li Many plugins provide either user functionality (commands, 475*8c35d5eeSXin Li autocommands, etc) or an API (of autoloaded functions) but not both. 476*8c35d5eeSXin Li This separation is encouraged, as it allows other plugins to pull in a 477*8c35d5eeSXin Li library without also pulling in commands, setting changes, and other 478*8c35d5eeSXin Li plugin functionality that affects the end user. 479*8c35d5eeSXin Li </p> 480*8c35d5eeSXin Li </BODY> 481*8c35d5eeSXin Li </STYLEPOINT> 482*8c35d5eeSXin Li 483*8c35d5eeSXin Li <STYLEPOINT title="Configuration"> 484*8c35d5eeSXin Li <SUMMARY> 485*8c35d5eeSXin Li Don't clobber user settings. Provide as much configurability as 486*8c35d5eeSXin Li possible: that's what Vim's all about. 487*8c35d5eeSXin Li </SUMMARY> 488*8c35d5eeSXin Li <BODY> 489*8c35d5eeSXin Li <ul> 490*8c35d5eeSXin Li <li> 491*8c35d5eeSXin Li Use maktaba flags for plugin configuration. Users can configure them 492*8c35d5eeSXin Li using the <code>:Glaive</code> command. 493*8c35d5eeSXin Li 494*8c35d5eeSXin Li </li> 495*8c35d5eeSXin Li <li> 496*8c35d5eeSXin Li Check if configuration variables exist before setting them. 497*8c35d5eeSXin Li <CODE_SNIPPET> 498*8c35d5eeSXin Li if !exists('g:myplugin_option') 499*8c35d5eeSXin Li let g:myplugin_option = 1 500*8c35d5eeSXin Li endif 501*8c35d5eeSXin Li </CODE_SNIPPET> 502*8c35d5eeSXin Li </li> 503*8c35d5eeSXin Li </ul> 504*8c35d5eeSXin Li </BODY> 505*8c35d5eeSXin Li </STYLEPOINT> 506*8c35d5eeSXin Li </CATEGORY> 507*8c35d5eeSXin Li <CATEGORY title="Style Guide"> 508*8c35d5eeSXin Li <p> 509*8c35d5eeSXin Li Follow google-wide style conventions. Mimic google python style when 510*8c35d5eeSXin Li in doubt. 511*8c35d5eeSXin Li </p> 512*8c35d5eeSXin Li 513*8c35d5eeSXin Li 514*8c35d5eeSXin Li 515*8c35d5eeSXin Li <STYLEPOINT title="Documentation"> 516*8c35d5eeSXin Li <SUMMARY> 517*8c35d5eeSXin Li Use <a href="https://github.com/google/vimdoc">vimdoc</a>. 518*8c35d5eeSXin Li </SUMMARY> 519*8c35d5eeSXin Li <BODY> 520*8c35d5eeSXin Li <p> 521*8c35d5eeSXin Li Provide help files generated by 522*8c35d5eeSXin Li <a href="https://github.com/google/vimdoc">vimdoc</a>. Write 523*8c35d5eeSXin Li documentation in .vim files in conformance with the vimdoc standards 524*8c35d5eeSXin Li and include fields like "description" and "author" in the 525*8c35d5eeSXin Li addon-info.json file (see the 526*8c35d5eeSXin Li <a href="https://github.com/MarcWeber/vim-addon-manager/blob/master/doc/vim-addon-manager-additional-documentation.txt">VAM documentation</a>). 527*8c35d5eeSXin Li </p> 528*8c35d5eeSXin Li </BODY> 529*8c35d5eeSXin Li </STYLEPOINT> 530*8c35d5eeSXin Li 531*8c35d5eeSXin Li <STYLEPOINT title="Whitespace"> 532*8c35d5eeSXin Li <SUMMARY> 533*8c35d5eeSXin Li Follow google-wide conventions. 534*8c35d5eeSXin Li </SUMMARY> 535*8c35d5eeSXin Li <BODY> 536*8c35d5eeSXin Li <ul> 537*8c35d5eeSXin Li <li> 538*8c35d5eeSXin Li Use two spaces for indents. 539*8c35d5eeSXin Li </li> 540*8c35d5eeSXin Li <li> 541*8c35d5eeSXin Li Do not use tabs. 542*8c35d5eeSXin Li </li> 543*8c35d5eeSXin Li <li> 544*8c35d5eeSXin Li Use spaces around operators except for arguments to commands. 545*8c35d5eeSXin Li <ul> 546*8c35d5eeSXin Li <li> 547*8c35d5eeSXin Li Using spaces around operators for commands is often invalid 548*8c35d5eeSXin Li syntax. This is inconsistently enforced by vimscript. To be 549*8c35d5eeSXin Li safe, always omit whitespace around arguments to commands. 550*8c35d5eeSXin Li </li> 551*8c35d5eeSXin Li <li> 552*8c35d5eeSXin Li <CODE_SNIPPET> 553*8c35d5eeSXin Li let s:variable = "concatenated " . "strings" 554*8c35d5eeSXin Li command -range=% MyCommand 555*8c35d5eeSXin Li </CODE_SNIPPET> 556*8c35d5eeSXin Li <BAD_CODE_SNIPPET> 557*8c35d5eeSXin Li let s:variable="concatenated "."strings" 558*8c35d5eeSXin Li command -range = % MyCommand 559*8c35d5eeSXin Li </BAD_CODE_SNIPPET> 560*8c35d5eeSXin Li </li> 561*8c35d5eeSXin Li </ul> 562*8c35d5eeSXin Li </li> 563*8c35d5eeSXin Li <li> 564*8c35d5eeSXin Li Do not introduce trailing whitespace. 565*8c35d5eeSXin Li <ul> 566*8c35d5eeSXin Li <li> 567*8c35d5eeSXin Li You need not go out of your way to remove it. 568*8c35d5eeSXin Li </li> 569*8c35d5eeSXin Li </ul> 570*8c35d5eeSXin Li </li> 571*8c35d5eeSXin Li <li> 572*8c35d5eeSXin Li Restrict lines to 80 columns wide. 573*8c35d5eeSXin Li </li> 574*8c35d5eeSXin Li <li> 575*8c35d5eeSXin Li Indent continued lines by two tabs (four spaces). 576*8c35d5eeSXin Li </li> 577*8c35d5eeSXin Li <li> 578*8c35d5eeSXin Li Do not waste whitespace aligning common segments of similar 579*8c35d5eeSXin Li commands. It is both difficult and expensive to maintain. 580*8c35d5eeSXin Li <ul> 581*8c35d5eeSXin Li <li> 582*8c35d5eeSXin Li <CODE_SNIPPET> 583*8c35d5eeSXin Li command -bang MyCommand call myplugin#foo() 584*8c35d5eeSXin Li command MyCommand2 call myplugin#bar() 585*8c35d5eeSXin Li </CODE_SNIPPET> 586*8c35d5eeSXin Li <BAD_CODE_SNIPPET> 587*8c35d5eeSXin Li command -bang MyCommand call myplugin#foo() 588*8c35d5eeSXin Li command MyCommand2 call myplugin#bar() 589*8c35d5eeSXin Li </BAD_CODE_SNIPPET> 590*8c35d5eeSXin Li </li> 591*8c35d5eeSXin Li </ul> 592*8c35d5eeSXin Li </li> 593*8c35d5eeSXin Li </ul> 594*8c35d5eeSXin Li <SUBSECTION title="Line Continuations"> 595*8c35d5eeSXin Li <ul start="7"> 596*8c35d5eeSXin Li <li> 597*8c35d5eeSXin Li Prefer line continuations on semantic boundaries. 598*8c35d5eeSXin Li <ul> 599*8c35d5eeSXin Li <li> 600*8c35d5eeSXin Li <CODE_SNIPPET> 601*8c35d5eeSXin Li command SomeLongCommand 602*8c35d5eeSXin Li \ call some#function() 603*8c35d5eeSXin Li </CODE_SNIPPET> 604*8c35d5eeSXin Li <BAD_CODE_SNIPPET> 605*8c35d5eeSXin Li command SomeLongCommand call 606*8c35d5eeSXin Li \ some#function() 607*8c35d5eeSXin Li </BAD_CODE_SNIPPET> 608*8c35d5eeSXin Li </li> 609*8c35d5eeSXin Li <li> 610*8c35d5eeSXin Li Use your best judgement. 611*8c35d5eeSXin Li </li> 612*8c35d5eeSXin Li </ul> 613*8c35d5eeSXin Li </li> 614*8c35d5eeSXin Li <li> 615*8c35d5eeSXin Li Place one space after the backslash denoting a line continuation. 616*8c35d5eeSXin Li <ul> 617*8c35d5eeSXin Li <li> 618*8c35d5eeSXin Li When continuing a multi-line command a pipe can be substituted 619*8c35d5eeSXin Li for this space as necessary, as follows: 620*8c35d5eeSXin Li <CODE_SNIPPET> 621*8c35d5eeSXin Li autocommand BufEnter <buffer> 622*8c35d5eeSXin Li \ if !empty(s:var) 623*8c35d5eeSXin Li \| call some#function() 624*8c35d5eeSXin Li \|else 625*8c35d5eeSXin Li \| call some#function(s:var) 626*8c35d5eeSXin Li \|endif 627*8c35d5eeSXin Li </CODE_SNIPPET> 628*8c35d5eeSXin Li </li> 629*8c35d5eeSXin Li </ul> 630*8c35d5eeSXin Li </li> 631*8c35d5eeSXin Li <li> 632*8c35d5eeSXin Li Do not continue multi-line commands when you can avoid it. Prefer 633*8c35d5eeSXin Li function calls. 634*8c35d5eeSXin Li </li> 635*8c35d5eeSXin Li </ul> 636*8c35d5eeSXin Li </SUBSECTION> 637*8c35d5eeSXin Li <SUBSECTION title="Comments"> 638*8c35d5eeSXin Li <ul> 639*8c35d5eeSXin Li <li> 640*8c35d5eeSXin Li Place a space after the <code>"</code> before the comment text. 641*8c35d5eeSXin Li <ul> 642*8c35d5eeSXin Li <li> 643*8c35d5eeSXin Li <CODE_SNIPPET> 644*8c35d5eeSXin Li " I am a line comment. 645*8c35d5eeSXin Li call call(s:my_function) 646*8c35d5eeSXin Li </CODE_SNIPPET> 647*8c35d5eeSXin Li </li> 648*8c35d5eeSXin Li </ul> 649*8c35d5eeSXin Li </li> 650*8c35d5eeSXin Li <li> 651*8c35d5eeSXin Li Do not use inline comments. 652*8c35d5eeSXin Li <ul> 653*8c35d5eeSXin Li <li> 654*8c35d5eeSXin Li Some commands treat them as comments and others as unclosed 655*8c35d5eeSXin Li quotes. There are many edge cases. It's difficult to get 656*8c35d5eeSXin Li right and difficult to maintain. 657*8c35d5eeSXin Li </li> 658*8c35d5eeSXin Li <li> 659*8c35d5eeSXin Li Where you would use an inline comment, put a line comment on 660*8c35d5eeSXin Li the line above. 661*8c35d5eeSXin Li </li> 662*8c35d5eeSXin Li </ul> 663*8c35d5eeSXin Li </li> 664*8c35d5eeSXin Li <li> 665*8c35d5eeSXin Li When leaving blank lines in comments, include the quote in the 666*8c35d5eeSXin Li blank line. 667*8c35d5eeSXin Li <ul> 668*8c35d5eeSXin Li <li> 669*8c35d5eeSXin Li <CODE_SNIPPET> 670*8c35d5eeSXin Li " I am one continuous 671*8c35d5eeSXin Li " 672*8c35d5eeSXin Li " comment block 673*8c35d5eeSXin Li </CODE_SNIPPET> 674*8c35d5eeSXin Li </li> 675*8c35d5eeSXin Li </ul> 676*8c35d5eeSXin Li </li> 677*8c35d5eeSXin Li </ul> 678*8c35d5eeSXin Li </SUBSECTION> 679*8c35d5eeSXin Li </BODY> 680*8c35d5eeSXin Li </STYLEPOINT> 681*8c35d5eeSXin Li 682*8c35d5eeSXin Li <STYLEPOINT title="Variables"> 683*8c35d5eeSXin Li <SUMMARY> 684*8c35d5eeSXin Li <p> 685*8c35d5eeSXin Li <code>plugin-names-like-this</code>, 686*8c35d5eeSXin Li <code>FunctionNamesLikeThis</code>, 687*8c35d5eeSXin Li <code>CommandNamesLikeThis</code>, 688*8c35d5eeSXin Li <code>augroup_names_like_this</code>, 689*8c35d5eeSXin Li <code>variable_names_like_this</code>. 690*8c35d5eeSXin Li </p> 691*8c35d5eeSXin Li <p> 692*8c35d5eeSXin Li Prefix all variables with their scope. 693*8c35d5eeSXin Li </p> 694*8c35d5eeSXin Li </SUMMARY> 695*8c35d5eeSXin Li <BODY> 696*8c35d5eeSXin Li <ul> 697*8c35d5eeSXin Li <li> 698*8c35d5eeSXin Li <code>variable_names_like_this</code> 699*8c35d5eeSXin Li <ul> 700*8c35d5eeSXin Li <li> 701*8c35d5eeSXin Li FuncRef variables count as functions and should be named like 702*8c35d5eeSXin Li functions. 703*8c35d5eeSXin Li </li> 704*8c35d5eeSXin Li <li> 705*8c35d5eeSXin Li This (pathological) convention is enforced by vim itself. 706*8c35d5eeSXin Li </li> 707*8c35d5eeSXin Li </ul> 708*8c35d5eeSXin Li </li> 709*8c35d5eeSXin Li <li> 710*8c35d5eeSXin Li Prefix global variables with <code>g:</code> 711*8c35d5eeSXin Li <ul> 712*8c35d5eeSXin Li <li> 713*8c35d5eeSXin Li Vimscript allows you to create global variables without 714*8c35d5eeSXin Li prefixing them. 715*8c35d5eeSXin Li </li> 716*8c35d5eeSXin Li <li> 717*8c35d5eeSXin Li It is very bad practice to introduce non-prefixed global 718*8c35d5eeSXin Li variables into scope. 719*8c35d5eeSXin Li </li> 720*8c35d5eeSXin Li <li> 721*8c35d5eeSXin Li Global variables should only be used for plugin configuration. 722*8c35d5eeSXin Li </li> 723*8c35d5eeSXin Li <li> 724*8c35d5eeSXin Li This does not apply to functions defined in 725*8c35d5eeSXin Li <code>autoload</code> directories. 726*8c35d5eeSXin Li </li> 727*8c35d5eeSXin Li </ul> 728*8c35d5eeSXin Li </li> 729*8c35d5eeSXin Li <li> 730*8c35d5eeSXin Li Prefix script-local variables with <code>s:</code> 731*8c35d5eeSXin Li <ul> 732*8c35d5eeSXin Li <li> 733*8c35d5eeSXin Li This prevents namespace collisions between plugins. 734*8c35d5eeSXin Li </li> 735*8c35d5eeSXin Li <li> 736*8c35d5eeSXin Li This also applies to script-local functions. 737*8c35d5eeSXin Li </li> 738*8c35d5eeSXin Li </ul> 739*8c35d5eeSXin Li </li> 740*8c35d5eeSXin Li <li> 741*8c35d5eeSXin Li Prefix function arguments with <code>a:</code> 742*8c35d5eeSXin Li <ul> 743*8c35d5eeSXin Li <li> 744*8c35d5eeSXin Li This is enforced by vim itself. 745*8c35d5eeSXin Li </li> 746*8c35d5eeSXin Li </ul> 747*8c35d5eeSXin Li </li> 748*8c35d5eeSXin Li <li> 749*8c35d5eeSXin Li Prefix function-local variables with <code>l:</code> 750*8c35d5eeSXin Li <ul> 751*8c35d5eeSXin Li <li> 752*8c35d5eeSXin Li This is not enforced by vimscript but is good practice. 753*8c35d5eeSXin Li </li> 754*8c35d5eeSXin Li <li> 755*8c35d5eeSXin Li It helps you remember that all other variables must be 756*8c35d5eeSXin Li prefixed with scope. 757*8c35d5eeSXin Li </li> 758*8c35d5eeSXin Li <li> 759*8c35d5eeSXin Li <code>l:</code> disambiguates between function-local and 760*8c35d5eeSXin Li vim-predefined variables. For example, <code>count</code> 761*8c35d5eeSXin Li refers to 762*8c35d5eeSXin Li <code>v:count</code>, not <code>l:count</code>. 763*8c35d5eeSXin Li </li> 764*8c35d5eeSXin Li <li> 765*8c35d5eeSXin Li It future proofs your scripts against the introduction of new 766*8c35d5eeSXin Li vim-predefined variables. 767*8c35d5eeSXin Li </li> 768*8c35d5eeSXin Li </ul> 769*8c35d5eeSXin Li </li> 770*8c35d5eeSXin Li <li> 771*8c35d5eeSXin Li Prefix pre-defined vim variables with <code>v:</code> 772*8c35d5eeSXin Li <ul> 773*8c35d5eeSXin Li <li> 774*8c35d5eeSXin Li This is not enforced by vimscript but is good practice. 775*8c35d5eeSXin Li </li> 776*8c35d5eeSXin Li <li> 777*8c35d5eeSXin Li It provides context as to where the (undeclared) variable is 778*8c35d5eeSXin Li coming from. 779*8c35d5eeSXin Li </li> 780*8c35d5eeSXin Li <li> 781*8c35d5eeSXin Li It reminds you that the variable can not be assigned to. 782*8c35d5eeSXin Li </li> 783*8c35d5eeSXin Li </ul> 784*8c35d5eeSXin Li </li> 785*8c35d5eeSXin Li <li> 786*8c35d5eeSXin Li Prefix buffer-local variables with <code>b:</code> 787*8c35d5eeSXin Li <ul> 788*8c35d5eeSXin Li <li> 789*8c35d5eeSXin Li This is useful for plugins that keep per-buffer state. 790*8c35d5eeSXin Li </li> 791*8c35d5eeSXin Li </ul> 792*8c35d5eeSXin Li </li> 793*8c35d5eeSXin Li </ul> 794*8c35d5eeSXin Li </BODY> 795*8c35d5eeSXin Li </STYLEPOINT> 796*8c35d5eeSXin Li 797*8c35d5eeSXin Li <STYLEPOINT title="Strings"> 798*8c35d5eeSXin Li <SUMMARY> 799*8c35d5eeSXin Li Prefer single quotes. 800*8c35d5eeSXin Li </SUMMARY> 801*8c35d5eeSXin Li <BODY> 802*8c35d5eeSXin Li <p> 803*8c35d5eeSXin Li Prefer single quoted strings. Specifically, in order of precedence: 804*8c35d5eeSXin Li </p> 805*8c35d5eeSXin Li <ul> 806*8c35d5eeSXin Li <li> 807*8c35d5eeSXin Li Always use single quotes for regular expressions. 808*8c35d5eeSXin Li <ul> 809*8c35d5eeSXin Li <li> 810*8c35d5eeSXin Li <code>'\s*'</code> is not the same as <code>"\s*"</code> 811*8c35d5eeSXin Li </li> 812*8c35d5eeSXin Li <li> 813*8c35d5eeSXin Li Single quotes will prevent the need for excessive backslashes. 814*8c35d5eeSXin Li </li> 815*8c35d5eeSXin Li <li> 816*8c35d5eeSXin Li Double single quotes escape to one single quote in single 817*8c35d5eeSXin Li quoted strings: <code>'example ('')'</code> represents the 818*8c35d5eeSXin Li string 819*8c35d5eeSXin Li <code>example (')</code> 820*8c35d5eeSXin Li </li> 821*8c35d5eeSXin Li </ul> 822*8c35d5eeSXin Li </li> 823*8c35d5eeSXin Li <li> 824*8c35d5eeSXin Li If your string requires escape characters (<code>\n</code>, 825*8c35d5eeSXin Li <code>\t</code>, etc.) use double quotes. 826*8c35d5eeSXin Li <ul> 827*8c35d5eeSXin Li <li> 828*8c35d5eeSXin Li Escapes can not be expressed in single quoted strings. 829*8c35d5eeSXin Li </li> 830*8c35d5eeSXin Li <li> 831*8c35d5eeSXin Li Remember that <code>'\n'</code> in a regex does not represent a 832*8c35d5eeSXin Li newline, but rather "\n". You only need to use double quotes 833*8c35d5eeSXin Li when you want to embed the represented character itself (e.g. a 834*8c35d5eeSXin Li newline) in the string. 835*8c35d5eeSXin Li </li> 836*8c35d5eeSXin Li </ul> 837*8c35d5eeSXin Li </li> 838*8c35d5eeSXin Li <li> 839*8c35d5eeSXin Li If your string contains no escapes nor single quotes, use single 840*8c35d5eeSXin Li quoted strings. 841*8c35d5eeSXin Li <ul> 842*8c35d5eeSXin Li <li> 843*8c35d5eeSXin Li Most strings in vimscript are regexes, so this provides maximum 844*8c35d5eeSXin Li consistency. 845*8c35d5eeSXin Li </li> 846*8c35d5eeSXin Li </ul> 847*8c35d5eeSXin Li </li> 848*8c35d5eeSXin Li <li> 849*8c35d5eeSXin Li If your non-regex string contains single quotes but no double 850*8c35d5eeSXin Li quotes, use double quotes. 851*8c35d5eeSXin Li <ul> 852*8c35d5eeSXin Li <li> 853*8c35d5eeSXin Li Don't bother escaping strings if you don't have to. 854*8c35d5eeSXin Li </li> 855*8c35d5eeSXin Li <li> 856*8c35d5eeSXin Li This is similar to the python string rules. 857*8c35d5eeSXin Li </li> 858*8c35d5eeSXin Li </ul> 859*8c35d5eeSXin Li </li> 860*8c35d5eeSXin Li <li> 861*8c35d5eeSXin Li If your string contains both single and double quotes, use whichever 862*8c35d5eeSXin Li quoting style requires less escaping. 863*8c35d5eeSXin Li <ul> 864*8c35d5eeSXin Li <li> 865*8c35d5eeSXin Li Break ties in favor of single quotes. 866*8c35d5eeSXin Li </li> 867*8c35d5eeSXin Li </ul> 868*8c35d5eeSXin Li </li> 869*8c35d5eeSXin Li </ul> 870*8c35d5eeSXin Li </BODY> 871*8c35d5eeSXin Li </STYLEPOINT> 872*8c35d5eeSXin Li 873*8c35d5eeSXin Li <STYLEPOINT title="Settings"> 874*8c35d5eeSXin Li <SUMMARY> 875*8c35d5eeSXin Li Prefer long names. Set settings locally. 876*8c35d5eeSXin Li </SUMMARY> 877*8c35d5eeSXin Li <BODY> 878*8c35d5eeSXin Li <ul start="6"> 879*8c35d5eeSXin Li <li> 880*8c35d5eeSXin Li Prefer long names of built in settings (i.e. <code>tabstop</code> 881*8c35d5eeSXin Li over 882*8c35d5eeSXin Li <code>ts</code>). 883*8c35d5eeSXin Li </li> 884*8c35d5eeSXin Li <li> 885*8c35d5eeSXin Li Set local settings unless you explicitly want to set global 886*8c35d5eeSXin Li settings. 887*8c35d5eeSXin Li <ul> 888*8c35d5eeSXin Li <li> 889*8c35d5eeSXin Li Use <code>setlocal</code> and <code>&l:</code> instead of 890*8c35d5eeSXin Li <code>set</code> and <code>&</code>. 891*8c35d5eeSXin Li </li> 892*8c35d5eeSXin Li </ul> 893*8c35d5eeSXin Li </li> 894*8c35d5eeSXin Li </ul> 895*8c35d5eeSXin Li </BODY> 896*8c35d5eeSXin Li </STYLEPOINT> 897*8c35d5eeSXin Li </CATEGORY> 898*8c35d5eeSXin Li <CATEGORY title="Usage Guide"> 899*8c35d5eeSXin Li <p> 900*8c35d5eeSXin Li Vim plugins should provide any or all of the following: 901*8c35d5eeSXin Li <strong>Commands,</strong> <strong>Autocommands,</strong> 902*8c35d5eeSXin Li <strong>Functions,</strong> <strong>Statusline Flags, and</strong> 903*8c35d5eeSXin Li <strong>Mappings.</strong> 904*8c35d5eeSXin Li </p> 905*8c35d5eeSXin Li 906*8c35d5eeSXin Li <STYLEPOINT title="Commands"> 907*8c35d5eeSXin Li <SUMMARY> 908*8c35d5eeSXin Li <ul> 909*8c35d5eeSXin Li <li>Define in <code>plugin/commands.vim</code>.</li> 910*8c35d5eeSXin Li <li>CommandNamesLikeThis.</li> 911*8c35d5eeSXin Li <li>Prefer semantic names to a unified prefix.</li> 912*8c35d5eeSXin Li <li>Do not use <code>[!]</code></li> 913*8c35d5eeSXin Li <li>Extract logic into functions.</li> 914*8c35d5eeSXin Li </ul> 915*8c35d5eeSXin Li </SUMMARY> 916*8c35d5eeSXin Li <BODY> 917*8c35d5eeSXin Li <ul> 918*8c35d5eeSXin Li <li> 919*8c35d5eeSXin Li <code>CommandNamesLikeThis</code> 920*8c35d5eeSXin Li </li> 921*8c35d5eeSXin Li <li> 922*8c35d5eeSXin Li Commands should be defined in one block with no whitespace between 923*8c35d5eeSXin Li them. 924*8c35d5eeSXin Li <ul> 925*8c35d5eeSXin Li <li> 926*8c35d5eeSXin Li Name commands semantically at the expense of a common prefix. 927*8c35d5eeSXin Li </li> 928*8c35d5eeSXin Li <li> 929*8c35d5eeSXin Li <BAD_CODE_SNIPPET> 930*8c35d5eeSXin Li command WhitespaceFixTrailing 931*8c35d5eeSXin Li command WhitespaceFixIndentation 932*8c35d5eeSXin Li </BAD_CODE_SNIPPET> 933*8c35d5eeSXin Li <CODE_SNIPPET> 934*8c35d5eeSXin Li command FixTrailingWhitespace 935*8c35d5eeSXin Li command FixIndentation 936*8c35d5eeSXin Li </CODE_SNIPPET> 937*8c35d5eeSXin Li </li> 938*8c35d5eeSXin Li </ul> 939*8c35d5eeSXin Li </li> 940*8c35d5eeSXin Li <li> 941*8c35d5eeSXin Li Use <code>command</code> without a bang. 942*8c35d5eeSXin Li <ul> 943*8c35d5eeSXin Li <li> 944*8c35d5eeSXin Li This notifies users to command name conflicts immediately at 945*8c35d5eeSXin Li startup. 946*8c35d5eeSXin Li </li> 947*8c35d5eeSXin Li <li> 948*8c35d5eeSXin Li Command name collisions are an error and should not fail 949*8c35d5eeSXin Li silently. 950*8c35d5eeSXin Li </li> 951*8c35d5eeSXin Li <li> 952*8c35d5eeSXin Li Plugins are guarded against re-entry, so a single vim session 953*8c35d5eeSXin Li should never attempt to re-define defined commands. 954*8c35d5eeSXin Li </li> 955*8c35d5eeSXin Li </ul> 956*8c35d5eeSXin Li </li> 957*8c35d5eeSXin Li <li> 958*8c35d5eeSXin Li Do not put logic in commands. 959*8c35d5eeSXin Li <ul> 960*8c35d5eeSXin Li <li> 961*8c35d5eeSXin Li Delegate to functions instead. 962*8c35d5eeSXin Li </li> 963*8c35d5eeSXin Li <li> 964*8c35d5eeSXin Li Pass non-argument command parameters (<code><bang></code>, 965*8c35d5eeSXin Li <code><register></code>, etc.) before argument parameters 966*8c35d5eeSXin Li (<code><f-args></code>, etc.). 967*8c35d5eeSXin Li </li> 968*8c35d5eeSXin Li <li> 969*8c35d5eeSXin Li Otherwise variable-length argument functions are difficult to 970*8c35d5eeSXin Li implement. 971*8c35d5eeSXin Li </li> 972*8c35d5eeSXin Li </ul> 973*8c35d5eeSXin Li </li> 974*8c35d5eeSXin Li <li> 975*8c35d5eeSXin Li Do not autoload commands. 976*8c35d5eeSXin Li <ul> 977*8c35d5eeSXin Li <li> 978*8c35d5eeSXin Li Autoloaded commands will not be available until after a function 979*8c35d5eeSXin Li in the same file is called. 980*8c35d5eeSXin Li </li> 981*8c35d5eeSXin Li <li> 982*8c35d5eeSXin Li Commands intended to be used in the .vimrc should be defined in 983*8c35d5eeSXin Li a <code>instant/commands.vim</code> file in plugins using 984*8c35d5eeSXin Li maktaba, or explicitly installed via an autoload function in 985*8c35d5eeSXin Li non-maktaba plugins. 986*8c35d5eeSXin Li </li> 987*8c35d5eeSXin Li </ul> 988*8c35d5eeSXin Li </li> 989*8c35d5eeSXin Li </ul> 990*8c35d5eeSXin Li <SUBSECTION title="Conventions"> 991*8c35d5eeSXin Li <ul> 992*8c35d5eeSXin Li <li> 993*8c35d5eeSXin Li Pass <code><bang></code> to functions with 994*8c35d5eeSXin Li <code>'<bang>' == '!'</code>. 995*8c35d5eeSXin Li <ul> 996*8c35d5eeSXin Li <li> 997*8c35d5eeSXin Li The function should receive a boolean parameter, not a string. 998*8c35d5eeSXin Li </li> 999*8c35d5eeSXin Li </ul> 1000*8c35d5eeSXin Li </li> 1001*8c35d5eeSXin Li </ul> 1002*8c35d5eeSXin Li </SUBSECTION> 1003*8c35d5eeSXin Li </BODY> 1004*8c35d5eeSXin Li </STYLEPOINT> 1005*8c35d5eeSXin Li 1006*8c35d5eeSXin Li <STYLEPOINT title="Autocommands"> 1007*8c35d5eeSXin Li <SUMMARY> 1008*8c35d5eeSXin Li <ul> 1009*8c35d5eeSXin Li <li>Define in <code>plugin/autocmds.vim</code>.</li> 1010*8c35d5eeSXin Li <li>Use augroups.</li> 1011*8c35d5eeSXin Li <li>augroup_names_like_this.</li> 1012*8c35d5eeSXin Li <li>Clear the augroup first.</li> 1013*8c35d5eeSXin Li <li>Extract logic into functions.</li> 1014*8c35d5eeSXin Li </ul> 1015*8c35d5eeSXin Li </SUMMARY> 1016*8c35d5eeSXin Li <BODY> 1017*8c35d5eeSXin Li <ul> 1018*8c35d5eeSXin Li <li> 1019*8c35d5eeSXin Li All autocommands should be defined in the 1020*8c35d5eeSXin Li <code>plugin/autocmds.vim</code> file. 1021*8c35d5eeSXin Li <ul> 1022*8c35d5eeSXin Li <li> 1023*8c35d5eeSXin Li This allows users to disable your autocommands with 1024*8c35d5eeSXin Li <code>Glaive myplugin !plugin[autocmds]</code>. 1025*8c35d5eeSXin Li </li> 1026*8c35d5eeSXin Li </ul> 1027*8c35d5eeSXin Li </li> 1028*8c35d5eeSXin Li <li> 1029*8c35d5eeSXin Li Declare all autocommands in an <code>augroup</code> block. 1030*8c35d5eeSXin Li <ul> 1031*8c35d5eeSXin Li <li> 1032*8c35d5eeSXin Li This allows your autocommands to be cleared with 1033*8c35d5eeSXin Li <code>autocmd!</code>. 1034*8c35d5eeSXin Li </li> 1035*8c35d5eeSXin Li <li> 1036*8c35d5eeSXin Li If your plugin only has one <code>augroup</code>, the 1037*8c35d5eeSXin Li <code>augroup</code> name should be the same as your plugin 1038*8c35d5eeSXin Li name, with underscores in place of any hyphens. 1039*8c35d5eeSXin Li </li> 1040*8c35d5eeSXin Li <li> 1041*8c35d5eeSXin Li Otherwise <code>augroup</code> names should start with your 1042*8c35d5eeSXin Li plugin name followed by an underscore. 1043*8c35d5eeSXin Li </li> 1044*8c35d5eeSXin Li </ul> 1045*8c35d5eeSXin Li </li> 1046*8c35d5eeSXin Li <li> 1047*8c35d5eeSXin Li Do not put logic in autocommands. 1048*8c35d5eeSXin Li <ul> 1049*8c35d5eeSXin Li <li> 1050*8c35d5eeSXin Li Delegate to functions instead. 1051*8c35d5eeSXin Li </li> 1052*8c35d5eeSXin Li </ul> 1053*8c35d5eeSXin Li </li> 1054*8c35d5eeSXin Li <li> 1055*8c35d5eeSXin Li When creating a new <code>augroup</code>, clear it with 1056*8c35d5eeSXin Li <code>autocmd!</code> 1057*8c35d5eeSXin Li <ul> 1058*8c35d5eeSXin Li <li> 1059*8c35d5eeSXin Li This allows your plugins to be re-enterable. 1060*8c35d5eeSXin Li </li> 1061*8c35d5eeSXin Li </ul> 1062*8c35d5eeSXin Li </li> 1063*8c35d5eeSXin Li </ul> 1064*8c35d5eeSXin Li </BODY> 1065*8c35d5eeSXin Li </STYLEPOINT> 1066*8c35d5eeSXin Li 1067*8c35d5eeSXin Li <STYLEPOINT title="Functions"> 1068*8c35d5eeSXin Li <SUMMARY> 1069*8c35d5eeSXin Li <ul> 1070*8c35d5eeSXin Li <li>FunctionNamesLikeThis.</li> 1071*8c35d5eeSXin Li <li>Autoload all functions.</li> 1072*8c35d5eeSXin Li <li>Prefix script-local functions with <code>s:</code></li> 1073*8c35d5eeSXin Li <li>Use <code>[!]</code>.</li> 1074*8c35d5eeSXin Li <li>Use <code>[abort]</code>.</li> 1075*8c35d5eeSXin Li </ul> 1076*8c35d5eeSXin Li </SUMMARY> 1077*8c35d5eeSXin Li <BODY> 1078*8c35d5eeSXin Li <ul> 1079*8c35d5eeSXin Li <li> 1080*8c35d5eeSXin Li <code>FunctionNamesLikeThis</code> 1081*8c35d5eeSXin Li </li> 1082*8c35d5eeSXin Li <li> 1083*8c35d5eeSXin Li Prefix all script-local functions with <code>s:</code> 1084*8c35d5eeSXin Li </li> 1085*8c35d5eeSXin Li <li> 1086*8c35d5eeSXin Li Do not provide global functions. Use autoloaded functions instead. 1087*8c35d5eeSXin Li </li> 1088*8c35d5eeSXin Li <li> 1089*8c35d5eeSXin Li Place two blank lines between top-level functions. 1090*8c35d5eeSXin Li </li> 1091*8c35d5eeSXin Li <li> 1092*8c35d5eeSXin Li Declare all functions with <code>abort</code>. 1093*8c35d5eeSXin Li <ul> 1094*8c35d5eeSXin Li <li> 1095*8c35d5eeSXin Li If you do not do this, the function's behavior depends upon 1096*8c35d5eeSXin Li whether it is called within a <code>try..endtry</code> block 1097*8c35d5eeSXin Li somewhere on the stack. 1098*8c35d5eeSXin Li </li> 1099*8c35d5eeSXin Li <li> 1100*8c35d5eeSXin Li The <code>abort</code> keyword forces the function to act 1101*8c35d5eeSXin Li consistently. 1102*8c35d5eeSXin Li </li> 1103*8c35d5eeSXin Li <li> 1104*8c35d5eeSXin Li Without it, the function may (or may not) attempt to continue 1105*8c35d5eeSXin Li execution after an error occurs. 1106*8c35d5eeSXin Li </li> 1107*8c35d5eeSXin Li </ul> 1108*8c35d5eeSXin Li </li> 1109*8c35d5eeSXin Li <li> 1110*8c35d5eeSXin Li Use <code>function!</code> with a bang. 1111*8c35d5eeSXin Li <ul> 1112*8c35d5eeSXin Li <li> 1113*8c35d5eeSXin Li This allows developers to re-source their scripts and have the 1114*8c35d5eeSXin Li functions reloaded without complaint. 1115*8c35d5eeSXin Li </li> 1116*8c35d5eeSXin Li <li> 1117*8c35d5eeSXin Li Function names should never collide because functions should 1118*8c35d5eeSXin Li always be either script-local or defined in an 1119*8c35d5eeSXin Li <code>autoload</code> directory. 1120*8c35d5eeSXin Li </li> 1121*8c35d5eeSXin Li <li> 1122*8c35d5eeSXin Li Failing to use a bang in any function in an autoload file will 1123*8c35d5eeSXin Li lead to cryptic errors if vim tries to re-source the file 1124*8c35d5eeSXin Li (e.g., if you refer to an nonexistent autoload function). 1125*8c35d5eeSXin Li </li> 1126*8c35d5eeSXin Li </ul> 1127*8c35d5eeSXin Li </li> 1128*8c35d5eeSXin Li <li> 1129*8c35d5eeSXin Li Use <code>...</code> for optional arguments, not for lists of 1130*8c35d5eeSXin Li arguments. 1131*8c35d5eeSXin Li <ul> 1132*8c35d5eeSXin Li <li> 1133*8c35d5eeSXin Li Vimscript functions take at most 20 arguments. 1134*8c35d5eeSXin Li </li> 1135*8c35d5eeSXin Li <li> 1136*8c35d5eeSXin Li Lists have no such length restriction. 1137*8c35d5eeSXin Li </li> 1138*8c35d5eeSXin Li <li> 1139*8c35d5eeSXin Li Your function is likely to break when given too many arguments 1140*8c35d5eeSXin Li if you use <code>...</code> for a list of arguments. 1141*8c35d5eeSXin Li </li> 1142*8c35d5eeSXin Li </ul> 1143*8c35d5eeSXin Li </li> 1144*8c35d5eeSXin Li <li> 1145*8c35d5eeSXin Li Throw exceptions rather than printing errors. 1146*8c35d5eeSXin Li <ul> 1147*8c35d5eeSXin Li <li> 1148*8c35d5eeSXin Li Printed errors can not be caught. 1149*8c35d5eeSXin Li </li> 1150*8c35d5eeSXin Li <li> 1151*8c35d5eeSXin Li Top-level functions expecting errors may catch them and print 1152*8c35d5eeSXin Li error messages, but even those should throw their own errors 1153*8c35d5eeSXin Li when they choke. 1154*8c35d5eeSXin Li </li> 1155*8c35d5eeSXin Li </ul> 1156*8c35d5eeSXin Li </li> 1157*8c35d5eeSXin Li </ul> 1158*8c35d5eeSXin Li </BODY> 1159*8c35d5eeSXin Li </STYLEPOINT> 1160*8c35d5eeSXin Li 1161*8c35d5eeSXin Li <STYLEPOINT title="Mappings"> 1162*8c35d5eeSXin Li <SUMMARY> 1163*8c35d5eeSXin Li <ul> 1164*8c35d5eeSXin Li <li> 1165*8c35d5eeSXin Li Provide opt-in key mappings in <code>plugin/mappings.vim</code>. 1166*8c35d5eeSXin Li </li> 1167*8c35d5eeSXin Li <li> 1168*8c35d5eeSXin Li <code><Plug></code> mappings can be defined in 1169*8c35d5eeSXin Li <code>plugin/plugs.vim</code> (unlike mappings.vim, plugs.vim is 1170*8c35d5eeSXin Li opt-out). 1171*8c35d5eeSXin Li </li> 1172*8c35d5eeSXin Li </ul> 1173*8c35d5eeSXin Li </SUMMARY> 1174*8c35d5eeSXin Li <BODY> 1175*8c35d5eeSXin Li <ul> 1176*8c35d5eeSXin Li <li> 1177*8c35d5eeSXin Li Define key mappings in <code>plugin/mappings.vim</code>, using 1178*8c35d5eeSXin Li <code>maktaba#plugin#MapPrefix</code> to get a prefix. 1179*8c35d5eeSXin Li <ul> 1180*8c35d5eeSXin Li <li> 1181*8c35d5eeSXin Li Mappings defined in the special <code>plugin/mappings.vim</code> 1182*8c35d5eeSXin Li file will be disabled by default (by the standard 1183*8c35d5eeSXin Li <code>maktaba#plugin#Enter()</code> boilerplate). 1184*8c35d5eeSXin Li </li> 1185*8c35d5eeSXin Li <li> 1186*8c35d5eeSXin Li Users can enable key mappings with 1187*8c35d5eeSXin Li <code>Glaive myplugin plugin[mappings]</code>. 1188*8c35d5eeSXin Li </li> 1189*8c35d5eeSXin Li </ul> 1190*8c35d5eeSXin Li </li> 1191*8c35d5eeSXin Li <li> 1192*8c35d5eeSXin Li Make all mappings with <code><unique></code>. 1193*8c35d5eeSXin Li <ul> 1194*8c35d5eeSXin Li <li> 1195*8c35d5eeSXin Li This will inform the user when they have a mapping conflict 1196*8c35d5eeSXin Li instead of silently clobbering their existing mappings. 1197*8c35d5eeSXin Li </li> 1198*8c35d5eeSXin Li </ul> 1199*8c35d5eeSXin Li </li> 1200*8c35d5eeSXin Li <li> 1201*8c35d5eeSXin Li You may provide pseudo-mappings using <code><Plug></code> and 1202*8c35d5eeSXin Li your plugin's name in <code>plugin/plugs.vim</code> (separate from 1203*8c35d5eeSXin Li standard key mappings). 1204*8c35d5eeSXin Li <ul> 1205*8c35d5eeSXin Li <li> 1206*8c35d5eeSXin Li <code><Plug></code> is a sequence which can not be typed. 1207*8c35d5eeSXin Li </li> 1208*8c35d5eeSXin Li <li> 1209*8c35d5eeSXin Li You can do something like 1210*8c35d5eeSXin Li <code>noremap <Plug>namespace#MappingName 1211*8c35d5eeSXin Li some_key_sequence</code> 1212*8c35d5eeSXin Li and then users can do 1213*8c35d5eeSXin Li <code>noremap <leader>x 1214*8c35d5eeSXin Li <Plug>namespace#MappingName</code> 1215*8c35d5eeSXin Li to take advantage of your pseudo-mapping. 1216*8c35d5eeSXin Li </li> 1217*8c35d5eeSXin Li <li> 1218*8c35d5eeSXin Li Pseudo-mappings should <strong>not</strong> be in 1219*8c35d5eeSXin Li <code>plugin/mappings.vim</code> or they will be disabled by 1220*8c35d5eeSXin Li default. 1221*8c35d5eeSXin Li </li> 1222*8c35d5eeSXin Li <li> 1223*8c35d5eeSXin Li Such pseudo-mappings should be named <code><Plug></code> 1224*8c35d5eeSXin Li followed by your plugin name, a pound sign, and a unique mapping 1225*8c35d5eeSXin Li name (CamelCased like a function). 1226*8c35d5eeSXin Li </li> 1227*8c35d5eeSXin Li </ul> 1228*8c35d5eeSXin Li </li> 1229*8c35d5eeSXin Li <li> 1230*8c35d5eeSXin Li Always use the <code>noremap</code> family of commands. Never use 1231*8c35d5eeSXin Li the <code>map</code> family. 1232*8c35d5eeSXin Li <ul> 1233*8c35d5eeSXin Li <li> 1234*8c35d5eeSXin Li <code>map</code> depends upon the user's existing mappings, and 1235*8c35d5eeSXin Li could do anything. 1236*8c35d5eeSXin Li </li> 1237*8c35d5eeSXin Li </ul> 1238*8c35d5eeSXin Li </li> 1239*8c35d5eeSXin Li <li> 1240*8c35d5eeSXin Li Only use <code>noremap</code> for commands that both make a motion 1241*8c35d5eeSXin Li and take a range. 1242*8c35d5eeSXin Li <ul> 1243*8c35d5eeSXin Li <li> 1244*8c35d5eeSXin Li <code>noremap</code> makes mappings in normal, visual, and 1245*8c35d5eeSXin Li operator-pending modes. 1246*8c35d5eeSXin Li </li> 1247*8c35d5eeSXin Li <li> 1248*8c35d5eeSXin Li If you don't want all these use <code>nnoremap</code> 1249*8c35d5eeSXin Li <code>onoremap</code> or <code>vnoremap</code> explicitly. 1250*8c35d5eeSXin Li </li> 1251*8c35d5eeSXin Li </ul> 1252*8c35d5eeSXin Li </li> 1253*8c35d5eeSXin Li <li> 1254*8c35d5eeSXin Li Always use <code><SID></code> in place of <code>s:</code> when 1255*8c35d5eeSXin Li accessing script locals in mappings. 1256*8c35d5eeSXin Li <ul> 1257*8c35d5eeSXin Li <li> 1258*8c35d5eeSXin Li Using <code>s:</code> will often fail as the mapping attempts to 1259*8c35d5eeSXin Li type a literal s and colon. 1260*8c35d5eeSXin Li </li> 1261*8c35d5eeSXin Li </ul> 1262*8c35d5eeSXin Li </li> 1263*8c35d5eeSXin Li </ul> 1264*8c35d5eeSXin Li </BODY> 1265*8c35d5eeSXin Li </STYLEPOINT> 1266*8c35d5eeSXin Li </CATEGORY> 1267*8c35d5eeSXin Li <CATEGORY title="Conventions"> 1268*8c35d5eeSXin Li <STYLEPOINT title="Dependency Checking"> 1269*8c35d5eeSXin Li <SUMMARY> 1270*8c35d5eeSXin Li Declare dependencies in addon-info.json and use <code>maktaba</code>. 1271*8c35d5eeSXin Li </SUMMARY> 1272*8c35d5eeSXin Li <BODY> 1273*8c35d5eeSXin Li <p> 1274*8c35d5eeSXin Li Declaring dependencies in addon-info.json allows conformant plugin 1275*8c35d5eeSXin Li managers (like VAM) to ensure dependencies are installed. See the 1276*8c35d5eeSXin Li <a href="https://github.com/MarcWeber/vim-addon-manager/blob/master/doc/vim-addon-manager-additional-documentation.txt">VAM documentation</a> for details. 1277*8c35d5eeSXin Li </p> 1278*8c35d5eeSXin Li <p> 1279*8c35d5eeSXin Li Calling <code>maktaba#library#Require</code> from dependent code at 1280*8c35d5eeSXin Li runtime ensures that dependencies have been installed and that they 1281*8c35d5eeSXin Li don't include unsafe non-library files. 1282*8c35d5eeSXin Li </p> 1283*8c35d5eeSXin Li </BODY> 1284*8c35d5eeSXin Li </STYLEPOINT> 1285*8c35d5eeSXin Li 1286*8c35d5eeSXin Li <STYLEPOINT title="Statusline Flags"> 1287*8c35d5eeSXin Li <SUMMARY> 1288*8c35d5eeSXin Li Use <code><plugin-name>#status#Status()</code> or its 1289*8c35d5eeSXin Li finer-grained variants to provide statusline flags. 1290*8c35d5eeSXin Li </SUMMARY> 1291*8c35d5eeSXin Li <BODY> 1292*8c35d5eeSXin Li <p> 1293*8c35d5eeSXin Li Following is a convention for exposing statusline flags to the user. A 1294*8c35d5eeSXin Li plugin should never modify the user's statusline except for when that 1295*8c35d5eeSXin Li is the only purpose of the plugin (powerline, etc.). 1296*8c35d5eeSXin Li </p> 1297*8c35d5eeSXin Li <ul> 1298*8c35d5eeSXin Li <li> 1299*8c35d5eeSXin Li Provide the 1300*8c35d5eeSXin Li <code class="green">Info</code>, 1301*8c35d5eeSXin Li <code class="yellow">Alert</code>, 1302*8c35d5eeSXin Li <code class="orange">Warning</code>, and 1303*8c35d5eeSXin Li <code class="red">Error</code> functions under the 1304*8c35d5eeSXin Li <code><plugin-name>#status</code> namespace. 1305*8c35d5eeSXin Li </li> 1306*8c35d5eeSXin Li <li> 1307*8c35d5eeSXin Li <code class="green">Info</code> should provide information about the 1308*8c35d5eeSXin Li state of the buffer. 1309*8c35d5eeSXin Li <ul> 1310*8c35d5eeSXin Li <li> 1311*8c35d5eeSXin Li Example: The current git branch. 1312*8c35d5eeSXin Li </li> 1313*8c35d5eeSXin Li </ul> 1314*8c35d5eeSXin Li </li> 1315*8c35d5eeSXin Li <li> 1316*8c35d5eeSXin Li <code class="yellow">Alert</code> should provide a quiet reminder 1317*8c35d5eeSXin Li that the buffer is non-standard. 1318*8c35d5eeSXin Li <ul> 1319*8c35d5eeSXin Li <li> 1320*8c35d5eeSXin Li Example: The readonly setting is on. 1321*8c35d5eeSXin Li </li> 1322*8c35d5eeSXin Li </ul> 1323*8c35d5eeSXin Li </li> 1324*8c35d5eeSXin Li <li> 1325*8c35d5eeSXin Li <code class="orange">Warning</code> should provide a warning about 1326*8c35d5eeSXin Li the current state of the buffer. 1327*8c35d5eeSXin Li <ul> 1328*8c35d5eeSXin Li <li> 1329*8c35d5eeSXin Li Example: The file has been edited elsewhere. 1330*8c35d5eeSXin Li </li> 1331*8c35d5eeSXin Li </ul> 1332*8c35d5eeSXin Li </li> 1333*8c35d5eeSXin Li <li> 1334*8c35d5eeSXin Li <code class="red">Error</code> should bring to attention a loud 1335*8c35d5eeSXin Li issue with the buffer. 1336*8c35d5eeSXin Li <ul> 1337*8c35d5eeSXin Li <li> 1338*8c35d5eeSXin Li Example: The file does not pass the syntax checker. 1339*8c35d5eeSXin Li </li> 1340*8c35d5eeSXin Li </ul> 1341*8c35d5eeSXin Li </li> 1342*8c35d5eeSXin Li <li> 1343*8c35d5eeSXin Li By following these conventions, users can easily build up their own 1344*8c35d5eeSXin Li statusline customizing the verbosity and colors to their tastes. 1345*8c35d5eeSXin Li </li> 1346*8c35d5eeSXin Li <li> 1347*8c35d5eeSXin Li All functions should take no arguments and should return either 1348*8c35d5eeSXin Li empty strings or strings enclosed by square brackets, e.g. 1349*8c35d5eeSXin Li <code>[Google]</code>. For example: 1350*8c35d5eeSXin Li <ul> 1351*8c35d5eeSXin Li <li> 1352*8c35d5eeSXin Li A trailing whitespace plugin might return <code>[$]</code> if 1353*8c35d5eeSXin Li the file contains trailing whitespace 1354*8c35d5eeSXin Li </li> 1355*8c35d5eeSXin Li <li> 1356*8c35d5eeSXin Li A prose writing plugin might return <code>[write]</code> if vim 1357*8c35d5eeSXin Li is in writing mode. 1358*8c35d5eeSXin Li </li> 1359*8c35d5eeSXin Li </ul> 1360*8c35d5eeSXin Li </li> 1361*8c35d5eeSXin Li <li> 1362*8c35d5eeSXin Li Consider providing the 1363*8c35d5eeSXin Li <code><plugin-name>#status#Status</code> function. 1364*8c35d5eeSXin Li <ul> 1365*8c35d5eeSXin Li <li> 1366*8c35d5eeSXin Li It should return the first non-empty of <code>Error</code>, 1367*8c35d5eeSXin Li <code>Warning</code>, <code>Alert</code>, or <code>Info</code>. 1368*8c35d5eeSXin Li </li> 1369*8c35d5eeSXin Li <li> 1370*8c35d5eeSXin Li This is useful for users who want only the most relevant flag 1371*8c35d5eeSXin Li and do not have a colored statusline. 1372*8c35d5eeSXin Li </li> 1373*8c35d5eeSXin Li </ul> 1374*8c35d5eeSXin Li </li> 1375*8c35d5eeSXin Li </ul> 1376*8c35d5eeSXin Li </BODY> 1377*8c35d5eeSXin Li </STYLEPOINT> 1378*8c35d5eeSXin Li </CATEGORY> 1379*8c35d5eeSXin Li <CATEGORY title="Forbidden Commands"> 1380*8c35d5eeSXin Li <p> 1381*8c35d5eeSXin Li These are commands which can only be used by a limited number of 1382*8c35d5eeSXin Li plugins, and should not in general be used by yours. 1383*8c35d5eeSXin Li </p> 1384*8c35d5eeSXin Li <ul> 1385*8c35d5eeSXin Li <li> 1386*8c35d5eeSXin Li Do not use <code>:match :2match</code> or <code>:3match</code> 1387*8c35d5eeSXin Li <ul> 1388*8c35d5eeSXin Li <li> 1389*8c35d5eeSXin Li These are reserved for the user and for vim itself. 1390*8c35d5eeSXin Li </li> 1391*8c35d5eeSXin Li <li> 1392*8c35d5eeSXin Li Use <code>matchadd()</code> to create a matchlevel unique to your 1393*8c35d5eeSXin Li plugin. 1394*8c35d5eeSXin Li </li> 1395*8c35d5eeSXin Li </ul> 1396*8c35d5eeSXin Li </li> 1397*8c35d5eeSXin Li <li> 1398*8c35d5eeSXin Li Do not use <code>echoerr</code>. 1399*8c35d5eeSXin Li <ul> 1400*8c35d5eeSXin Li <li> 1401*8c35d5eeSXin Li <code>echoerr</code> does not print the red error message that you 1402*8c35d5eeSXin Li might think it does. 1403*8c35d5eeSXin Li </li> 1404*8c35d5eeSXin Li <li> 1405*8c35d5eeSXin Li <code>echoerr</code> prints an error message as well as context 1406*8c35d5eeSXin Li about the code where <code>echoerr</code> was called. 1407*8c35d5eeSXin Li </li> 1408*8c35d5eeSXin Li <li> 1409*8c35d5eeSXin Li <code>echoerr</code> is best suited for debugging. 1410*8c35d5eeSXin Li </li> 1411*8c35d5eeSXin Li <li> 1412*8c35d5eeSXin Li Use <code>echohl</code> in tandem with <code>echomsg</code> if 1413*8c35d5eeSXin Li you want the red error bar. 1414*8c35d5eeSXin Li </li> 1415*8c35d5eeSXin Li </ul> 1416*8c35d5eeSXin Li </li> 1417*8c35d5eeSXin Li <li> 1418*8c35d5eeSXin Li Use <code>echomsg</code> instead of <code>echo</code>. 1419*8c35d5eeSXin Li <ul> 1420*8c35d5eeSXin Li <li> 1421*8c35d5eeSXin Li <code>echomsg</code> messages can be reviewed with the 1422*8c35d5eeSXin Li <code>:messages</code> command. 1423*8c35d5eeSXin Li </li> 1424*8c35d5eeSXin Li <li> 1425*8c35d5eeSXin Li <code>echo</code> messages disappear permanently on redraw, which 1426*8c35d5eeSXin Li can be very annoying to users who failed to read the message in 1427*8c35d5eeSXin Li time. 1428*8c35d5eeSXin Li </li> 1429*8c35d5eeSXin Li </ul> 1430*8c35d5eeSXin Li </li> 1431*8c35d5eeSXin Li </ul> 1432*8c35d5eeSXin Li </CATEGORY> 1433*8c35d5eeSXin Li <CATEGORY title="Layout"> 1434*8c35d5eeSXin Li <p> 1435*8c35d5eeSXin Li Lay out <code>plugin/</code> files in the following sections, if 1436*8c35d5eeSXin Li applicable, separated by two blank lines: 1437*8c35d5eeSXin Li </p> 1438*8c35d5eeSXin Li <ul> 1439*8c35d5eeSXin Li <li> 1440*8c35d5eeSXin Li Declaration of script constants 1441*8c35d5eeSXin Li </li> 1442*8c35d5eeSXin Li <li> 1443*8c35d5eeSXin Li Declaration of configuration variables 1444*8c35d5eeSXin Li </li> 1445*8c35d5eeSXin Li <li> 1446*8c35d5eeSXin Li Other declarations (commands in <code>commands.vim</code> file, 1447*8c35d5eeSXin Li autocommands in <code>autocmds.vim</code> file, etc.) 1448*8c35d5eeSXin Li </li> 1449*8c35d5eeSXin Li </ul> 1450*8c35d5eeSXin Li <p> 1451*8c35d5eeSXin Li Lay out <code>autoload/</code> files in the following sections, if 1452*8c35d5eeSXin Li applicable, separated by two blank lines: 1453*8c35d5eeSXin Li </p> 1454*8c35d5eeSXin Li <ul> 1455*8c35d5eeSXin Li <li> 1456*8c35d5eeSXin Li <code>maktaba#library#Require</code> calls 1457*8c35d5eeSXin Li </li> 1458*8c35d5eeSXin Li <li> 1459*8c35d5eeSXin Li Script-local variables 1460*8c35d5eeSXin Li </li> 1461*8c35d5eeSXin Li <li> 1462*8c35d5eeSXin Li Script-local functions 1463*8c35d5eeSXin Li </li> 1464*8c35d5eeSXin Li <li> 1465*8c35d5eeSXin Li Private autoloaded functions 1466*8c35d5eeSXin Li </li> 1467*8c35d5eeSXin Li <li> 1468*8c35d5eeSXin Li Public autoloaded functions 1469*8c35d5eeSXin Li </li> 1470*8c35d5eeSXin Li </ul> 1471*8c35d5eeSXin Li <p> 1472*8c35d5eeSXin Li This is recommended convention and is not enforced. 1473*8c35d5eeSXin Li </p> 1474*8c35d5eeSXin Li 1475*8c35d5eeSXin Li </CATEGORY> 1476*8c35d5eeSXin Li <CATEGORY title="Recommended Shortcuts"> 1477*8c35d5eeSXin Li 1478*8c35d5eeSXin Li <p> 1479*8c35d5eeSXin Li Use the following shortcuts: 1480*8c35d5eeSXin Li </p> 1481*8c35d5eeSXin Li <ul> 1482*8c35d5eeSXin Li <li> 1483*8c35d5eeSXin Li <code>catch</code> over <code>catch /.*/</code> 1484*8c35d5eeSXin Li </li> 1485*8c35d5eeSXin Li <li> 1486*8c35d5eeSXin Li <code>return</code> over <code>return 0</code> when the return value 1487*8c35d5eeSXin Li has no semantic purpose. 1488*8c35d5eeSXin Li </li> 1489*8c35d5eeSXin Li </ul> 1490*8c35d5eeSXin Li 1491*8c35d5eeSXin Li </CATEGORY> 1492*8c35d5eeSXin Li <CATEGORY title="Errata"> 1493*8c35d5eeSXin Li <p> 1494*8c35d5eeSXin Li This section plumbs some of the darker corners of vimscript, explaining 1495*8c35d5eeSXin Li the language pathologies that you wish you didn't have to know. 1496*8c35d5eeSXin Li 1497*8c35d5eeSXin Li </p> 1498*8c35d5eeSXin Li 1499*8c35d5eeSXin Li <STYLEPOINT title="Compatibility Mode"> 1500*8c35d5eeSXin Li <SUMMARY> 1501*8c35d5eeSXin Li If you don't support vi-compatibility mode, fail gracefully. 1502*8c35d5eeSXin Li </SUMMARY> 1503*8c35d5eeSXin Li <BODY> 1504*8c35d5eeSXin Li <p> 1505*8c35d5eeSXin Li When <code>compatible</code> is set, many vim features are not 1506*8c35d5eeSXin Li available. The vim feature which most commonly affects vimscript 1507*8c35d5eeSXin Li authors is line continuations. 1508*8c35d5eeSXin Li </p> 1509*8c35d5eeSXin Li <p> 1510*8c35d5eeSXin Li If you want your plugin to work in vim with vi compatibility on, you 1511*8c35d5eeSXin Li will need to save the compatibility options at the beginning of each 1512*8c35d5eeSXin Li plugin file, clear them, and restore them at the end of each plugin 1513*8c35d5eeSXin Li file. See <code>:help use-cpo-save</code> for details. 1514*8c35d5eeSXin Li </p> 1515*8c35d5eeSXin Li <p> 1516*8c35d5eeSXin Li Plugins that depend on maktaba generally don't need to worry about 1517*8c35d5eeSXin Li compatible mode since maktaba currently just disables it, printing a 1518*8c35d5eeSXin Li warning. 1519*8c35d5eeSXin Li </p> 1520*8c35d5eeSXin Li </BODY> 1521*8c35d5eeSXin Li </STYLEPOINT> 1522*8c35d5eeSXin Li </CATEGORY> 1523*8c35d5eeSXin Li 1524*8c35d5eeSXin Li <p align="right"> 1525*8c35d5eeSXin Li Revision 1.1 1526*8c35d5eeSXin Li </p> 1527*8c35d5eeSXin Li 1528*8c35d5eeSXin Li 1529*8c35d5eeSXin Li <address> 1530*8c35d5eeSXin Li Nate Soares<br/> 1531*8c35d5eeSXin Li Joshua Hoak<br/> 1532*8c35d5eeSXin Li David Barnett<br/> 1533*8c35d5eeSXin Li </address> 1534*8c35d5eeSXin Li</GUIDE> 1535