Altering Vim config for Javascript semistandard

TLDR; here’s a snapshot of what you need in ~/.vimrc:

If you’ve reached this page then you must be a). Team Vim, and b). wondering how to modify your Vim configuration for Javascript, maybe to comply with the semistandard linter and the Holberton checker.

Congratulations for your good taste.

The semistandard linter requires Javascript code to be indented by two spaces and does not allow newlines at the end of files. If your code doesn’t comply, you’ll see errors similar to the following:

Thankfully, it’s not all that difficult to configure Vim to automatically indent and remove newlines when we’re editing Javascript files. No need to fear the Vim…

Vim will generally expect to find the configuration file located in your home directory. If you already have settings in there, you should be able to just append what you need to the end. If the file doesn’t already exist, that’s okay, you can create it from scratch. Just make sure it’s at ~/.vimrc

I have a few settings in mine already. `set number` automatically opens vim with the line numbers visible, but this is just personal preference. As you can see, I’ve set up vim to behave differently based on three different languages. If you’re a fellow Holberton student, these settings are useful to help your code comply with Betty for C, pep8 for Python, and now semistandard for Javascript.

Setting the tabstop parameter will adjust how long your tabs are, measured in spaces. For Javascript I have this set to 2. However, the tabs won’t automatically expand to spaces unless you tell Vim to do so. The expandtab parameter will automatically replace tabs with spaces. This is required for both pep8 and semistandard, and hence I have it configured for both python and javascript. Even if you set your tab length to be two spaces long but still formatted as tabs, semistandard will trip if they haven’t been expanded into spaces.

Make sure they are converted into spaces with the noexpandtab parameter!

Additionally, Vim generally appends an extra line by default. Most of Holberton’s curriculum expects this behavior. However, the semistandard linter isn’t happy with newlines at the end, so the nofixendofline setting will prevent this.

To modify .vimrc file just for Javascript this should be all you need:

Happy coding!