Google Style Guides

Google Vimscript Guide

Background

This is the in-depth vimscript guide. If you’re just a casual user looking to write a plugin, the abbreviated style guide is for you.

This rather rotund guide dives into justifications and clarifications. It provides an idealized set of rules that are rather too draconian to push on casual scripters.

It’s for users who want to know why certain decisions were made in the abbreviated guide and who want to learn a thing or two about using vimscript safely.

Fair warning: Vimscript is a maddening abyss. When you gaze into it, it gazes also into you. Proceed with caution.

Portability

Vim is highly configurable. Users can change many of the default settings, including the case sensitivity, the regular expression rules, the substitution rules, and more. In order for your vimscript to work for all users, follow these guidelines:

In general, guard all commands and functions against user settings.

Language Guide

All other language features are fair game.

Structure

Libraries vs. Functionality

Separate library-providing plugins from command-providing plugins.

Many plugins provide either user functionality (commands, autocommands, etc) or an API (of autoloaded functions) but not both. This separation is encouraged, as it allows other plugins to pull in a library without also pulling in commands, setting changes, and other plugin functionality that affects the end user.

Configuration

Don’t clobber user settings. Provide as much configurability as possible: that’s what Vim’s all about.

Style Guide

Follow google-wide style conventions. Mimic google python style when in doubt.

Documentation

Use vimdoc.

Provide help files generated by vimdoc. Write documentation in .vim files in conformance with the vimdoc standards and include fields like “description” and “author” in the addon-info.json file (see the VAM documentation).

Whitespace

Follow google-wide conventions.

Line Continuations

Comments

Variables

plugin-names-like-this, FunctionNamesLikeThis, CommandNamesLikeThis, augroup_names_like_this, variable_names_like_this. Prefix all variables with their scope.

Strings

Prefer single quotes.

Prefer single quoted strings. Specifically, in order of precedence:

Settings

Prefer long names. Set settings locally.

Usage Guide

Vim plugins should provide any or all of the following: Commands, Autocommands, Functions, Statusline Flags, and Mappings.

Commands

Conventions

Autocommands

Functions

Mappings

Conventions

Dependency Checking

Declare dependencies in addon-info.json and use maktaba.

Declaring dependencies in addon-info.json allows conformant plugin managers (like VAM) to ensure dependencies are installed. See the VAM documentation for details.

Calling maktaba#library#Require from dependent code at runtime ensures that dependencies have been installed and that they don’t include unsafe non-library files.

Statusline Flags

Use <plugin-name>#status#Status() or its finer-grained variants to provide statusline flags.

Following is a convention for exposing statusline flags to the user. A plugin should never modify the user’s statusline except for when that is the only purpose of the plugin (powerline, etc.).

Forbidden Commands

These are commands which can only be used by a limited number of plugins, and should not in general be used by yours.

Layout

Lay out plugin/ files in the following sections, if applicable, separated by two blank lines:

Lay out autoload/ files in the following sections, if applicable, separated by two blank lines:

This is recommended convention and is not enforced.

Use the following shortcuts:

Errata

This section plumbs some of the darker corners of vimscript, explaining the language pathologies that you wish you didn’t have to know.

Compatibility Mode

If you don’t support vi-compatibility mode, fail gracefully.

When compatible is set, many vim features are not available. The vim feature which most commonly affects vimscript authors is line continuations.

If you want your plugin to work in vim with vi compatibility on, you will need to save the compatibility options at the beginning of each plugin file, clear them, and restore them at the end of each plugin file. See :help use-cpo-save for details.

Plugins that depend on maktaba generally don’t need to worry about compatible mode since maktaba currently just disables it, printing a warning.