mirror of
Fork 0

592 lines
16 KiB
Raw Normal View History

This project adds [CoffeeScript] support to vim. It covers syntax, indenting,
compiling, and more.
2012-08-16 23:41:25 -04:00
2012-08-16 23:41:25 -04:00
[CoffeeScript]: http://coffeescript.org/
2012-08-16 23:41:25 -04:00
## Table of Contents
2012-08-16 23:41:25 -04:00
- Installation
- [Requirements](#requirements)
- [Install using Pathogen](#install-using-pathogen)
- [Install using Vundle](#install-using-vundle)
- [Install from a Zip File](#install-from-a-zip-file)
- Coffee Commands
- [Compile to JavaScript](#compile-to-javascript)
- [Compile CoffeeScript Snippets](#coffeecompile-compile-coffeescript-snippets)
- [Live Preview Compiling](#coffeewatch-live-preview-compiling)
- [Run CoffeeScript Snippets](#coffeerun-run-coffeescript-snippets)
- [Lint your CoffeeScript](#coffeelint-lint-your-coffeescript)
- Extras
- [Literate CoffeeScript](#literate-coffeescript)
- [CoffeeScript in HTML](#coffeescript-in-html)
- [CoffeeScript in Haml](#coffeescript-in-haml)
- Configuration
- [Custom Autocmds](#custom-autocmds)
- [Configuration Variables](#configuration-variables)
- [Configure Syntax Highlighting](#configure-syntax-highlighting)
- [Tune Vim for CoffeeScript](#tune-vim-for-coffeescript)
2012-08-16 23:41:25 -04:00
## Requirements
2012-08-16 23:41:25 -04:00
- vim 7.4 or later
- coffee 1.2.0 or later
2012-08-16 23:41:25 -04:00
## Install using Pathogen
2012-08-16 23:41:25 -04:00
This project uses rolling releases based on git commits, so pathogen is a
natural fit for it. If you're already using pathogen, you can skip to step 4.
2012-08-16 23:41:25 -04:00
1. Install [pathogen.vim] into `~/.vim/autoload/` (see [pathogen's
readme][install-pathogen] for more information.)
2012-08-16 23:41:25 -04:00
[pathogen.vim]: http://www.vim.org/scripts/script.php?script_id=2332
[install-pathogen]: https://github.com/tpope/vim-pathogen#installation
2012-08-16 23:41:25 -04:00
2. Enable pathogen in your vimrc. Here's a bare-minimum vimrc that enables
all the features of `vim-coffee-script`:
2012-08-16 23:41:25 -04:00
call pathogen#infect()
syntax enable
filetype plugin indent on
2012-08-16 23:41:25 -04:00
If you already have a vimrc built up, just make sure it contains these calls,
in this order.
2012-08-16 23:41:25 -04:00
3. Create the directory `~/.vim/bundle/`:
2012-08-16 23:41:25 -04:00
mkdir ~/.vim/bundle
2012-08-16 23:41:25 -04:00
4. Clone the `vim-coffee-script` repo into `~/.vim/bundle/`:
2012-08-16 23:41:25 -04:00
git clone https://github.com/kchmck/vim-coffee-script.git ~/.vim/bundle/vim-coffee-script/
2012-08-16 23:41:25 -04:00
Updating takes two steps:
2012-08-16 23:41:25 -04:00
1. Change into `~/.vim/bundle/vim-coffee-script/`:
cd ~/.vim/bundle/vim-coffee-script
2012-08-16 23:41:25 -04:00
2. Pull in the latest changes:
git pull
2012-08-16 23:41:25 -04:00
## Install using Vundle
2012-08-16 23:41:25 -04:00
1. [Install Vundle] into `~/.vim/bundle/`.
2012-08-16 23:41:25 -04:00
[Install Vundle]: https://github.com/gmarik/vundle#quick-start
2012-08-16 23:41:25 -04:00
2. Configure your vimrc for Vundle. Here's a bare-minimum vimrc that enables all
the features of `vim-coffee-script`:
2012-08-16 23:41:25 -04:00
set nocompatible
filetype off
2012-08-16 23:41:25 -04:00
set rtp+=~/.vim/bundle/vundle/
call vundle#rc()
2012-08-16 23:41:25 -04:00
Plugin 'kchmck/vim-coffee-script'
2012-08-16 23:41:25 -04:00
syntax enable
filetype plugin indent on
2012-08-16 23:41:25 -04:00
If you're adding Vundle to a built-up vimrc, just make sure all these calls
are in there and that they occur in this order.
3. Open vim and run `:PluginInstall`.
2012-08-16 23:41:25 -04:00
To update, open vim and run `:PluginInstall!` (notice the bang!)
## Install from a Zip File
1. Download the latest zip file from [vim.org][zip].
2. Extract the archive into `~/.vim/`:
2012-08-16 23:41:25 -04:00
unzip -od ~/.vim/ ARCHIVE.zip
2012-08-16 23:41:25 -04:00
This should create the files `~/.vim/autoload/coffee.vim`,
`~/.vim/compiler/coffee.vim`, etc.
2012-08-16 23:41:25 -04:00
You can update the plugin using the same steps.
[zip]: http://www.vim.org/scripts/script.php?script_id=3590
## Compile to JavaScript
A `coffee` wrapper for use with `:make` is enabled automatically for coffee
files if no other compiler is loaded. To enable it manually, run
2012-08-16 23:41:25 -04:00
:compiler coffee
The `:make` command is then configured to use the `coffee` compiler and
recognize its errors. I've included a quick reference here but be sure to check
out [`:help :make`][make] for a full reference of the command.
2012-08-16 23:41:25 -04:00
2012-08-16 23:41:25 -04:00
![make Result](http://i.imgur.com/eGIjEdn.png)
2012-08-16 23:41:25 -04:00
[make]: http://vimdoc.sourceforge.net/htmldoc/quickfix.html#:make_makeprg
2012-08-16 23:41:25 -04:00
Consider the full signature of a `:make` call as
:[silent] make[!] [COFFEE-OPTIONS]...
By default `:make` shows all compiler output and jumps to the first line
reported as an error. Compiler output can be hidden with a leading `:silent`:
:silent make
Line-jumping can be turned off by adding a bang:
`COFFEE-OPTIONS` given to `:make` are passed along to `coffee` (see also
:make --bare --output /some/dir
See the [full table of options](http://coffeescript.org/#usage) for a
list of all the options that `coffee` recognizes.
*Configuration*: [`coffee_compiler`](#coffee_compiler),
#### The quickfix window
Compiler errors are added to the [quickfix] list by `:make`, but the quickfix
window isn't automatically shown. The [`:cwindow`][cwindow] command will pop up
the quickfix window if there are any errors:
This is usually the desired behavior, so you may want to add an autocmd to your
vimrc to do this automatically:
autocmd QuickFixCmdPost * nested cwindow | redraw!
2012-08-16 23:41:25 -04:00
The `redraw!` command is needed to fix a redrawing quirk in terminal vim, but
can removed for gVim.
[quickfix]: http://vimdoc.sourceforge.net/htmldoc/quickfix.html#quickfix
[cwindow]: http://vimdoc.sourceforge.net/htmldoc/quickfix.html#:cwindow
2012-08-16 23:41:25 -04:00
#### Recompile on write
2012-08-16 23:41:25 -04:00
To recompile a file when it's written, add a `BufWritePost` autocmd to your
2012-08-16 23:41:25 -04:00
autocmd BufWritePost *.coffee silent make!
2012-08-16 23:41:25 -04:00
#### Cake and Cakefiles
2012-08-16 23:41:25 -04:00
A `cake` compiler is also available with the call
2012-08-16 23:41:25 -04:00
:compiler cake
2012-08-16 23:41:25 -04:00
You can then use `:make` as above to run your Cakefile and capture any `coffee`
2012-08-16 23:41:25 -04:00
:silent make build
2012-08-16 23:41:25 -04:00
It runs within the current directory, so make sure you're in the directory of
your Cakefile before calling it.
2012-08-16 23:41:25 -04:00
*Configuration*: [`coffee_cake`](#coffee_cake),
2012-08-16 23:41:25 -04:00
## CoffeeCompile: Compile CoffeeScript Snippets
2012-08-16 23:41:25 -04:00
CoffeeCompile shows how the current file or a snippet of CoffeeScript is
compiled to JavaScript.
2012-08-16 23:41:25 -04:00
:[RANGE] CoffeeCompile [vert[ical]] [WINDOW-SIZE]
2012-08-16 23:41:25 -04:00
Calling `:CoffeeCompile` without a range compiles the whole file:
2012-08-16 23:41:25 -04:00
2012-08-16 23:41:25 -04:00
![CoffeeCompile Result](http://i.imgur.com/bpiAxaa.png)
2012-08-16 23:41:25 -04:00
Calling it with a range, like in visual mode, compiles only the selected snippet
of CoffeeScript:
2012-08-16 23:41:25 -04:00
![CoffeeCompile Snippet](http://i.imgur.com/x3OT3Ay.png)
2012-08-16 23:41:25 -04:00
![Compiled Snippet](http://i.imgur.com/J02j4T8.png)
2012-08-16 23:41:25 -04:00
Each file gets its own CoffeeCompile buffer, and the same buffer is used for all
future calls of `:CoffeeCompile` on that file. It can be quickly closed by
hitting `q` in normal mode.
Using `vert` opens the CoffeeCompile buffer vertically instead of horizontally
(see also [`coffee_compile_vert`](#coffee_compile_vert)):
:CoffeeCompile vert
By default the CoffeeCompile buffer splits the source buffer in half, but this
can be overridden by passing in a `WINDOW-SIZE`:
2012-08-16 23:41:25 -04:00
:CoffeeCompile 4
*Configuration*: [`coffee_compiler`](#coffee_compiler`),
#### Quick syntax checking
If compiling a snippet results in a compiler error, CoffeeCompile adds that
error to the [quickfix] list.
[quickfix]: http://vimdoc.sourceforge.net/htmldoc/quickfix.html#quickfix
![Syntax Checking](http://i.imgur.com/RC8accF.png)
![Syntax Checking Result](http://i.imgur.com/gi1ON75.png)
2012-08-16 23:41:25 -04:00
You can use this to quickly check the syntax of a snippet.
2012-08-16 23:41:25 -04:00
## CoffeeWatch: Live Preview Compiling
2012-08-16 23:41:25 -04:00
CoffeeWatch emulates using the Try CoffeeScript preview box on the [CoffeeScript
2012-08-16 23:41:25 -04:00
2012-08-16 23:41:25 -04:00
![CoffeeWatch Result](http://i.imgur.com/rJbOeeS.png)
2012-08-16 23:41:25 -04:00
CoffeeWatch takes the same options as CoffeeCompile:
2012-08-16 23:41:25 -04:00
:CoffeeWatch [vert[ical]] [WINDOW-SIZE]
2012-08-16 23:41:25 -04:00
After a source buffer is watched, leaving insert mode or saving the file fires
off a recompile of the CoffeeScript:
![Insert Mode](http://i.imgur.com/SBVcf4k.png)
You can force recompilation by calling `:CoffeeWatch`.
To get synchronized scrolling of the source buffer and CoffeeWatch buffer, set
on each:
2012-08-16 23:41:25 -04:00
:setl scrollbind
*Configuration*: [`coffee_compiler`](#coffee_compiler),
## CoffeeRun: Run CoffeeScript Snippets
CoffeeRun compiles the current file or selected snippet and runs the resulting
![CoffeeRun Output](http://i.imgur.com/wZQbggN.png)
The command has two forms:
This form applies when no `RANGE` is given or when the given range is `1,$`
(first line to last line). It allows passing `PROGRAM-OPTIONS` to your compiled
program. The filename is passed directly to `coffee` so you must save the file
for your changes to take effect.
This form applies with all other ranges. It compiles and runs the lines within
the given `RANGE` and any extra `COFFEE-OPTIONS` are passed to `coffee`.
*Configuration*: [`coffee_compiler`](#coffee_compiler),
## CoffeeLint: Lint your CoffeeScript
CoffeeLint runs [coffeelint](http://www.coffeelint.org/) (version 1.4.0 or later
required) on the current file and adds any issues to the [quickfix] list.
![CoffeeLint Result](http://i.imgur.com/9hSIj3W.png)
:[RANGE] CoffeeLint[!] [COFFEELINT-OPTIONS]... [ | cwindow]
If a `RANGE` is given, only those lines are piped to `coffeelint`. Options given
in `COFFEELINT-OPTIONS` are passed to `coffeelint` (see also
:CoffeeLint -f lint.json
It behaves very similar to `:make`, described [above](#compile-to-javascript).
:CoffeeLint! | cwindow
*Configuration*: [`coffee_linter`](#coffee_linter),
2012-08-16 23:41:25 -04:00
## Literate CoffeeScript
2012-08-16 23:41:25 -04:00
2017-03-07 12:04:28 -05:00
Literate CoffeeScript syntax and indent support is now built in! The `Coffee`
commands detect when they're running on a litcoffee file and pass the
`--literate` flag to their respective tools.
Literate CoffeeScript syntax and indent support was written by @mintplant
(Michael Smith). A standalone repo
[exists](https://github.com/jwhitley/vim-literate-coffeescript), but you'll
need to copy the `ftplugin/litcoffee.vim` file or set up an autocmd to get the
`Coffee` commands to be automatically loaded for litcoffee files.
## CoffeeScript in HTML
CoffeeScript is highlighted and indented within
<script type="text/coffeescript">
blocks in html files.
## CoffeeScript in Haml
CoffeeScript is highlighted within the `:coffeescript` filter in haml files:
console.log "hullo"
At this time, coffee indenting doesn't work in these blocks.
## Custom Autocmds
You can [define commands][autocmd-explain] to be ran automatically on these
custom events.
In all cases, the name of the command running the event (`CoffeeCompile`,
`CoffeeWatch`, or `CoffeeRun`) is matched by the [`{pat}`][autocmd] argument.
You can match all commands with a `*` or only specific commands by separating
them with a comma: `CoffeeCompile,CoffeeWatch`.
[autocmd-explain]: http://vimdoc.sourceforge.net/htmldoc/usr_40.html#40.3
[autocmd]: http://vimdoc.sourceforge.net/htmldoc/autocmd.html#:autocmd
#### CoffeeBufNew
CoffeeBufNew is ran when a new scratch buffer is created. It's called from the
new buffer, so it can be used to do additional set up.
augroup CoffeeBufNew
autocmd User * set wrap
augroup END
*Used By*: CoffeeCompile, CoffeeWatch, CoffeeRun
#### CoffeeBufUpdate
CoffeeBufUpdate is ran when a scratch buffer is updated with output from
`coffee`. It's called from the scratch buffer, so it can be used to alter the
compiled output.
" Switch back to the source buffer after updating.
augroup CoffeeBufUpdate
autocmd User CoffeeCompile,CoffeeRun exec bufwinnr(b:coffee_src_buf) 'wincmd w'
augroup END
For example, to strip off the "Generated by" comment on the first line, put this
in your vimrc:
function! s:RemoveGeneratedBy()
" If there was an error compiling, there's no comment to remove.
if v:shell_error
" Save cursor position.
let pos = getpos('.')
" Remove first line.
set modifiable
1 delete _
set nomodifiable
" Restore cursor position.
call setpos('.', pos)
augroup CoffeeBufUpdate
autocmd User CoffeeCompile,CoffeeWatch call s:RemoveGeneratedBy()
augroup END
*Used By*: CoffeeCompile, CoffeeWatch, CoffeeRun
## Configuration Variables
This is the full list of configuration variables available, with example
settings and default values. Use these in your vimrc to control the default
#### coffee\_indent\_keep\_current
By default, the indent function matches the indent of the previous line if it
doesn't find a reason to indent or outdent. To change this behavior so it
instead keeps the [current indent of the cursor][98], use
let coffee_indent_keep_current = 1
[98]: https://github.com/kchmck/vim-coffee-script/pull/98
*Default*: `unlet coffee_indent_keep_current`
Note that if you change this after a coffee file has been loaded, you'll have to
reload the indent script for the change to take effect:
unlet b:did_indent | runtime indent/coffee.vim
#### coffee\_compiler
Path to the `coffee` executable used by the `Coffee` commands:
let coffee_compiler = '/usr/bin/coffee'
*Default*: `'coffee'` (search `$PATH` for executable)
#### coffee\_make\_options
Options to pass to `coffee` with `:make`:
let coffee_make_options = '--bare'
*Default*: `''` (nothing)
Note that `coffee_make_options` is embedded into `'makeprg'`, so `:compiler
coffee` must be ran after changing `coffee_make_options` for the changes to take
#### coffee\_cake
Path to the `cake` executable:
let coffee_cake = '/opt/bin/cake'
*Default*: `'cake'` (search `$PATH` for executable)
#### coffee\_cake\_options
Options to pass to `cake` with `:make`:
let coffee_cake_options = 'build'
*Default*: `''` (nothing)
#### coffee\_linter
Path to the `coffeelint` executable:
let coffee_linter = '/opt/bin/coffeelint'
*Default*: `'coffeelint'` (search `$PATH` for executable)
#### coffee\_lint\_options
Options to pass to `coffeelint`:
2012-08-16 23:41:25 -04:00
let coffee_lint_options = '-f lint.json'
*Default*: `''` (nothing)
#### coffee\_compile\_vert
Open the CoffeeCompile buffer with a vertical split instead of a horizontal
let coffee_compile_vert = 1
*Default*: `unlet coffee_compile_vert`
#### coffee\_watch\_vert
2012-08-16 23:41:25 -04:00
Open the CoffeeWatch buffer with a vertical split instead of a horizontal
2012-08-16 23:41:25 -04:00
let coffee_watch_vert = 1
2012-08-16 23:41:25 -04:00
*Default*: `unlet coffee_watch_vert`
2012-08-16 23:41:25 -04:00
#### coffee\_run\_vert
2012-08-16 23:41:25 -04:00
Open the CoffeeRun buffer with a vertical split instead of a horizontal
2012-08-16 23:41:25 -04:00
let coffee_run_vert = 1
2012-08-16 23:41:25 -04:00
*Default*: `unlet coffee_run_vert`
2012-08-16 23:41:25 -04:00
## Configure Syntax Highlighting
2012-08-16 23:41:25 -04:00
Add these lines to your vimrc to disable the relevant syntax group.
2012-08-16 23:41:25 -04:00
#### Disable trailing whitespace error
Trailing whitespace is highlighted as an error by default. This can be disabled
hi link coffeeSpaceError NONE
#### Disable trailing semicolon error
Trailing semicolons are considered an error (for help transitioning from
2012-08-16 23:41:25 -04:00
JavaScript.) This can be disabled with:
hi link coffeeSemicolonError NONE
#### Disable reserved words error
Reserved words like `function` and `var` are highlighted as an error where
they're not allowed in CoffeeScript. This can be disabled with:
hi link coffeeReservedError NONE
## Tune Vim for CoffeeScript
2012-08-16 23:41:25 -04:00
Changing these core settings can make vim more CoffeeScript friendly.
#### Fold by indentation
Folding by indentation works well for CoffeeScript functions and classes:
2012-08-16 23:41:25 -04:00
To fold by indentation in CoffeeScript files, add this line to your vimrc:
2012-08-16 23:41:25 -04:00
autocmd BufNewFile,BufReadPost *.coffee setl foldmethod=indent nofoldenable
2012-08-16 23:41:25 -04:00
With this, folding is disabled by default but can be quickly toggled per-file
by hitting `zi`. To enable folding by default, remove `nofoldenable`:
autocmd BufNewFile,BufReadPost *.coffee setl foldmethod=indent
2012-08-16 23:41:25 -04:00
#### Two-space indentation
To get standard two-space indentation in CoffeeScript files, add this line to
your vimrc:
2012-08-16 23:41:25 -04:00
autocmd BufNewFile,BufReadPost *.coffee setl shiftwidth=2 expandtab