Google Vimscript Guide

Revision 1.1

Nate Soares
Artemis Sparks
David Barnett

Each style point has a summary for which additional information is available by toggling the accompanying arrow button that looks this way: ▶. You may toggle all summaries with the big arrow button:

▶ Toggle all summaries
Table of Contents

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

link ▶
Separate library-providing plugins from command-providing plugins.

Configuration

link ▶
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

link ▶
Use vimdoc.

Whitespace

link ▶
Follow google-wide conventions.

Variables

link ▶

plugin-names-like-this, FunctionNamesLikeThis, CommandNamesLikeThis, augroup_names_like_this, variable_names_like_this.

Prefix all variables with their scope.

Strings

link ▶
Prefer single quotes.

Settings

link ▶
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

link ▶
  • Define in plugin/commands.vim.
  • CommandNamesLikeThis.
  • Prefer semantic names to a unified prefix.
  • Do not use [!]
  • Extract logic into functions.

Autocommands

link ▶
  • Define in plugin/autocmds.vim.
  • Use augroups.
  • augroup_names_like_this.
  • Clear the augroup first.
  • Extract logic into functions.

Functions

link ▶
  • FunctionNamesLikeThis.
  • Autoload all functions.
  • Prefix script-local functions with s:
  • Use [!].
  • Use [abort].

Mappings

link ▶
  • Provide opt-in key mappings in plugin/mappings.vim.
  • <Plug> mappings can be defined in plugin/plugs.vim (unlike mappings.vim, plugs.vim is opt-out).

Conventions

Dependency Checking

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

Statusline Flags

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

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

link ▶
If you don't support vi-compatibility mode, fail gracefully.

Revision 1.1

Nate Soares
Artemis Sparks
David Barnett