This project adds CoffeeScript support to the vim editor. It handles syntax,
indenting, and compiling. Also included is an eco syntax and support for
text/coffeescript
in html.
This is the quickest way to get things running.
-
Download the latest zipball over at vim.org.
-
Extract the archive into
~/.vim/
:unzip -od ~/.vim vim-coffee-script-HASH.zip
These steps are also used to update the plugin.
Since this plugin uses "rolling releases" based on git commits, using pathogen
and git is the preferred way to install. The plugin ends up contained in its own
directory, and updates are just a git pull
away.
-
Install tpope's pathogen into
~/.vim/autoload/
and add this line to yourvimrc
:call pathogen#infect()
To get the all the features of this plugin, make sure you also have a
filetype plugin indent on
line.
-
Create, and change into, the
~/.vim/bundle/
directory:$ mkdir ~/.vim/bundle $ cd ~/.vim/bundle
-
Make a clone of the
vim-coffee-script
repository:$ git clone https://github.com/kchmck/vim-coffee-script.git
-
Change into the
~/.vim/bundle/vim-coffee-script/
directory:$ cd ~/.vim/bundle/vim-coffee-script
-
Pull in the latest changes:
$ git pull
The CoffeeMake
command compiles the current file and parses any errors:
The full signature of the command is:
:[silent] CoffeeMake[!] [COFFEE-OPTS]...
By default, CoffeeMake
shows all compiler output and jumps to the first line
reported as an error by coffee
:
:CoffeeMake
Compiler output can be hidden with silent
:
:silent CoffeeMake
Line-jumping can be turned off by adding a bang:
:CoffeeMake!
Options given to CoffeeMake
are passed along to coffee
:
:CoffeeMake --bare
To get autocompiling when a file is written, add an autocmd
like this to your
vimrc
:
au BufWritePost *.coffee silent CoffeeMake!
All of the customizations above can be used, too. This one compiles silently
and with the -b
option, but shows any errors:
au BufWritePost *.coffee silent CoffeeMake! -b | cwindow | redraw!
The redraw!
command is needed to fix a redrawing quirk in terminal vim, but
can removed for gVim.
The CoffeeMake
command passes any options in the coffee_make_options
variable along to the compiler. This can be used to set default options:
let coffee_make_options = "--bare"
The CoffeeCompile
command shows how the current file or a snippet of
CoffeeScript is compiled to JavaScript. Calling CoffeeCompile
without a range
compiles the whole file:
Calling CoffeeCompile
with a range, like in visual mode, compiles the selected
snippet of CoffeeScript:
This scratch buffer can be quickly closed by hitting the q
key.
The CoffeeRun
command compiles the current file or selected snippet and runs
the resulting JavaScript. Output is shown at the bottom of the screen:
You can configure plugin behavior by adding the relevant let
statements to
your vimrc
.
Trailing whitespace is highlighted as an error by default. This can be disabled with:
let coffee_no_trailing_space_error = 1
Trailing semicolons are also considered an error (for help transitioning from JavaScript.) This can be disabled with:
let coffee_no_trailing_semicolon_error = 1
Reserved words like function
and var
are highlighted as an error in contexts
disallowed by CoffeeScript. This can be disabled with:
let coffee_no_reserved_words_error = 1
Changing these core settings can make vim more CoffeeScript friendly.
Folding by indentation is a good fit for CoffeeScript functions and classes:
To fold by indentation in CoffeeScript files, add this line to your vimrc
:
au BufNewFile,BufReadPost *.coffee setl foldmethod=indent nofoldenable
With this, folding is disabled by default but can be quickly toggled per-file
by hitting zi
. To enable it by default, remove nofoldenable
:
au BufNewFile,BufReadPost *.coffee setl foldmethod=indent
To get standard two-space indentation in CoffeeScript files, add this line to
your vimrc
:
au BufNewFile,BufReadPost *.coffee setl shiftwidth=2 expandtab