Simple image host.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Readme.md 16KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599
  1. This project adds [CoffeeScript] support to vim. It covers syntax, indenting,
  2. compiling, and more.
  3. ![Screenshot](http://i.imgur.com/j1BhpZQ.png)
  4. [CoffeeScript]: http://coffeescript.org/
  5. ## Table of Contents
  6. - Installation
  7. - [Requirements](#requirements)
  8. - [Install using Pathogen](#install-using-pathogen)
  9. - [Install using Vundle](#install-using-vundle)
  10. - [Install from a Zip File](#install-from-a-zip-file)
  11. - Coffee Commands
  12. - [Compile to JavaScript](#compile-to-javascript)
  13. - [Compile CoffeeScript Snippets](#coffeecompile-compile-coffeescript-snippets)
  14. - [Live Preview Compiling](#coffeewatch-live-preview-compiling)
  15. - [Run CoffeeScript Snippets](#coffeerun-run-coffeescript-snippets)
  16. - [Lint your CoffeeScript](#coffeelint-lint-your-coffeescript)
  17. - Extras
  18. - [Literate CoffeeScript](#literate-coffeescript)
  19. - [CoffeeScript in HTML](#coffeescript-in-html)
  20. - [CoffeeScript in Haml](#coffeescript-in-haml)
  21. - Configuration
  22. - [Custom Autocmds](#custom-autocmds)
  23. - [Configuration Variables](#configuration-variables)
  24. - [Configure Syntax Highlighting](#configure-syntax-highlighting)
  25. - [Tune Vim for CoffeeScript](#tune-vim-for-coffeescript)
  26. ## Requirements
  27. - vim 7.4 or later
  28. - coffee 1.2.0 or later
  29. ## Install using Pathogen
  30. This project uses rolling releases based on git commits, so pathogen is a
  31. natural fit for it. If you're already using pathogen, you can skip to step 4.
  32. 1. Install [pathogen.vim] into `~/.vim/autoload/` (see [pathogen's
  33. readme][install-pathogen] for more information.)
  34. [pathogen.vim]: http://www.vim.org/scripts/script.php?script_id=2332
  35. [install-pathogen]: https://github.com/tpope/vim-pathogen#installation
  36. 2. Enable pathogen in your vimrc. Here's a bare-minimum vimrc that enables
  37. all the features of `vim-coffee-script`:
  38. ```vim
  39. call pathogen#infect()
  40. syntax enable
  41. filetype plugin indent on
  42. ```
  43. If you already have a vimrc built up, just make sure it contains these calls,
  44. in this order.
  45. 3. Create the directory `~/.vim/bundle/`:
  46. mkdir ~/.vim/bundle
  47. 4. Clone the `vim-coffee-script` repo into `~/.vim/bundle/`:
  48. git clone https://github.com/kchmck/vim-coffee-script.git ~/.vim/bundle/vim-coffee-script/
  49. Updating takes two steps:
  50. 1. Change into `~/.vim/bundle/vim-coffee-script/`:
  51. cd ~/.vim/bundle/vim-coffee-script
  52. 2. Pull in the latest changes:
  53. git pull
  54. ## Install using Vundle
  55. 1. [Install Vundle] into `~/.vim/bundle/`.
  56. [Install Vundle]: https://github.com/gmarik/vundle#quick-start
  57. 2. Configure your vimrc for Vundle. Here's a bare-minimum vimrc that enables all
  58. the features of `vim-coffee-script`:
  59. ```vim
  60. set nocompatible
  61. filetype off
  62. set rtp+=~/.vim/bundle/vundle/
  63. call vundle#rc()
  64. Plugin 'kchmck/vim-coffee-script'
  65. syntax enable
  66. filetype plugin indent on
  67. ```
  68. If you're adding Vundle to a built-up vimrc, just make sure all these calls
  69. are in there and that they occur in this order.
  70. 3. Open vim and run `:PluginInstall`.
  71. To update, open vim and run `:PluginInstall!` (notice the bang!)
  72. ## Install from a Zip File
  73. 1. Download the latest zip file from [vim.org][zip].
  74. 2. Extract the archive into `~/.vim/`:
  75. unzip -od ~/.vim/ ARCHIVE.zip
  76. This should create the files `~/.vim/autoload/coffee.vim`,
  77. `~/.vim/compiler/coffee.vim`, etc.
  78. You can update the plugin using the same steps.
  79. [zip]: http://www.vim.org/scripts/script.php?script_id=3590
  80. ## Compile to JavaScript
  81. A `coffee` wrapper for use with `:make` is enabled automatically for coffee
  82. files if no other compiler is loaded. To enable it manually, run
  83. :compiler coffee
  84. The `:make` command is then configured to use the `coffee` compiler and
  85. recognize its errors. I've included a quick reference here but be sure to check
  86. out [`:help :make`][make] for a full reference of the command.
  87. ![make](http://i.imgur.com/scUXmxR.png)
  88. ![make Result](http://i.imgur.com/eGIjEdn.png)
  89. [make]: http://vimdoc.sourceforge.net/htmldoc/quickfix.html#:make_makeprg
  90. Consider the full signature of a `:make` call as
  91. :[silent] make[!] [COFFEE-OPTIONS]...
  92. By default `:make` shows all compiler output and jumps to the first line
  93. reported as an error. Compiler output can be hidden with a leading `:silent`:
  94. :silent make
  95. Line-jumping can be turned off by adding a bang:
  96. :make!
  97. `COFFEE-OPTIONS` given to `:make` are passed along to `coffee` (see also
  98. [`coffee_make_options`](#coffee_make_options)):
  99. :make --bare --output /some/dir
  100. See the [full table of options](http://coffeescript.org/#usage) for a
  101. list of all the options that `coffee` recognizes.
  102. *Configuration*: [`coffee_compiler`](#coffee_compiler),
  103. [`coffee_make_options`](#coffee_make_options)
  104. #### The quickfix window
  105. Compiler errors are added to the [quickfix] list by `:make`, but the quickfix
  106. window isn't automatically shown. The [`:cwindow`][cwindow] command will pop up
  107. the quickfix window if there are any errors:
  108. :make
  109. :cwindow
  110. This is usually the desired behavior, so you may want to add an autocmd to your
  111. vimrc to do this automatically:
  112. autocmd QuickFixCmdPost * nested cwindow | redraw!
  113. The `redraw!` command is needed to fix a redrawing quirk in terminal vim, but
  114. can removed for gVim.
  115. [quickfix]: http://vimdoc.sourceforge.net/htmldoc/quickfix.html#quickfix
  116. [cwindow]: http://vimdoc.sourceforge.net/htmldoc/quickfix.html#:cwindow
  117. #### Recompile on write
  118. To recompile a file when it's written, add a `BufWritePost` autocmd to your
  119. vimrc:
  120. autocmd BufWritePost *.coffee silent make!
  121. #### Cake and Cakefiles
  122. A `cake` compiler is also available with the call
  123. :compiler cake
  124. You can then use `:make` as above to run your Cakefile and capture any `coffee`
  125. errors:
  126. :silent make build
  127. It runs within the current directory, so make sure you're in the directory of
  128. your Cakefile before calling it.
  129. *Configuration*: [`coffee_cake`](#coffee_cake),
  130. [`coffee_cake_options`](#coffee_cake_options)
  131. ## CoffeeCompile: Compile CoffeeScript Snippets
  132. CoffeeCompile shows how the current file or a snippet of CoffeeScript is
  133. compiled to JavaScript.
  134. :[RANGE] CoffeeCompile [vert[ical]] [WINDOW-SIZE]
  135. Calling `:CoffeeCompile` without a range compiles the whole file:
  136. ![CoffeeCompile](http://i.imgur.com/0zFG0l0.png)
  137. ![CoffeeCompile Result](http://i.imgur.com/bpiAxaa.png)
  138. Calling it with a range, like in visual mode, compiles only the selected snippet
  139. of CoffeeScript:
  140. ![CoffeeCompile Snippet](http://i.imgur.com/x3OT3Ay.png)
  141. ![Compiled Snippet](http://i.imgur.com/J02j4T8.png)
  142. Each file gets its own CoffeeCompile buffer, and the same buffer is used for all
  143. future calls of `:CoffeeCompile` on that file. It can be quickly closed by
  144. hitting `q` in normal mode.
  145. Using `vert` opens the CoffeeCompile buffer vertically instead of horizontally
  146. (see also [`coffee_compile_vert`](#coffee_compile_vert)):
  147. :CoffeeCompile vert
  148. By default the CoffeeCompile buffer splits the source buffer in half, but this
  149. can be overridden by passing in a `WINDOW-SIZE`:
  150. :CoffeeCompile 4
  151. *Configuration*: [`coffee_compiler`](#coffee_compiler`),
  152. [`coffee_compile_vert`](#coffee_compile_vert)
  153. #### Quick syntax checking
  154. If compiling a snippet results in a compiler error, CoffeeCompile adds that
  155. error to the [quickfix] list.
  156. [quickfix]: http://vimdoc.sourceforge.net/htmldoc/quickfix.html#quickfix
  157. ![Syntax Checking](http://i.imgur.com/RC8accF.png)
  158. ![Syntax Checking Result](http://i.imgur.com/gi1ON75.png)
  159. You can use this to quickly check the syntax of a snippet.
  160. ## CoffeeWatch: Live Preview Compiling
  161. CoffeeWatch emulates using the Try CoffeeScript preview box on the [CoffeeScript
  162. homepage][CoffeeScript].
  163. ![CoffeeWatch](http://i.imgur.com/TRHdIMG.png)
  164. ![CoffeeWatch Result](http://i.imgur.com/rJbOeeS.png)
  165. CoffeeWatch takes the same options as CoffeeCompile:
  166. :CoffeeWatch [vert[ical]] [WINDOW-SIZE]
  167. After a source buffer is watched, leaving insert mode or saving the file fires
  168. off a recompile of the CoffeeScript:
  169. ![Insert Mode](http://i.imgur.com/SBVcf4k.png)
  170. ![Recompile](http://i.imgur.com/pbPMog7.png)
  171. You can force recompilation by calling `:CoffeeWatch`.
  172. To get synchronized scrolling of the source buffer and CoffeeWatch buffer, set
  173. [`'scrollbind'`](http://vimdoc.sourceforge.net/htmldoc/options.html#'scrollbind')
  174. on each:
  175. :setl scrollbind
  176. *Configuration*: [`coffee_compiler`](#coffee_compiler),
  177. [`coffee_watch_vert`](#coffee_watch_vert)
  178. ## CoffeeRun: Run CoffeeScript Snippets
  179. CoffeeRun compiles the current file or selected snippet and runs the resulting
  180. JavaScript.
  181. ![CoffeeRun](http://i.imgur.com/YSkHUuQ.png)
  182. ![CoffeeRun Output](http://i.imgur.com/wZQbggN.png)
  183. The command has two forms:
  184. :CoffeeRun [PROGRAM-OPTIONS]...
  185. This form applies when no `RANGE` is given or when the given range is `1,$`
  186. (first line to last line). It allows passing `PROGRAM-OPTIONS` to your compiled
  187. program. The filename is passed directly to `coffee` so you must save the file
  188. for your changes to take effect.
  189. :RANGE CoffeeRun [COFFEE-OPTIONS]...
  190. This form applies with all other ranges. It compiles and runs the lines within
  191. the given `RANGE` and any extra `COFFEE-OPTIONS` are passed to `coffee`.
  192. *Configuration*: [`coffee_compiler`](#coffee_compiler),
  193. [`coffee_run_vert`](#coffee_run_vert)
  194. ## CoffeeLint: Lint your CoffeeScript
  195. CoffeeLint runs [coffeelint](http://www.coffeelint.org/) (version 1.4.0 or later
  196. required) on the current file and adds any issues to the [quickfix] list.
  197. ![CoffeeLint](http://i.imgur.com/UN8Nr5N.png)
  198. ![CoffeeLint Result](http://i.imgur.com/9hSIj3W.png)
  199. :[RANGE] CoffeeLint[!] [COFFEELINT-OPTIONS]... [ | cwindow]
  200. If a `RANGE` is given, only those lines are piped to `coffeelint`. Options given
  201. in `COFFEELINT-OPTIONS` are passed to `coffeelint` (see also
  202. [`coffee_lint_options`](#coffee_lint_options)):
  203. :CoffeeLint -f lint.json
  204. It behaves very similar to `:make`, described [above](#compile-to-javascript).
  205. :CoffeeLint! | cwindow
  206. *Configuration*: [`coffee_linter`](#coffee_linter),
  207. [`coffee_lint_options`](#coffee_lint_options)
  208. ## Literate CoffeeScript
  209. Literate CoffeeScript syntax and indent support is provided by
  210. [vim-literate-coffeescript]. The `Coffee` commands detect when they're running
  211. on a litcoffee file and pass the `--literate` flag to their respective tools,
  212. but at this time the commands are not automatically loaded when a litcoffee file
  213. is opened.
  214. [vim-literate-coffeescript]: https://github.com/mintplant/vim-literate-coffeescript
  215. To load them, run
  216. runtime ftplugin/coffee.vim
  217. while inside a litcoffee buffer. To do this automatically, add
  218. autocmd FileType litcoffee runtime ftplugin/coffee.vim
  219. to your vimrc.
  220. ## CoffeeScript in HTML
  221. CoffeeScript is highlighted and indented within
  222. ```html
  223. <script type="text/coffeescript">
  224. </script>
  225. ```
  226. blocks in html files.
  227. ## CoffeeScript in Haml
  228. CoffeeScript is highlighted within the `:coffeescript` filter in haml files:
  229. ```haml
  230. :coffeescript
  231. console.log "hullo"
  232. ```
  233. At this time, coffee indenting doesn't work in these blocks.
  234. ## Custom Autocmds
  235. You can [define commands][autocmd-explain] to be ran automatically on these
  236. custom events.
  237. In all cases, the name of the command running the event (`CoffeeCompile`,
  238. `CoffeeWatch`, or `CoffeeRun`) is matched by the [`{pat}`][autocmd] argument.
  239. You can match all commands with a `*` or only specific commands by separating
  240. them with a comma: `CoffeeCompile,CoffeeWatch`.
  241. [autocmd-explain]: http://vimdoc.sourceforge.net/htmldoc/usr_40.html#40.3
  242. [autocmd]: http://vimdoc.sourceforge.net/htmldoc/autocmd.html#:autocmd
  243. #### CoffeeBufNew
  244. CoffeeBufNew is ran when a new scratch buffer is created. It's called from the
  245. new buffer, so it can be used to do additional set up.
  246. ```vim
  247. augroup CoffeeBufNew
  248. autocmd User * set wrap
  249. augroup END
  250. ```
  251. *Used By*: CoffeeCompile, CoffeeWatch, CoffeeRun
  252. #### CoffeeBufUpdate
  253. CoffeeBufUpdate is ran when a scratch buffer is updated with output from
  254. `coffee`. It's called from the scratch buffer, so it can be used to alter the
  255. compiled output.
  256. ```vim
  257. " Switch back to the source buffer after updating.
  258. augroup CoffeeBufUpdate
  259. autocmd User CoffeeCompile,CoffeeRun exec bufwinnr(b:coffee_src_buf) 'wincmd w'
  260. augroup END
  261. ```
  262. For example, to strip off the "Generated by" comment on the first line, put this
  263. in your vimrc:
  264. ```vim
  265. function! s:RemoveGeneratedBy()
  266. " If there was an error compiling, there's no comment to remove.
  267. if v:shell_error
  268. return
  269. endif
  270. " Save cursor position.
  271. let pos = getpos('.')
  272. " Remove first line.
  273. set modifiable
  274. 1 delete _
  275. set nomodifiable
  276. " Restore cursor position.
  277. call setpos('.', pos)
  278. endfunction
  279. augroup CoffeeBufUpdate
  280. autocmd User CoffeeCompile,CoffeeWatch call s:RemoveGeneratedBy()
  281. augroup END
  282. ```
  283. *Used By*: CoffeeCompile, CoffeeWatch, CoffeeRun
  284. ## Configuration Variables
  285. This is the full list of configuration variables available, with example
  286. settings and default values. Use these in your vimrc to control the default
  287. behavior.
  288. #### coffee\_indent\_keep\_current
  289. By default, the indent function matches the indent of the previous line if it
  290. doesn't find a reason to indent or outdent. To change this behavior so it
  291. instead keeps the [current indent of the cursor][98], use
  292. let coffee_indent_keep_current = 1
  293. [98]: https://github.com/kchmck/vim-coffee-script/pull/98
  294. *Default*: `unlet coffee_indent_keep_current`
  295. Note that if you change this after a coffee file has been loaded, you'll have to
  296. reload the indent script for the change to take effect:
  297. unlet b:did_indent | runtime indent/coffee.vim
  298. #### coffee\_compiler
  299. Path to the `coffee` executable used by the `Coffee` commands:
  300. let coffee_compiler = '/usr/bin/coffee'
  301. *Default*: `'coffee'` (search `$PATH` for executable)
  302. #### coffee\_make\_options
  303. Options to pass to `coffee` with `:make`:
  304. let coffee_make_options = '--bare'
  305. *Default*: `''` (nothing)
  306. Note that `coffee_make_options` is embedded into `'makeprg'`, so `:compiler
  307. coffee` must be ran after changing `coffee_make_options` for the changes to take
  308. effect.
  309. #### coffee\_cake
  310. Path to the `cake` executable:
  311. let coffee_cake = '/opt/bin/cake'
  312. *Default*: `'cake'` (search `$PATH` for executable)
  313. #### coffee\_cake\_options
  314. Options to pass to `cake` with `:make`:
  315. let coffee_cake_options = 'build'
  316. *Default*: `''` (nothing)
  317. #### coffee\_linter
  318. Path to the `coffeelint` executable:
  319. let coffee_linter = '/opt/bin/coffeelint'
  320. *Default*: `'coffeelint'` (search `$PATH` for executable)
  321. #### coffee\_lint\_options
  322. Options to pass to `coffeelint`:
  323. let coffee_lint_options = '-f lint.json'
  324. *Default*: `''` (nothing)
  325. #### coffee\_compile\_vert
  326. Open the CoffeeCompile buffer with a vertical split instead of a horizontal
  327. one:
  328. let coffee_compile_vert = 1
  329. *Default*: `unlet coffee_compile_vert`
  330. #### coffee\_watch\_vert
  331. Open the CoffeeWatch buffer with a vertical split instead of a horizontal
  332. one:
  333. let coffee_watch_vert = 1
  334. *Default*: `unlet coffee_watch_vert`
  335. #### coffee\_run\_vert
  336. Open the CoffeeRun buffer with a vertical split instead of a horizontal
  337. one:
  338. let coffee_run_vert = 1
  339. *Default*: `unlet coffee_run_vert`
  340. ## Configure Syntax Highlighting
  341. Add these lines to your vimrc to disable the relevant syntax group.
  342. #### Disable trailing whitespace error
  343. Trailing whitespace is highlighted as an error by default. This can be disabled
  344. with:
  345. hi link coffeeSpaceError NONE
  346. #### Disable trailing semicolon error
  347. Trailing semicolons are considered an error (for help transitioning from
  348. JavaScript.) This can be disabled with:
  349. hi link coffeeSemicolonError NONE
  350. #### Disable reserved words error
  351. Reserved words like `function` and `var` are highlighted as an error where
  352. they're not allowed in CoffeeScript. This can be disabled with:
  353. hi link coffeeReservedError NONE
  354. ## Tune Vim for CoffeeScript
  355. Changing these core settings can make vim more CoffeeScript friendly.
  356. #### Fold by indentation
  357. Folding by indentation works well for CoffeeScript functions and classes:
  358. ![Folding](http://i.imgur.com/gDgUBdO.png)
  359. To fold by indentation in CoffeeScript files, add this line to your vimrc:
  360. autocmd BufNewFile,BufReadPost *.coffee setl foldmethod=indent nofoldenable
  361. With this, folding is disabled by default but can be quickly toggled per-file
  362. by hitting `zi`. To enable folding by default, remove `nofoldenable`:
  363. autocmd BufNewFile,BufReadPost *.coffee setl foldmethod=indent
  364. #### Two-space indentation
  365. To get standard two-space indentation in CoffeeScript files, add this line to
  366. your vimrc:
  367. autocmd BufNewFile,BufReadPost *.coffee setl shiftwidth=2 expandtab