Vim關於Vue的生態鏈仍是不多,不過湊活湊活仍是能用的。javascript
縮進採用的是兩個空格,.vimrc配置:css
au BufNewFile,BufRead *.html,*.js,*.vue set tabstop=2 au BufNewFile,BufRead *.html,*.js,*.vue set softtabstop=2 au BufNewFile,BufRead *.html,*.js,*.vue set shiftwidth=2 au BufNewFile,BufRead *.html,*.js,*.vue set expandtab au BufNewFile,BufRead *.html,*.js,*.vue set autoindent au BufNewFile,BufRead *.html,*.js,*.vue set fileformat=unix
重要的語法高亮,支持最好的應該是vim-vue。html
使用Vundle下載:vue
Plugin 'posva/vim-vue'
:PluginInstall
這樣語法高亮基本上就實現了,不過會出現滑動過快高亮失效的狀況,文檔中給出的緣由是vue包含html、css、js語句,vim-vue有時候會感到困惑,解決辦法:java
autocmd FileType vue syntax sync fromstart
若是想使用一些已經寫好的html、css、js配置,能夠添加:git
autocmd BufRead,BufNewFile *.vue setlocal filetype=vue.html.javascript.css
語法檢查能夠使用vim-syntastic/syntastic配合ESLint。github
使用Vundle下載:npm
Plugin 'scrooloose/syntastic'
:PluginInstall
配置eslint檢查器:vim
let g:syntastic_javascript_checkers = ['eslint']
能夠使用打開一個vue文件,輸入命令:babel
:SyntasticInfo
能夠看到:
Syntastic version: 3.8.0-101 (Vim 704, Linux, GUI) Info for filetype: vue Global mode: active Filetype vue is active The current file will be checked automatically Available checker: eslint Currently enabled checker: eslint
如今就差ESLint了。
Vue的官方推薦ESLint插件是eslint-plugin-vue。
下載:
npm install eslint eslint-plugin-vue
配置.eslintrc
{ "extends": ["plugin:vue/recommended"], "plugins": [ "vue" ], "parserOptions": { "parser": "babel-eslint", }, "rules": { }, "settings": { "html/html-extensions": [".html", ".vue"], }, }
注意,配置中不要包含eslint-plugin-html插件,eslint-plugin-html會使eslint-plugin-vue失效,由於eslint-plugin-html會從<script>中提取內容,但eslint-plugin-vue是須要<script>和<template>標籤的。
"plugins": [ "vue", - "html" //去除 ]
這樣,:w保存vue文件時就會有紅塊報錯了:
在每行以前的 >>
表示該行中有語法錯誤,將光標移到該行,錯誤描述就會展現在 Vim 窗口的最底下。
輸入:Errors命令也會打印出詳細的報錯信息。
配合eslint-plugin-standard使用的時候,若是<script>縮進以下:
<script> let a = { foo: 1, bar: 2 } </script>
會報縮進錯誤:
Expected indentation of 0 spaces but found 2. (indent)
官方給出瞭解決方法:vue/script-indent
{ "extends": ["plugin:vue/recommended", "standard"], "plugins": [ "vue" ], "parserOptions": { "parser": "babel-eslint", }, "rules": { "vue/script-indent": ["error", 2, { "baseIndent": 1 }] }, "settings": { "html/html-extensions": [".html", ".vue"], }, "overrides": [ { "files": ["*.vue"], "rules": { "indent": "off" } } ] }
一是添加rule:
"vue/script-indent": ["error", 2, { "baseIndent": 1 }]
數字2表明1次縮進2個空格,數字1表明縮進1次。
二是關閉默認indent:
"overrides": [ { "files": ["*.vue"], "rules": { "indent": "off" } } ]
over。能夠愉悅得用Vim寫Vue了。