替 Vim 安裝語法檢查工具:Syntastic

Nov 18, 2016 Vim English

Syntastic 是一個檢查程式語法的工具,它不僅支援多種程式語言,而且可進一步結合其他工具針對 Coding Style 進行檢查 (如 Flake8 支援 PEP 8),對於想要把 Vim 當做 IDE 的人相當的有用。本篇將介紹如何在 macOS 上安裝、使用Syntastic



前置準備

我習慣使用套件管理工具 Vundle 來安裝 Vim 的套件,因此接下來也會使用它來進行安裝的步驟。你也可以試試看這個簡單、方便的工具。

安裝 Syntastic

接下來就透過 Vundle 來安裝 Syntastic。首先開啟 Vim 的設定檔 (/etc/vimrc~/.vimrc),接著加入套件名稱與相關設定於該檔案中。範例如下:

" 省略部分內容 ... "

set rtp+=~/.vim/bundle/Vundle.vim
call vundle#begin()

Plugin 'gmarik/Vundle.vim'

" 在此加入 Syntastic 套件 "
Plugin 'scrooloose/syntastic'

call vundle#end()
filetype plugin indent on

" 省略部分內容 ... "

" 以下為 Syntastic 相關設定,直接加入在 vimrc 檔案最後即可 "
set statusline+=%#warningmsg#
set statusline+=%{SyntasticStatuslineFlag()}
set statusline+=%*

let g:syntastic_always_populate_loc_list = 1
let g:syntastic_auto_loc_list = 1
let g:syntastic_check_on_open = 1
let g:syntastic_check_on_wq = 0

若你有特別想使用的檢查工具,也可以在設定檔中指定,如下:

" 指定其他檢查工具,但記得需要先自行安裝 "
let g:syntastic_<filetype>_checkers = ['<checker-name>']

" 例如:使用 Flake8 與 pylint 取代預設檢查工具 "
let g:syntastic_python_checkers = ['flake8', 'pylint']

此外,我們還可以設定特定的顯示符號或者是表情符號,如下:

let g:syntastic_error_symbol = 'Put a symbol or an emoji you like'
let g:syntastic_warning_symbol = 'Put a symbol or an emoji you like'

接著在 Vim 中執行安裝套件指令:

:PluginInstall

或者在 Terminal 中執行:

vim +PluginInstall +qall


使用 Syntastic

安裝完後,基本的語法檢查會根據不同檔案類別自動啟用,一旦執行儲存時它會自動開啟一個視窗並將錯誤的行號及原因列出。若需要更詳細的資訊,執行以下指令:

:help syntastic




你也可能會喜歡:




若對於文章內容有任何建議與指正,非常歡迎你告訴我或者與我一起討論 ! :)

zeckli.devforgalaxy@gmail.com   © 2015-2019 zeckli, thanks to Jekyll and GitHub.