I've started working in some projects that build upon Google Closure's framework. Trying to do things properly proper, and hoping to avoid messing with my Vim configuration, I installed Sublime Text and configured it to use Closure's style and linter, but I was constantly fighting with Sublime Text and didn't feel comfortable with it at all.
So I decided to invest a little bit of time in tweaking Vim to make it work with these style rules but at the same time not to destroy my configuration.
Before I detail how I did it, compare the two configurations:
- My default configuration was: tabs when pressing TAB, four space indentation, and JSHint.
- Closure requires: spaces, two spaces indentation, a maximum of 80 character lines, and gjslint (its own linter).
As you can see, they are the total opposite. How could I do this without adding 'hint' headers to every file specifying which settings to use? I finally devised this simple solution: I created a function to enter "Closure mode"! Whenever I need to write Closure-compliant code, I exit insert mode and type
That executes the function that changes everything:
Once that is run, my Vim is "ruined", i.e. my whitespace settings are lost and I'd need to change each value manually back to where they were if I wanted to edit some code in "my style". That, or open a new instance of Vim.
There's evidently room for improvement: abstract the function even more so I can toggle between both configurations (mine and Closure's), and map it to a certain keystroke combination, and even show a message in the status bar saying what is the current configuration, so if you have any idea of how to do that, feel free to experiment with the code :-)