分类: 系统运维
2012-03-28 15:39:03
ref:http://blog.deadpansincerity.com/2011/05/setting-up-emacs-as-a-javascript-editing-environment-for-fun-and-profit/
I’ve been doing a lot of Javascript lately, which has naturally led to a whole lot of trips down the .emacs rabbit-hole
So, here are the steps you need to turn OOTB Emacs into the perfect environment for working on Javascript:
Auto-completionFirst up, you’re going to want decent auto-completion, so install the straightforwardly named . Due to some mode-name aliasing that goes on in the Emacs23 javascript mode, You’ll need to make sure it can find the relevant Javascript dictionary so
and then add to your .emacs:
You now have auto-completion for all variable names in all open Javascript buffers, as well as for standard syntax words (setTimeout, parseInt, onFocus etc).
SnippetingNot content with that, we’ll also want some kind of intelligent snippet package right – how many times do you really want to type all those semicolons and curly braces? Once is good for me.
The excellent provides this (check out the if you’ve not come across it before)
Once you’ve got that downloaded and installed, you’ll want some Javascript snippets to load in, the ones I use, for standard Javascript and jQuery can be downloaded from , and then the directory provided by that tarfile needs to go in yasnippet/snippets/text-mode
So for our .emacs:
One of the problems with Javascript programming is that the feedback loop for syntax errors is *slow* – if you’re working with the web you have to wait for a whole pageload. So we’ll want on-the-fly error checking.
originally wrote , which runs a lighthweight node.js server that passes your file to to integrate with – the Emacs solution for on-the-fly syntax checking. The fork I’m currently maintaining at contains some enhancements such as automatic initialization and JSLint options passed from Emacs that the original has yet to merge in. Given that JSLint frequently complains about things that you might like it to er, not complain about, the ability to change the options with the lintnode-jslint-excludes variable makes this far more useable.
So:
Install and (The package manager for node)
Get lintnode and the dependencies
Then we need this in our .emacs:
Syntax errors will now be given a red background without having to leave the comfort of Emacs.
Even better, if we install the package, we get the error message in the minibuffer when the cursor is on a line with an error.
Get the source from http://www.emacswiki.org/emacs/flymake-cursor.el and then add to your .emacs:
Edit:
As Jesse and Magnar point out in the comments, it’s probably a better idea to not exclude ‘undef from the jslint errors, but rather to declare global variables at the top of your .js file
This way you can let jslint know about other libraries you are using, but still get the checks for undefined variables in your own code.
Code foldingEspecially if you’re dealing with large projects, the ability to hide portions of code can be great for maintaining focus. Emacs comes with this built in, but you have to enable it, so returning to our .emacs:
This means that when your cursor is in the function…
… and you M-x hs-hide-block, the function turns into:
To my taste the default keybinding of C-c @ C-h to hide blocks is somewhat perverse, so I’d recommend this in your .emacs:
YMMV.
Javascript ConsoleHaving a REPL for the language you’re working with close to hand is basically required for me to enjoy working with it. Now that we’ve installed node.js, though, we can have us a Javascript console inside Emacs with …
Once you’ve grabbed the file, your .emacs should have:
You can then run a Javascript REPL with M-x run-js and also send portions of files by selecting them and then running M-x send-region. Neat.
By way of conclusionThere are a few other tweaks that make my Javascripting just that little bit nicer, but they’re more personal Emacs setup than anything specific to Javascript. So, there you have it, the perfect Javascript editing environment.
Love regards etc