簡體   English   中英

如何將jscs autofix功能集成到vim中?

[英]How can I integrate jscs autofix feature into vim?

我正在嘗試獲取一個命令,我可以在vim中運行,以便在我的代碼中使jscs自動糾正格式化問題。 到目前為止,我想出了:

:nmap <F5> :!jscs -x .<CR>

這是好的,但它在整個目錄上運行它,我需要確認vim我想重新加載緩沖區。 有沒有辦法讓vim只修復當前文件並在沒有重新加載的情況下顯示更改?

每當您保存文件時,這將通過jscs的修復模式管道當前文件(在實踐中您的里程可能會有所不同!):

function! JscsFix()
    "Save current cursor position"
    let l:winview = winsaveview()
    "Pipe the current buffer (%) through the jscs -x command"
    % ! jscs -x
    "Restore cursor position - this is needed as piping the file"
    "through jscs jumps the cursor to the top"
    call winrestview(l:winview)
endfunction
command! JscsFix :call JscsFix()

"Run the JscsFix command just before the buffer is written for *.js files"
autocmd BufWritePre *.js JscsFix

它還會創建一個命令JscsFix ,您可以JscsFix它運行:JscsFix 要將其綁定到鍵(在本例中為<leader>g ),請使用noremap <leader>g :JscsFix<cr>

vim-autoformat支持JSCS開箱即用。 調用其:Autoformat命令僅修復當前文件。 請注意,它會編輯當前緩沖區中的文件,因此只會出現更改; 系統不會提示您重新加載。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM