Giter Club home page Giter Club logo

clean-vimscript's Introduction

Clean Vimscript

A collection of notes on how to write clean and idiomatic Vimscript.

This used to be a collection of mental notes. Until I decided to write them down.

Best Practices

Don't touch the system runtime files

User configuration should be in the user directory. This is your $HOME and all your configuration should be in there. The files below $VIRUNTIME should not be touched by the user.

Rationale: Everything in $VIMRUNTIME is there for a reason. It controls Vim for all users on the system. Even with only one user, changes in $VIMRUNTIME will usually not be versioned. They will be hard to track, hard to find and create behavior that cannot be reproduced on other (clean) machines.

Put vimrc below .vim

User configuration should be in a directory .vim or _vimfiles below your $HOME directory. This goes first and foremost for the vimrc file.

Rationale: Putting everything-Vim into a directory of its own makes it trivially easy to use version control on it. Just git init in there. You will be able to track changes and clone your configuration to other machines.

See :help vimfiles for places to put the configuration and :help xdg-vimrcfor an XDG compliant way to do so.

Put tweaks in after

Changes from the global default runtime files should be in $HOME/.vim/after/<somer_dir>/<some_file>.

Rationale: The runtime files in after will be sourced after the ones in the global runtime directory. This will allow you to override or tweak global defaults without losing all of them or changing them.

According to `:help vimfiles':

In the "after" directory in your home directory. This is for personal preferences to overrule or add to the distributed defaults or system-wide settings (rarely needed).

The "rarely needed" seems to be a huge understatement.

Use :nnoremap by default

When creating a mapping, use :nnoremap unless you explicitly need another command from the :map family of commands.

Rationale: In most cases, :nnoremap is exactly what you want. It's also minimal in scope and effect. This guards against unwanted mappings.

Prefer :normal! to :normal

When running normal mode commands from the command line, prefer normal-bang to normal.

Rationale: The bang-form of normal will not execute any mappings on its {rhs}, making sure default functionality will be used. This is more robust to mappings providing their own spin on default functionality.

Make functions abort

When defining a :function, follow it with the abort argument. This will make the function abort if errors are encountered during its execution.

Rationale: In most cases, it's no use to keep going after a failure. In the best case, nothing happens, in the worst, garbage is created. Errors during a function's execution should be anticipated and handled explicitly.

Make commands accept bar

When defining a user :command, provide the -bar option unless there's a good reason not to.

Rationale: Providing -bar will allow commands to be chained using the | or <bar> operators. This is what you'd expect from any command. Exceptions are commands that accept a variable number of arguments.

Debugging

You wrote some Vimscript but it's not working yet. This section covers some techniques to narrow down the bug.

Is Vim broken or is it the config?

Retry whatever it is that's not working with vim -N -u NONE to start Vim without any configuration. You may :source your configuration files one by one until you can reproduce your issue.

A related technique is to start vim --clean for default configuration.

Run a linter

There's the vint linter for vimscript. Run it on your code and fix any issues that crop up.

clean-vimscript's People

Contributors

frdr avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.